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.

TestJava5ReflectionBasedReferenceTypeDelegate.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* *******************************************************************
  2. * Copyright (c) 2005-2017 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. * Andrew Clement Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import junit.framework.Test;
  14. import junit.framework.TestSuite;
  15. import java.lang.annotation.Retention;
  16. import java.lang.annotation.RetentionPolicy;
  17. import java.lang.reflect.Field;
  18. import org.aspectj.weaver.reflect.ReflectionBasedReferenceTypeDelegateTest;
  19. public class TestJava5ReflectionBasedReferenceTypeDelegate extends ReflectionBasedReferenceTypeDelegateTest {
  20. public static Test suite() {
  21. TestSuite suite = new TestSuite("TestJava5ReflectionBasedReferenceTypeDelegate");
  22. suite.addTestSuite(TestJava5ReflectionBasedReferenceTypeDelegate.class);
  23. return suite;
  24. }
  25. /**
  26. * Let's play about with a generic type and ensure we can work with it in a reflective world.
  27. */
  28. public void testResolveGeneric() {
  29. UnresolvedType collectionType = UnresolvedType.forName("java.util.Collection");
  30. world.resolve(collectionType).getRawType().resolve(world);
  31. ResolvedMember[] methods = world.resolve(collectionType).getDeclaredMethods();
  32. int i = findMethod("toArray", 1, methods);
  33. assertTrue("Couldn't find 'toArray' in the set of methods? ", i != -1);
  34. // String expectedSignature = "java.lang.Object[] java.util.Collection.toArray(java.lang.Object[])";
  35. String expectedSignature = "([Ljava/lang/Object;)[Ljava/lang/Object;";
  36. assertTrue("Expected signature of '" + expectedSignature + "' but it was '" + methods[i].getSignatureErased(), methods[i]
  37. .getSignatureErased().equals(expectedSignature));
  38. }
  39. /**
  40. * Can we resolve the dreaded Enum type...
  41. */
  42. public void testResolveEnum() {
  43. ResolvedType enumType = world.resolve("java.lang.Enum");
  44. assertTrue("Should be the raw type but is " + enumType.typeKind, enumType.isRawType());
  45. ResolvedType theGenericEnumType = enumType.getGenericType();
  46. assertTrue("Should have a type variable ", theGenericEnumType.getTypeVariables().length > 0);
  47. TypeVariable tv = theGenericEnumType.getTypeVariables()[0];
  48. String expected = "TypeVar E extends java.lang.Enum<E>";
  49. assertTrue("Type variable should be '" + expected + "' but is '" + tv + "'", tv.toString().equals(expected));
  50. }
  51. public void testResolveClass() {
  52. world.resolve("java.lang.Class").getGenericType();
  53. }
  54. public void testGenericInterfaceSuperclass_ReflectionWorldResolution() {
  55. UnresolvedType javaUtilMap = UnresolvedType.forName("java.util.Map");
  56. ReferenceType rawType = (ReferenceType) world.resolve(javaUtilMap);
  57. assertTrue("Should be the raw type ?!? " + rawType.getTypekind(), rawType.isRawType());
  58. ReferenceType genericType = (ReferenceType) rawType.getGenericType();
  59. assertTrue("Should be the generic type ?!? " + genericType.getTypekind(), genericType.isGenericType());
  60. ResolvedType rt = rawType.getSuperclass();
  61. assertTrue("Superclass for Map raw type should be Object but was " + rt, rt.equals(UnresolvedType.OBJECT));
  62. ResolvedType rt2 = genericType.getSuperclass();
  63. assertTrue("Superclass for Map generic type should be Object but was " + rt2, rt2.equals(UnresolvedType.OBJECT));
  64. }
  65. /**
  66. * This is testing the optimization in the reflective annotation finder to verify that if you only want runtime
  67. * annotation info then we use reflection and don't go digging through the classfile bytes.
  68. */
  69. public void testAnnotationFinderClassRetention() throws Exception {
  70. ResolvedType type = world.resolve(AnnoTesting.class.getName());
  71. ResolvedMember[] ms = type.getDeclaredMethods();
  72. int findMethod = findMethod("a", ms);
  73. ResolvedMember methodWithOnlyClassLevelAnnotation = ms[findMethod("a", ms)];
  74. ResolvedMember methodWithOnlyRuntimeLevelAnnotation = ms[findMethod("b", ms)];
  75. ResolvedMember methodWithClassAndRuntimeLevelAnnotations = ms[findMethod("c", ms)];
  76. ResolvedMember methodWithClassAndRuntimeLevelAnnotations2 = ms[findMethod("d", ms)];
  77. assertTrue(methodWithOnlyClassLevelAnnotation.hasAnnotation(world.resolve(AnnoClass.class.getName())));
  78. assertTrue(methodWithOnlyRuntimeLevelAnnotation.hasAnnotation(world.resolve(AnnoRuntime.class.getName())));
  79. // This is the tricky scenario.
  80. // When asking about the runtime level annotations it should not go digging into bcel
  81. assertTrue(methodWithClassAndRuntimeLevelAnnotations.hasAnnotation(world.resolve(AnnoRuntime.class.getName())));
  82. Field annotationsField = ResolvedMemberImpl.class.getDeclaredField("annotationTypes");
  83. annotationsField.setAccessible(true);
  84. ResolvedType[] annoTypes = (ResolvedType[])annotationsField.get(methodWithClassAndRuntimeLevelAnnotations);
  85. // Should only be the runtime one here
  86. assertEquals(1, annoTypes.length);
  87. // But when you do ask again and this time for class level, it should redo the unpack and pull both runtime and class out
  88. assertTrue(methodWithClassAndRuntimeLevelAnnotations.hasAnnotation(world.resolve(AnnoClass.class.getName())));
  89. annotationsField.setAccessible(true);
  90. annoTypes = (ResolvedType[])annotationsField.get(methodWithClassAndRuntimeLevelAnnotations);
  91. // Now both should be there
  92. assertEquals(2, annoTypes.length);
  93. assertTrue(methodWithClassAndRuntimeLevelAnnotations2.hasAnnotation(world.resolve(AnnoRuntime.class.getName())));
  94. // now ask for 'all annotations' via another route, this should reunpack and get them all
  95. ResolvedType[] annotations = methodWithClassAndRuntimeLevelAnnotations2.getAnnotationTypes();
  96. assertEquals(2,annotations.length);
  97. }
  98. @Retention(RetentionPolicy.CLASS)
  99. @interface AnnoClass {}
  100. @Retention(RetentionPolicy.RUNTIME)
  101. @interface AnnoRuntime {}
  102. class AnnoTesting {
  103. @AnnoClass
  104. public void a() {}
  105. @AnnoRuntime
  106. public void b() {}
  107. @AnnoClass @AnnoRuntime
  108. public void c() {}
  109. @AnnoClass @AnnoRuntime
  110. public void d() {}
  111. }
  112. }