1 18 19 package alt.jiapi.reflect; 20 21 import java.lang.reflect.Modifier ; 22 23 import junit.framework.TestCase; 24 25 42 public class ReflectionTest extends TestCase { 43 private Object testField; 45 private Loader loader; 46 private JiapiClass clazz; 47 48 public ReflectionTest(String name) { 49 super(name); 50 } 51 52 public String getName() { 53 return "ReflectionTest: " + super.getName(); 54 } 55 56 57 protected void setUp() { 58 try { 59 loader = new Loader(); 60 clazz = loader.loadClass(getClass().getName()); 62 } 63 catch (Exception e) { 64 fail(e.getMessage()); 65 } 66 } 67 68 72 public void testMethodGetting() { 73 try { 74 JiapiMethod m = null; 75 76 m = clazz.getDeclaredMethod("m_void", new String [0]); 77 assertNotNull(m); 78 m = clazz.getDeclaredMethod("m_object", 79 new String []{"java.lang.Object"}); 80 assertNotNull(m); 81 m = clazz.getDeclaredMethod("m_int", new String []{"int"}); 82 assertNotNull(m); 83 m = clazz.getDeclaredMethod("m_long", new String []{"long"}); 84 assertNotNull(m); 85 m = clazz.getDeclaredMethod("m_byte", new String []{"byte"}); 86 assertNotNull(m); 87 m = clazz.getDeclaredMethod("m_char", new String []{"char"}); 88 assertNotNull(m); 89 m = clazz.getDeclaredMethod("m_float", new String []{"float"}); 90 assertNotNull(m); 91 m = clazz.getDeclaredMethod("m_double", new String []{"double"}); 92 assertNotNull(m); 93 m = clazz.getDeclaredMethod("m_boolean", new String []{"boolean"}); 94 assertNotNull(m); 95 m = clazz.getDeclaredMethod("boolean_m_boolean", 96 new String []{"boolean"}); 97 assertNotNull(m); 98 m = clazz.getDeclaredMethod("m_object_array", 99 new String []{"java.lang.Object[]"}); 100 assertNotNull(m); 101 m = clazz.getDeclaredMethod("m_int_array", new String []{"int[]"}); 102 assertNotNull(m); 103 m = clazz.getDeclaredMethod("m_int_array_long_array", 104 new String []{"int[]", "long[]"}); 105 assertNotNull(m); 106 m = clazz.getDeclaredMethod("m_int_array_long_array", 107 new String []{"int []", "long []"}); 108 assertNotNull(m); 109 110 m = null; 111 try { 112 m = clazz.getDeclaredMethod("nonExistentMethod", 113 new String []{"int[]", "long[]"}); 114 } 115 catch(Exception e) { 116 } 118 assertNull(m); 119 } 120 catch (Exception e) { 121 fail(e.getMessage()); } 123 } 124 125 126 127 130 public void testAdditions() { 131 JiapiField f = null; 133 try { 134 f = clazz.addField("newField1"); 135 assertNotNull(f); 136 137 try { 139 f = clazz.addField(f.getName()); 140 fail("FieldExistsException should have been thrown"); 141 } 142 catch (Exception e) { 143 assertTrue(true); } 145 } 146 catch (Exception e) { 147 fail(e.getMessage()); 148 } 149 150 try { 151 f = clazz.addField(Modifier.PUBLIC, "int", "newField2"); 153 assertNotNull(f); 154 f = clazz.addField(Modifier.PUBLIC, "java.lang.Object", 156 "newField3"); 157 assertNotNull(f); 158 } 159 catch (Exception e) { 160 fail(e.getMessage()); 161 } 162 163 164 JiapiMethod m = null; 165 try { 166 m = clazz.addMethod(Modifier.PUBLIC, "newMethod", 167 new Signature("void", new String [0])); 168 assertNotNull(m); 169 170 try { 172 m = clazz.addMethod(m); 173 fail("MethodExistsException should have been thrown"); 174 } 175 catch (Exception e) { 176 assertTrue(true); } 178 } 179 catch (Exception e) { 180 fail(e.getMessage()); 181 } 182 } 183 184 public void m_void() {} 188 public void m_object(Object o) {} 189 public void m_int(int i) {} 190 public void m_long(long l) {} 191 public void m_byte(byte b) {} 192 public void m_char(char l) {} 193 public void m_float(float l) {} 194 public void m_double(double l) {} 195 public void m_boolean(boolean l) {} 196 197 public boolean boolean_m_boolean(boolean l) { return false;} 198 199 public void m_object_array(Object [] o) {} 200 public void m_int_array(int[] i) {} 201 public void m_int_array_long_array(int[] i, long[] l) {} 202 } 203 204 | Popular Tags |