001: /***
002: * ASM Guide
003: * Copyright (c) 2007 Eric Bruneton
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package ch3.sec2;
030:
031: import java.lang.reflect.Method;
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: import org.objectweb.asm.ClassWriter;
036: import org.objectweb.asm.Type;
037: import org.objectweb.asm.util.CheckClassAdapter;
038:
039: import util.AbstractTestCase;
040:
041: /**
042: * ASM Guide example test class.
043: *
044: * @author Eric Bruneton
045: */
046: public class BeanGenerator3Test extends AbstractTestCase {
047:
048: public void test() throws Exception {
049: Map<String, Type> fields = new HashMap<String, Type>();
050: fields.put("f0", Type.BOOLEAN_TYPE);
051: fields.put("f1", Type.INT_TYPE);
052: fields.put("f2", Type.LONG_TYPE);
053: fields.put("f3", Type.FLOAT_TYPE);
054: fields.put("f4", Type.DOUBLE_TYPE);
055: fields.put("f5", Type.getType("Ljava/lang/String;"));
056: fields.put("f6", Type.getType("[I"));
057: BeanGenerator3 bg = new BeanGenerator3("MyBean", fields);
058:
059: ClassWriter cw = new ClassWriter(0);
060: CheckClassAdapter ca = new CheckClassAdapter(cw);
061: bg.generate(ca);
062:
063: Class c = defineClass("MyBean", cw.toByteArray());
064: checkClass(c);
065: }
066:
067: protected void checkClass(Class c) throws Exception {
068: Object bean = c.newInstance();
069: Method getF0 = c.getMethod("getF0");
070: Method setF0 = c.getMethod("setF0", boolean.class);
071: setF0.invoke(bean, true);
072: assertEquals(true, getF0.invoke(bean));
073:
074: Method getF1 = c.getMethod("getF1");
075: Method setF1 = c.getMethod("setF1", int.class);
076: setF1.invoke(bean, 1);
077: assertEquals(1, getF1.invoke(bean));
078:
079: Method getF2 = c.getMethod("getF2");
080: Method setF2 = c.getMethod("setF2", long.class);
081: setF2.invoke(bean, 1L);
082: assertEquals(1L, getF2.invoke(bean));
083:
084: Method getF3 = c.getMethod("getF3");
085: Method setF3 = c.getMethod("setF3", float.class);
086: setF3.invoke(bean, 1f);
087: assertEquals(1f, getF3.invoke(bean));
088:
089: Method getF4 = c.getMethod("getF4");
090: Method setF4 = c.getMethod("setF4", double.class);
091: setF4.invoke(bean, 1d);
092: assertEquals(1d, getF4.invoke(bean));
093:
094: Method getF5 = c.getMethod("getF5");
095: Method setF5 = c.getMethod("setF5", String.class);
096: setF5.invoke(bean, "1");
097: assertEquals("1", getF5.invoke(bean));
098:
099: Method getF6 = c.getMethod("getF6");
100: Method setF6 = c.getMethod("setF6", int[].class);
101: setF6.invoke(bean, new Object[] { new int[] { 1 } });
102: int[] v = (int[]) getF6.invoke(bean);
103: assertNotNull(v);
104: assertEquals(1, v.length);
105: assertEquals(1, v[0]);
106: }
107: }
|