You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReflectionBasedReferenceTypeDelegateTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* *******************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Adrian Colyer Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.reflect;
  13. import java.lang.reflect.Method;
  14. import java.util.HashSet;
  15. import java.util.Set;
  16. import org.aspectj.bridge.IMessageHandler;
  17. import org.aspectj.weaver.ReferenceType;
  18. import org.aspectj.weaver.ResolvedMember;
  19. import org.aspectj.weaver.ResolvedType;
  20. import org.aspectj.weaver.UnresolvedType;
  21. import org.aspectj.weaver.bcel.BcelWorld;
  22. import junit.framework.TestCase;
  23. public abstract class ReflectionBasedReferenceTypeDelegateTest extends TestCase {
  24. protected ReflectionWorld world;
  25. private ResolvedType objectType;
  26. private ResolvedType classType;
  27. public void testIsAspect() {
  28. assertFalse(objectType.isAspect());
  29. }
  30. public void testIsAnnotationStyleAspect() {
  31. assertFalse(objectType.isAnnotationStyleAspect());
  32. }
  33. public void testIsInterface() {
  34. assertFalse(objectType.isInterface());
  35. assertTrue(world.resolve("java.io.Serializable").isInterface());
  36. }
  37. public void testIsEnum() {
  38. assertFalse(objectType.isEnum());
  39. }
  40. public void testIsAnnotation() {
  41. assertFalse(objectType.isAnnotation());
  42. }
  43. public void testIsAnnotationWithRuntimeRetention() {
  44. assertFalse(objectType.isAnnotationWithRuntimeRetention());
  45. }
  46. public void testIsClass() {
  47. assertTrue(objectType.isClass());
  48. assertFalse(world.resolve("java.io.Serializable").isClass());
  49. }
  50. public void testIsGeneric() {
  51. assertFalse(objectType.isGenericType());
  52. }
  53. public void testIsExposedToWeaver() {
  54. assertFalse(objectType.isExposedToWeaver());
  55. }
  56. public void testHasAnnotation() {
  57. assertFalse(objectType.hasAnnotation(UnresolvedType.forName("Foo")));
  58. }
  59. public void testGetAnnotations() {
  60. assertEquals("no entries", 0, objectType.getAnnotations().length);
  61. }
  62. public void testGetAnnotationTypes() {
  63. assertEquals("no entries", 0, objectType.getAnnotationTypes().length);
  64. }
  65. public void testGetTypeVariables() {
  66. assertEquals("no entries", 0, objectType.getTypeVariables().length);
  67. }
  68. public void testGetPerClause() {
  69. assertNull(objectType.getPerClause());
  70. }
  71. public void testGetModifiers() {
  72. assertEquals(Object.class.getModifiers(), objectType.getModifiers());
  73. }
  74. public void testGetSuperclass() {
  75. assertTrue("Superclass of object should be null, but it is: " + objectType.getSuperclass(),
  76. objectType.getSuperclass() == null);
  77. assertEquals(objectType, world.resolve("java.lang.Class").getSuperclass());
  78. ResolvedType d = world.resolve("reflect.tests.D");
  79. assertEquals(world.resolve("reflect.tests.C"), d.getSuperclass());
  80. }
  81. protected int findMethod(String name, ResolvedMember[] methods) {
  82. for (int i = 0; i < methods.length; i++) {
  83. if (name.equals(methods[i].getName())) {
  84. return i;
  85. }
  86. }
  87. return -1;
  88. }
  89. protected int findMethod(String name, int numArgs, ResolvedMember[] methods) {
  90. for (int i = 0; i < methods.length; i++) {
  91. if (name.equals(methods[i].getName()) && (methods[i].getParameterTypes().length == numArgs)) {
  92. return i;
  93. }
  94. }
  95. return -1;
  96. }
  97. public void testGetDeclaredMethods() {
  98. ResolvedMember[] methods = objectType.getDeclaredMethods();
  99. assertEquals(Object.class.getDeclaredMethods().length + Object.class.getDeclaredConstructors().length, methods.length);
  100. ResolvedType c = world.resolve("reflect.tests.C");
  101. methods = c.getDeclaredMethods();
  102. assertEquals(3, methods.length);
  103. int idx = findMethod("foo", methods);
  104. assertTrue(idx > -1);
  105. assertEquals(world.resolve("java.lang.String"), methods[idx].getReturnType());
  106. assertEquals(1, methods[idx].getParameterTypes().length);
  107. assertEquals(objectType, methods[idx].getParameterTypes()[0]);
  108. assertEquals(1, methods[idx].getExceptions().length);
  109. assertEquals(world.resolve("java.lang.Exception"), methods[idx].getExceptions()[0]);
  110. int baridx = findMethod("bar", methods);
  111. int initidx = findMethod("<init>", methods);
  112. assertTrue(baridx > -1);
  113. assertTrue(initidx > -1);
  114. assertTrue(baridx != initidx && baridx != idx && idx <= 2 && initidx <= 2 && baridx <= 2);
  115. ResolvedType d = world.resolve("reflect.tests.D");
  116. methods = d.getDeclaredMethods();
  117. assertEquals(2, methods.length);
  118. classType = world.resolve("java.lang.Class");
  119. methods = classType.getDeclaredMethods();
  120. assertEquals(Class.class.getDeclaredMethods().length + Class.class.getDeclaredConstructors().length, methods.length);
  121. }
  122. public void testGetDeclaredFields() {
  123. ResolvedMember[] fields = objectType.getDeclaredFields();
  124. assertEquals(0, fields.length);
  125. ResolvedType c = world.resolve("reflect.tests.C");
  126. fields = c.getDeclaredFields();
  127. assertEquals(2, fields.length);
  128. assertEquals("f", fields[0].getName());
  129. assertEquals("s", fields[1].getName());
  130. assertEquals(UnresolvedType.INT, fields[0].getReturnType());
  131. assertEquals(world.resolve("java.lang.String"), fields[1].getReturnType());
  132. }
  133. public void testGetDeclaredInterfaces() {
  134. ResolvedType[] interfaces = objectType.getDeclaredInterfaces();
  135. assertEquals(0, interfaces.length);
  136. ResolvedType d = world.resolve("reflect.tests.D");
  137. interfaces = d.getDeclaredInterfaces();
  138. assertEquals(1, interfaces.length);
  139. assertEquals(world.resolve("java.io.Serializable"), interfaces[0]);
  140. }
  141. public void testGetDeclaredPointcuts() {
  142. ResolvedMember[] pointcuts = objectType.getDeclaredPointcuts();
  143. assertEquals(0, pointcuts.length);
  144. }
  145. public void testSerializableSuperclass() {
  146. ResolvedType serializableType = world.resolve("java.io.Serializable");
  147. ResolvedType superType = serializableType.getSuperclass();
  148. assertTrue("Superclass of serializable should be Object but was " + superType, superType.equals(UnresolvedType.OBJECT));
  149. BcelWorld bcelworld = new BcelWorld();
  150. bcelworld.setBehaveInJava5Way(true);
  151. ResolvedType bcelSupertype = bcelworld.resolve(UnresolvedType.SERIALIZABLE).getSuperclass();
  152. assertTrue("Should be null but is " + bcelSupertype, bcelSupertype.equals(UnresolvedType.OBJECT));
  153. }
  154. public void testSubinterfaceSuperclass() {
  155. ResolvedType ifaceType = world.resolve("java.security.Key");
  156. ResolvedType superType = ifaceType.getSuperclass();
  157. assertTrue("Superclass should be Object but was " + superType, superType.equals(UnresolvedType.OBJECT));
  158. BcelWorld bcelworld = new BcelWorld();
  159. bcelworld.setBehaveInJava5Way(true);
  160. ResolvedType bcelSupertype = bcelworld.resolve("java.security.Key").getSuperclass();
  161. assertTrue("Should be null but is " + bcelSupertype, bcelSupertype.equals(UnresolvedType.OBJECT));
  162. }
  163. public void testVoidSuperclass() {
  164. ResolvedType voidType = world.resolve(Void.TYPE);
  165. ResolvedType superType = voidType.getSuperclass();
  166. assertNull(superType);
  167. BcelWorld bcelworld = new BcelWorld();
  168. bcelworld.setBehaveInJava5Way(true);
  169. ResolvedType bcelSupertype = bcelworld.resolve("void").getSuperclass();
  170. assertTrue("Should be null but is " + bcelSupertype, bcelSupertype == null);
  171. }
  172. public void testIntSuperclass() {
  173. ResolvedType voidType = world.resolve(Integer.TYPE);
  174. ResolvedType superType = voidType.getSuperclass();
  175. assertNull(superType);
  176. BcelWorld bcelworld = new BcelWorld();
  177. bcelworld.setBehaveInJava5Way(true);
  178. ResolvedType bcelSupertype = bcelworld.resolve("int").getSuperclass();
  179. assertTrue("Should be null but is " + bcelSupertype, bcelSupertype == null);
  180. }
  181. public void testGenericInterfaceSuperclass_BcelWorldResolution() {
  182. BcelWorld bcelworld = new BcelWorld();
  183. bcelworld.setBehaveInJava5Way(true);
  184. UnresolvedType javaUtilMap = UnresolvedType.forName("java.util.Map");
  185. ReferenceType rawType = (ReferenceType) bcelworld.resolve(javaUtilMap);
  186. assertTrue("Should be the raw type ?!? " + rawType.getTypekind(), rawType.isRawType());
  187. ReferenceType genericType = (ReferenceType) rawType.getGenericType();
  188. assertTrue("Should be the generic type ?!? " + genericType.getTypekind(), genericType.isGenericType());
  189. ResolvedType rt = rawType.getSuperclass();
  190. assertTrue("Superclass for Map raw type should be Object but was " + rt, rt.equals(UnresolvedType.OBJECT));
  191. ResolvedType rt2 = genericType.getSuperclass();
  192. assertTrue("Superclass for Map generic type should be Object but was " + rt2, rt2.equals(UnresolvedType.OBJECT));
  193. }
  194. // FIXME asc maybe. The reflection list of methods returned doesn't include <clinit> (the static initializer) ... is that really
  195. // a problem.
  196. public void testCompareSubclassDelegates() {
  197. boolean barfIfClinitMissing = false;
  198. world.setBehaveInJava5Way(true);
  199. BcelWorld bcelWorld = new BcelWorld(getClass().getClassLoader(), IMessageHandler.THROW, null);
  200. bcelWorld.setBehaveInJava5Way(true);
  201. UnresolvedType javaUtilHashMap = UnresolvedType.forName("java.util.HashMap");
  202. ReferenceType rawType = (ReferenceType) bcelWorld.resolve(javaUtilHashMap);
  203. ReferenceType rawReflectType = (ReferenceType) world.resolve(javaUtilHashMap);
  204. ResolvedMember[] rms1 = rawType.getDelegate().getDeclaredMethods();
  205. ResolvedMember[] rms2 = rawReflectType.getDelegate().getDeclaredMethods();
  206. StringBuffer errors = new StringBuffer();
  207. Set one = new HashSet();
  208. for (int i = 0; i < rms1.length; i++) {
  209. one.add(rms1[i].toString());
  210. }
  211. Set two = new HashSet();
  212. for (int i = 0; i < rms2.length; i++) {
  213. two.add(rms2[i].toString());
  214. }
  215. for (int i = 0; i < rms2.length; i++) {
  216. if (!one.contains(rms2[i].toString())) {
  217. errors.append("Couldn't find " + rms2[i].toString() + " in the bcel set\n");
  218. }
  219. }
  220. for (int i = 0; i < rms1.length; i++) {
  221. if (!two.contains(rms1[i].toString())) {
  222. if (!barfIfClinitMissing && rms1[i].getName().equals("<clinit>"))
  223. continue;
  224. errors.append("Couldn't find " + rms1[i].toString() + " in the reflection set\n");
  225. }
  226. }
  227. assertTrue("Errors:" + errors.toString(), errors.length() == 0);
  228. // the good old ibm vm seems to offer clinit through its reflection support (see pr145322)
  229. if (rms1.length == rms2.length)
  230. return;
  231. if (barfIfClinitMissing) {
  232. // the numbers must be exact
  233. assertEquals(rms1.length, rms2.length);
  234. } else {
  235. // the numbers can be out by one in favour of bcel
  236. if (rms1.length != (rms2.length + 1)) {
  237. for (int i = 0; i < rms1.length; i++) {
  238. System.err.println("bcel" + i + " is " + rms1[i]);
  239. }
  240. for (int i = 0; i < rms2.length; i++) {
  241. System.err.println("refl" + i + " is " + rms2[i]);
  242. }
  243. }
  244. assertTrue("Should be one extra (clinit) in BCEL case, but bcel=" + rms1.length + " reflect=" + rms2.length,
  245. rms1.length == rms2.length + 1);
  246. }
  247. }
  248. public void testArrayArgsSig() throws Exception {
  249. Method invokeMethod = Method.class.getMethod("invoke", new Class[] { Object.class, Object[].class });
  250. ResolvedMember reflectionMethod = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMethod(invokeMethod, world);
  251. String exp = "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;";
  252. assertTrue("Expected: \n" + exp + "\n but got:\n" + reflectionMethod.getSignature(), reflectionMethod.getSignature()
  253. .equals(exp));
  254. }
  255. // todo: array of int
  256. protected void setUp() throws Exception {
  257. world = new ReflectionWorld(getClass().getClassLoader());
  258. objectType = world.resolve("java.lang.Object");
  259. }
  260. }