diff options
author | aclement <aclement> | 2008-02-25 21:46:11 +0000 |
---|---|---|
committer | aclement <aclement> | 2008-02-25 21:46:11 +0000 |
commit | fdb16f70ce79ff3ac1d62bcc5021e24f21756291 (patch) | |
tree | 0fb2aa53a5fd897d20bcc9b3fe2a90a6e4c2175e /weaver5 | |
parent | aff8542d4f5e6b386dd0a216b4cf9adf5d413212 (diff) | |
download | aspectj-fdb16f70ce79ff3ac1d62bcc5021e24f21756291.tar.gz aspectj-fdb16f70ce79ff3ac1d62bcc5021e24f21756291.zip |
annoValMatch: implementation
Diffstat (limited to 'weaver5')
-rw-r--r-- | weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java | 68 | ||||
-rw-r--r-- | weaver5/java5-testsrc/org/aspectj/weaver/tools/Java15PointcutExpressionTest.java | 41 |
2 files changed, 109 insertions, 0 deletions
diff --git a/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java b/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java index cecf1c461..ed12bbed0 100644 --- a/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java +++ b/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java @@ -21,11 +21,14 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; +import org.aspectj.apache.bcel.classfile.AnnotationDefault; +import org.aspectj.apache.bcel.classfile.Attribute; import org.aspectj.apache.bcel.classfile.JavaClass; import org.aspectj.apache.bcel.classfile.LocalVariable; import org.aspectj.apache.bcel.classfile.LocalVariableTable; import org.aspectj.apache.bcel.util.NonCachingClassLoaderRepository; import org.aspectj.apache.bcel.util.Repository; +import org.aspectj.weaver.AnnotationX; import org.aspectj.weaver.ResolvedType; import org.aspectj.weaver.UnresolvedType; import org.aspectj.weaver.World; @@ -96,6 +99,71 @@ public class Java15AnnotationFinder implements AnnotationFinder, ArgNameFinder { return null; } + public AnnotationX getAnnotationOfType(UnresolvedType ofType,Member onMember) { + if (!(onMember instanceof AccessibleObject)) return null; + // here we really want both the runtime visible AND the class visible annotations + // so we bail out to Bcel and then chuck away the JavaClass so that we don't hog + // memory. + try { + JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass()); + org.aspectj.apache.bcel.classfile.annotation.Annotation[] anns = new org.aspectj.apache.bcel.classfile.annotation.Annotation[0]; + if (onMember instanceof Method) { + org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method)onMember); + if (bcelMethod == null) { + System.err.println("Unexpected problem in Java15AnnotationFinder: cannot retrieve annotations on method '"+onMember.getName()+"' in class '"+jc.getClassName()+"'"); + } else { + anns = bcelMethod.getAnnotations(); + } + } else if (onMember instanceof Constructor) { + org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor)onMember); + anns = bcelCons.getAnnotations(); + } else if (onMember instanceof Field) { + org.aspectj.apache.bcel.classfile.Field bcelField = jc.getField((Field)onMember); + anns = bcelField.getAnnotations(); + } + // the answer is cached and we don't want to hold on to memory + bcelRepository.clear(); + if (anns == null) anns = new org.aspectj.apache.bcel.classfile.annotation.Annotation[0]; + // convert to our Annotation type + for (int i=0;i<anns.length;i++) { + if (anns[i].getTypeSignature().equals(ofType.getSignature())) { + return new AnnotationX(anns[i],world); + } + } + return null; + } catch (ClassNotFoundException cnfEx) { + // just use reflection then + } + + return null; + } + + public String getAnnotationDefaultValue(Member onMember) { + try { + JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass()); + if (onMember instanceof Method) { + org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method)onMember); + if (bcelMethod == null) { + System.err.println("Unexpected problem in Java15AnnotationFinder: cannot retrieve annotations on method '"+onMember.getName()+"' in class '"+jc.getClassName()+"'"); + } else { + Attribute[] attrs = bcelMethod.getAttributes(); + for (int i = 0; i < attrs.length; i++) { + Attribute attribute = attrs[i]; + if (attribute.getName().equals("AnnotationDefault")) { + AnnotationDefault def = (AnnotationDefault)attribute; + return def.getElementValue().stringifyValue(); + } + } + return null; + } + } + } catch (ClassNotFoundException cnfEx) { + // just use reflection then + } + + return null; + } + public Set getAnnotations(Member onMember) { if (!(onMember instanceof AccessibleObject)) return Collections.EMPTY_SET; // here we really want both the runtime visible AND the class visible annotations diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/tools/Java15PointcutExpressionTest.java b/weaver5/java5-testsrc/org/aspectj/weaver/tools/Java15PointcutExpressionTest.java index 98da1b2b5..fa1e1d17d 100644 --- a/weaver5/java5-testsrc/org/aspectj/weaver/tools/Java15PointcutExpressionTest.java +++ b/weaver5/java5-testsrc/org/aspectj/weaver/tools/Java15PointcutExpressionTest.java @@ -100,6 +100,47 @@ public class Java15PointcutExpressionTest extends TestCase { } + public void testMatchingAnnotationValueExpressions() throws SecurityException, NoSuchMethodException { + PointcutParser p = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this.getClass().getClassLoader()); + PointcutExpression pexpr = null; + ShadowMatch match = null; + + Method n = test.AnnoValues.class.getMethod("none",null); + Method r = test.AnnoValues.class.getMethod("redMethod",null); + Method g = test.AnnoValues.class.getMethod("greenMethod",null); + Method b = test.AnnoValues.class.getMethod("blueMethod",null); + Method d = test.AnnoValues.class.getMethod("defaultMethod",null); + + pexpr = p.parsePointcutExpression("execution(@test.A3(test.Color.RED) public void *(..))"); + assertTrue("Should match", pexpr.matchesMethodExecution(n).neverMatches()); // default value RED + assertTrue("Should match", pexpr.matchesMethodExecution(r).alwaysMatches()); + assertTrue("Should not match", pexpr.matchesMethodExecution(g).neverMatches()); + assertTrue("Should not match", pexpr.matchesMethodExecution(b).neverMatches()); + assertTrue("Should match", pexpr.matchesMethodExecution(d).alwaysMatches()); + + pexpr = p.parsePointcutExpression("execution(@test.A3(test.Color.GREEN) public void *(..))"); + assertTrue("Should not match", pexpr.matchesMethodExecution(n).neverMatches()); // default value RED + assertTrue("Should not match", pexpr.matchesMethodExecution(r).neverMatches()); + assertTrue("Should match", pexpr.matchesMethodExecution(g).alwaysMatches()); + assertTrue("Should not match", pexpr.matchesMethodExecution(b).neverMatches()); + assertTrue("Should not match", pexpr.matchesMethodExecution(d).neverMatches()); + + pexpr = p.parsePointcutExpression("execution(@test.A3(test.Color.BLUE) public void *(..))"); + assertTrue("Should not match", pexpr.matchesMethodExecution(n).neverMatches()); // default value RED + assertTrue("Should not match", pexpr.matchesMethodExecution(r).neverMatches()); + assertTrue("Should not match", pexpr.matchesMethodExecution(g).neverMatches()); + assertTrue("Should match", pexpr.matchesMethodExecution(b).alwaysMatches()); + assertTrue("Should not match", pexpr.matchesMethodExecution(d).neverMatches()); + + pexpr = p.parsePointcutExpression("execution(@test.A3 public void *(..))"); + assertTrue("Should match", pexpr.matchesMethodExecution(n).neverMatches()); // default value RED + assertTrue("Should match", pexpr.matchesMethodExecution(r).alwaysMatches()); + assertTrue("Should match", pexpr.matchesMethodExecution(g).alwaysMatches()); + assertTrue("Should match", pexpr.matchesMethodExecution(b).alwaysMatches()); + assertTrue("Should match", pexpr.matchesMethodExecution(d).alwaysMatches()); + + } + /** * Test matching of pointcuts against expressions. A reflection world is being used on the backend here (not a Bcel one). |