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.

AjTypeTest.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.internal.lang.reflect;
  13. import java.io.Serializable;
  14. import java.lang.annotation.Retention;
  15. import java.lang.annotation.RetentionPolicy;
  16. import java.lang.reflect.Constructor;
  17. import java.lang.reflect.Field;
  18. import java.lang.reflect.Method;
  19. import java.lang.reflect.Type;
  20. import java.lang.reflect.TypeVariable;
  21. import junit.framework.TestCase;
  22. import org.aspectj.lang.reflect.AjType;
  23. import org.aspectj.lang.reflect.AjTypeSystem;
  24. public class AjTypeTest extends TestCase {
  25. private AjType<String> stringType;
  26. @Override
  27. protected void setUp() throws Exception {
  28. super.setUp();
  29. stringType = AjTypeSystem.getAjType(String.class);
  30. }
  31. public void testCreateAjType() {
  32. assertNotNull("should find type",stringType);
  33. }
  34. public void testGetName() {
  35. assertEquals(String.class.getName(),stringType.getName());
  36. }
  37. public void testGetPackage() {
  38. assertEquals(String.class.getPackage(),stringType.getPackage());
  39. }
  40. public void testGetInterfaces() {
  41. Class[] i1 = String.class.getInterfaces();
  42. AjType<?>[] i2 = stringType.getInterfaces();
  43. assertEquals(i1.length,i2.length);
  44. for (int i = 0; i < i1.length; i++)
  45. assertEquals(i1[i],i2[i].getJavaClass());
  46. }
  47. public void testGetModifiers() {
  48. assertEquals(String.class.getModifiers(),stringType.getModifiers());
  49. }
  50. public void testGetSupertype() {
  51. Class<?> stringSuper = String.class.getSuperclass();
  52. AjType ajSuper = stringType.getSupertype();
  53. assertEquals(AjTypeSystem.getAjType(stringSuper),ajSuper);
  54. }
  55. public void testObjectSupertype() {
  56. AjType<?> objectSuper = AjTypeSystem.getAjType(Object.class).getSupertype();
  57. assertNull(objectSuper);
  58. }
  59. public void testInterfaceSupertype() {
  60. AjType<?> serializableSuper = AjTypeSystem.getAjType(Serializable.class).getSupertype();
  61. assertNull(serializableSuper);
  62. }
  63. public void testGetGenericSupertype() {
  64. Type t = AjTypeSystem.getAjType(Goo.class).getGenericSupertype();
  65. assertEquals(Foo.class,t);
  66. }
  67. public void testGetEnclosingMethod() {
  68. new Goo().foo();
  69. }
  70. public void testGetEnclosingConstructor() {
  71. new Goo();
  72. }
  73. public void testGetEnclosingType() {
  74. AjType t = AjTypeSystem.getAjType(Foo.Z.class);
  75. assertEquals("org.aspectj.internal.lang.reflect.Foo",t.getEnclosingType().getName());
  76. }
  77. public void testGetDeclaringType() {
  78. AjType t = AjTypeSystem.getAjType(Foo.Z.class);
  79. assertEquals("org.aspectj.internal.lang.reflect.Foo",t.getDeclaringType().getName());
  80. }
  81. public void testIsAnnotationPresent() {
  82. AjType<Foo> foo = AjTypeSystem.getAjType(Foo.class);
  83. AjType<Goo> goo = AjTypeSystem.getAjType(Goo.class);
  84. assertTrue(foo.isAnnotationPresent(SomeAnn.class));
  85. assertFalse(goo.isAnnotationPresent(SomeAnn.class));
  86. }
  87. public void testGetAnnotation() {
  88. AjType<Foo> foo = AjTypeSystem.getAjType(Foo.class);
  89. AjType<Goo> goo = AjTypeSystem.getAjType(Goo.class);
  90. assertNotNull(foo.getAnnotation(SomeAnn.class));
  91. assertNull(goo.getAnnotation(SomeAnn.class));
  92. }
  93. public void testGetAnnotations() {
  94. AjType<Foo> foo = AjTypeSystem.getAjType(Foo.class);
  95. AjType<Goo> goo = AjTypeSystem.getAjType(Goo.class);
  96. assertEquals(1,foo.getAnnotations().length);
  97. assertEquals(0,goo.getAnnotations().length);
  98. }
  99. public void testGetDeclaredAnnotations() {
  100. AjType<Foo> foo = AjTypeSystem.getAjType(Foo.class);
  101. AjType<Goo> goo = AjTypeSystem.getAjType(Goo.class);
  102. assertEquals(0,goo.getDeclaredAnnotations().length);
  103. assertEquals(1,foo.getDeclaredAnnotations().length);
  104. }
  105. public void testGetAjTypes() {
  106. AjType<Foo> foo = AjTypeSystem.getAjType(Foo.class);
  107. AjType[] fooTypes = foo.getAjTypes();
  108. assertEquals(1,fooTypes.length);
  109. assertEquals("org.aspectj.internal.lang.reflect.Foo$Z",fooTypes[0].getName());
  110. }
  111. public void testGetDeclaredAjTypes() {
  112. AjType<Foo> foo = AjTypeSystem.getAjType(Foo.class);
  113. AjType[] fooTypes = foo.getDeclaredAjTypes();
  114. assertEquals(2,fooTypes.length);
  115. // Alex -> Adrian: looks like you can not make assumption on the ordering
  116. String s = " " + fooTypes[0].getName() + " " + fooTypes[1].getName();
  117. assertTrue(s.contains(" org.aspectj.internal.lang.reflect.Foo$Z"));
  118. assertTrue(s.contains(" org.aspectj.internal.lang.reflect.Foo$XX"));
  119. }
  120. public void testGetConstructor() throws Exception {
  121. Constructor c1 = String.class.getConstructor(String.class);
  122. Constructor c2 = stringType.getConstructor(stringType);
  123. assertEquals(c1,c2);
  124. }
  125. public void testGetConstructors() {
  126. Constructor[] c1 = String.class.getConstructors();
  127. Constructor[] c2 = stringType.getConstructors();
  128. assertEquals(c1.length,c2.length);
  129. for (int i = 0; i < c1.length; i++)
  130. assertEquals(c1[i],c2[i]);
  131. }
  132. public void testGetDeclaredConstructor() throws Exception {
  133. Constructor c1 = String.class.getDeclaredConstructor(String.class);
  134. Constructor c2 = stringType.getDeclaredConstructor(stringType);
  135. assertEquals(c1,c2);
  136. }
  137. public void testGetDeclaredConstructors() {
  138. Constructor[] c1 = String.class.getDeclaredConstructors();
  139. Constructor[] c2 = stringType.getDeclaredConstructors();
  140. assertEquals(c1.length,c2.length);
  141. for (int i = 0; i < c1.length; i++)
  142. assertEquals(c1[i],c2[i]);
  143. }
  144. public void testGetDeclaredField() throws Exception {
  145. Field f1 = String.class.getDeclaredField("value");
  146. Field f2 = stringType.getDeclaredField("value");
  147. assertEquals(f1,f2);
  148. }
  149. public void testGetDeclaredFields() {
  150. Field[] f1 = String.class.getDeclaredFields();
  151. Field[] f2 = stringType.getDeclaredFields();
  152. assertEquals(f1.length,f2.length);
  153. for (int i = 0; i < f1.length; i++)
  154. assertEquals(f1[i],f2[i]);
  155. }
  156. public void testGetField() throws Exception {
  157. AjType<Goo> goo = AjTypeSystem.getAjType(Goo.class);
  158. assertEquals("g",goo.getField("g").getName());
  159. }
  160. public void testGetFields() {
  161. AjType<Goo> goo = AjTypeSystem.getAjType(Goo.class);
  162. Field[] fields = goo.getFields();
  163. assertEquals(1,fields.length);
  164. assertEquals("g",fields[0].getName());
  165. }
  166. public void testGetDeclaredMethod() throws Exception {
  167. Method m1 = String.class.getDeclaredMethod("toUpperCase");
  168. Method m2 = stringType.getDeclaredMethod("toUpperCase");
  169. assertEquals(m1,m2);
  170. }
  171. public void testGetMethod() throws Exception {
  172. Method m1 = String.class.getMethod("toUpperCase");
  173. Method m2 = stringType.getMethod("toUpperCase");
  174. assertEquals(m1,m2);
  175. }
  176. public void testGetDeclaredMethods() {
  177. Method[] m1 = String.class.getDeclaredMethods();
  178. Method[] m2 = stringType.getDeclaredMethods();
  179. assertEquals(m1.length,m2.length);
  180. for (int i = 0; i < m1.length; i++)
  181. assertEquals(m1[i],m2[i]);
  182. }
  183. public void testGetMethods() {
  184. Method[] m1 = String.class.getMethods();
  185. Method[] m2 = stringType.getMethods();
  186. assertEquals(m1.length,m2.length);
  187. for (int i = 0; i < m1.length; i++)
  188. assertEquals(m1[i],m2[i]);
  189. }
  190. public void testGetEnumConstants() {
  191. AjType e = AjTypeSystem.getAjType(E.class);
  192. Object[] consts = e.getEnumConstants();
  193. assertEquals(3,consts.length);
  194. }
  195. public void testGetTypeParameters() {
  196. AjType<Foo> foo = AjTypeSystem.getAjType(Foo.class);
  197. TypeVariable<Class<Foo>>[] tvs = foo.getTypeParameters();
  198. assertEquals(1,tvs.length);
  199. assertEquals("T",tvs[0].getName());
  200. }
  201. public void testIsEnum() {
  202. assertFalse(stringType.isEnum());
  203. }
  204. public void testIsInstance() {
  205. assertTrue(stringType.isInstance("I am"));
  206. }
  207. public void testIsInterface() {
  208. assertFalse(stringType.isInterface());
  209. assertTrue(AjTypeSystem.getAjType(Serializable.class).isInterface());
  210. }
  211. public void testIsLocalClass() {
  212. assertFalse(stringType.isLocalClass());
  213. }
  214. public void testIsArray() {
  215. assertFalse(stringType.isArray());
  216. assertTrue(AjTypeSystem.getAjType(Integer[].class).isArray());
  217. }
  218. public void testIsPrimitive() {
  219. assertFalse(stringType.isPrimitive());
  220. assertTrue(AjTypeSystem.getAjType(boolean.class).isPrimitive());
  221. }
  222. public void testIsAspect() {
  223. assertFalse(stringType.isAspect());
  224. }
  225. public void testIsMemberAspect() {
  226. assertFalse(stringType.isMemberAspect());
  227. }
  228. public void testIsPrivileged() {
  229. assertFalse(stringType.isPrivileged());
  230. }
  231. public void testEquals() {
  232. AjType stringTypeTwo = AjTypeSystem.getAjType(String.class);
  233. assertTrue(stringType.equals(stringTypeTwo));
  234. }
  235. public void testHashCode() {
  236. AjType stringTypeTwo = AjTypeSystem.getAjType(String.class);
  237. assertEquals(stringType.hashCode(),stringTypeTwo.hashCode());
  238. }
  239. }
  240. @Retention(RetentionPolicy.RUNTIME)
  241. @interface SomeAnn {}
  242. @SomeAnn
  243. class Foo<T> {
  244. public Foo() {
  245. class Y { int y; }
  246. AjType t = AjTypeSystem.getAjType(Y.class);
  247. Constructor c = t.getEnclosingConstructor();
  248. if (!c.getName().equals("org.aspectj.internal.lang.reflect.Foo")) throw new RuntimeException("should have been Foo");
  249. }
  250. public void foo() {
  251. class X { int x; }
  252. AjType t = AjTypeSystem.getAjType(X.class);
  253. Method m = t.getEnclosingMethod();
  254. if (!m.getName().equals("foo")) throw new RuntimeException("should have been foo");
  255. }
  256. public class Z { int z; }
  257. class XX { int xx; }
  258. }
  259. class Goo extends Foo {
  260. @interface IX {}
  261. public Goo() {
  262. super();
  263. }
  264. public int g;
  265. int g2;
  266. }
  267. enum E { A,B,C; }