diff options
author | Andy Clement <aclement@pivotal.io> | 2017-09-28 16:05:13 -0700 |
---|---|---|
committer | Andy Clement <aclement@pivotal.io> | 2017-10-20 13:09:48 -0700 |
commit | d2659f6c5823e321fec4b6021c2524f1c259527a (patch) | |
tree | 6e12d5a3fa43b2bcf9d2c8616d7eb4b164eb8d57 /weaver5 | |
parent | 3d85b130f5409e259795f2e685d17d9ff1604a22 (diff) | |
download | aspectj-d2659f6c5823e321fec4b6021c2524f1c259527a.tar.gz aspectj-d2659f6c5823e321fec4b6021c2524f1c259527a.zip |
Tests for smart annotation unpacking for reflection types
Diffstat (limited to 'weaver5')
-rw-r--r-- | weaver5/java5-testsrc/org/aspectj/weaver/TestJava5ReflectionBasedReferenceTypeDelegate.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/TestJava5ReflectionBasedReferenceTypeDelegate.java b/weaver5/java5-testsrc/org/aspectj/weaver/TestJava5ReflectionBasedReferenceTypeDelegate.java index 3eb59cad6..9474d63cd 100644 --- a/weaver5/java5-testsrc/org/aspectj/weaver/TestJava5ReflectionBasedReferenceTypeDelegate.java +++ b/weaver5/java5-testsrc/org/aspectj/weaver/TestJava5ReflectionBasedReferenceTypeDelegate.java @@ -15,6 +15,10 @@ package org.aspectj.weaver; import junit.framework.Test; import junit.framework.TestSuite; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.reflect.Field; + import org.aspectj.weaver.reflect.ReflectionBasedReferenceTypeDelegateTest; public class TestJava5ReflectionBasedReferenceTypeDelegate extends ReflectionBasedReferenceTypeDelegateTest { @@ -73,5 +77,72 @@ public class TestJava5ReflectionBasedReferenceTypeDelegate extends ReflectionBas ResolvedType rt2 = genericType.getSuperclass(); assertTrue("Superclass for Map generic type should be Object but was " + rt2, rt2.equals(UnresolvedType.OBJECT)); } + + + /** + * This is testing the optimization in the reflective annotation finder to verify that if you only want runtime + * annotation info then we use reflection and don't go digging through the classfile bytes. + */ + public void testAnnotationFinderClassRetention() throws Exception { + ResolvedType type = world.resolve(AnnoTesting.class.getName()); + ResolvedMember[] ms = type.getDeclaredMethods(); + int findMethod = findMethod("a", ms); + + ResolvedMember methodWithOnlyClassLevelAnnotation = ms[findMethod("a", ms)]; + ResolvedMember methodWithOnlyRuntimeLevelAnnotation = ms[findMethod("b", ms)]; + ResolvedMember methodWithClassAndRuntimeLevelAnnotations = ms[findMethod("c", ms)]; + ResolvedMember methodWithClassAndRuntimeLevelAnnotations2 = ms[findMethod("d", ms)]; + + assertTrue(methodWithOnlyClassLevelAnnotation.hasAnnotation(world.resolve(AnnoClass.class.getName()))); + assertTrue(methodWithOnlyRuntimeLevelAnnotation.hasAnnotation(world.resolve(AnnoRuntime.class.getName()))); + + // This is the tricky scenario. + + // When asking about the runtime level annotations it should not go digging into bcel + assertTrue(methodWithClassAndRuntimeLevelAnnotations.hasAnnotation(world.resolve(AnnoRuntime.class.getName()))); + + Field annotationsField = ResolvedMemberImpl.class.getDeclaredField("annotationTypes"); + annotationsField.setAccessible(true); + ResolvedType[] annoTypes = (ResolvedType[])annotationsField.get(methodWithClassAndRuntimeLevelAnnotations); + + // Should only be the runtime one here + assertEquals(1, annoTypes.length); + + // But when you do ask again and this time for class level, it should redo the unpack and pull both runtime and class out + assertTrue(methodWithClassAndRuntimeLevelAnnotations.hasAnnotation(world.resolve(AnnoClass.class.getName()))); + + annotationsField.setAccessible(true); + annoTypes = (ResolvedType[])annotationsField.get(methodWithClassAndRuntimeLevelAnnotations); + + // Now both should be there + assertEquals(2, annoTypes.length); + + assertTrue(methodWithClassAndRuntimeLevelAnnotations2.hasAnnotation(world.resolve(AnnoRuntime.class.getName()))); + // now ask for 'all annotations' via another route, this should reunpack and get them all + ResolvedType[] annotations = methodWithClassAndRuntimeLevelAnnotations2.getAnnotationTypes(); + assertEquals(2,annotations.length); + } + + @Retention(RetentionPolicy.CLASS) + @interface AnnoClass {} + + @Retention(RetentionPolicy.RUNTIME) + @interface AnnoRuntime {} + + class AnnoTesting { + + @AnnoClass + public void a() {} + + @AnnoRuntime + public void b() {} + + @AnnoClass @AnnoRuntime + public void c() {} + + @AnnoClass @AnnoRuntime + public void d() {} + + } }
\ No newline at end of file |