From ae085e2a79e3d8a71b03f422db8292d1007e04c1 Mon Sep 17 00:00:00 2001 From: aclement Date: Wed, 8 Dec 2004 14:34:33 +0000 Subject: [PATCH] Annotation matching. --- .../compiler/lookup/EclipseSourceType.java | 2 +- .../ajc150/AllTestsJava5_binaryWeaving.java | 1 + .../ajc150/AnnotationPointcuts.java | 42 ++++++++++--------- .../org/aspectj/weaver/AnnotatedElement.java | 2 +- weaver/src/org/aspectj/weaver/Member.java | 2 +- .../src/org/aspectj/weaver/ResolvedTypeX.java | 10 ++--- weaver/src/org/aspectj/weaver/TypeX.java | 2 +- .../org/aspectj/weaver/bcel/BcelField.java | 2 +- .../org/aspectj/weaver/bcel/BcelMethod.java | 2 +- .../aspectj/weaver/bcel/BcelObjectType.java | 2 +- .../patterns/ExactAnnotationTypePattern.java | 5 +-- .../patterns/AnnotationPatternTestCase.java | 2 +- .../weaver/patterns/ParserTestCase.java | 10 +++-- 13 files changed, 44 insertions(+), 40 deletions(-) diff --git a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceType.java b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceType.java index 2efe987b8..7a5207324 100644 --- a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceType.java +++ b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceType.java @@ -221,7 +221,7 @@ public class EclipseSourceType extends ResolvedTypeX.ConcreteName { return (binding.getAccessFlags() & ACC_ANNOTATION)!=0; } - public boolean hasAnnotation(ResolvedTypeX ofType) { + public boolean hasAnnotation(TypeX ofType) { throw new RuntimeException("How to implement this? Needs to ask eclipse!"); } diff --git a/tests/src/org/aspectj/systemtest/ajc150/AllTestsJava5_binaryWeaving.java b/tests/src/org/aspectj/systemtest/ajc150/AllTestsJava5_binaryWeaving.java index ec734bd87..f03ceef4f 100644 --- a/tests/src/org/aspectj/systemtest/ajc150/AllTestsJava5_binaryWeaving.java +++ b/tests/src/org/aspectj/systemtest/ajc150/AllTestsJava5_binaryWeaving.java @@ -29,6 +29,7 @@ public class AllTestsJava5_binaryWeaving { suite.addTestSuite(CovarianceTests.class); suite.addTestSuite(Enums.class); suite.addTestSuite(Annotations.class); + suite.addTestSuite(AnnotationPointcuts.class); suite.addTestSuite(Varargs.class); //$JUnit-END$ return suite; diff --git a/tests/src/org/aspectj/systemtest/ajc150/AnnotationPointcuts.java b/tests/src/org/aspectj/systemtest/ajc150/AnnotationPointcuts.java index dba69c367..58b41528e 100644 --- a/tests/src/org/aspectj/systemtest/ajc150/AnnotationPointcuts.java +++ b/tests/src/org/aspectj/systemtest/ajc150/AnnotationPointcuts.java @@ -27,24 +27,28 @@ public class AnnotationPointcuts extends TestUtils { } // before(): call(@SimpleAnnotation * *(..)) { } -// public void test001_usingAnnotationsInPointcuts() { -// CompilationResult cR = binaryWeave("testcode.jar","AnnotationAspect02.aj",0,0); -// System.err.println(cR.getStandardError()); -// System.err.println(cR.getErrorMessages()); -// System.err.println(cR.getInfoMessages()); + public void test001_usingAnnotationsInPointcuts() { + CompilationResult cR = binaryWeave("testcode.jar","AnnotationAspect02.aj",0,0); + System.err.println(cR.getStandardError()); + System.err.println(cR.getErrorMessages()); + System.err.println(cR.getInfoMessages()); + verifyWeavingMessagesOutput(cR,new String[]{ +"weaveinfo Type 'AnnotatedType' (AnnotatedType.java:3) advised by before advice from 'AnnotationAspect02' (AnnotationAspect02.aj:4)", +"weaveinfo Type 'AnnotatedType' (AnnotatedType.java:3) advised by before advice from 'AnnotationAspect02' (AnnotationAspect02.aj:2)", +"weaveinfo Type 'AnnotatedType' (AnnotatedType.java:4) advised by before advice from 'AnnotationAspect02' (AnnotationAspect02.aj:4)"}); + +// assertTrue("Expected three message about ITDs not allowed on Annotations but got: #"+ +// cR.getErrorMessages().size()+": \n"+cR.getErrorMessages(), +// cR.getErrorMessages().size()==3); +// IMessage msg1_ctor = (IMessage)cR.getErrorMessages().get(0); +// IMessage msg2_method = (IMessage)cR.getErrorMessages().get(1); +// IMessage msg3_field = (IMessage)cR.getErrorMessages().get(2); +// assertTrue("Expected message about ITDCs on annotations not allowed, but got: \n"+msg1_ctor, +// msg1_ctor.toString().indexOf("can't make inter-type constructor declarations")!=-1); +// assertTrue("Expected message about ITDMs on annotations not allowed, but got: \n"+msg2_method, +// msg2_method.toString().indexOf("can't make inter-type method declarations")!=-1); +// assertTrue("Expected message about ITDFs on annotations not allowed, but got: \n"+msg3_field, +// msg3_field.toString().indexOf("can't make inter-type field declarations")!=-1); // verifyWeavingMessagesOutput(cR,new String[]{}); -//// assertTrue("Expected three message about ITDs not allowed on Annotations but got: #"+ -//// cR.getErrorMessages().size()+": \n"+cR.getErrorMessages(), -//// cR.getErrorMessages().size()==3); -//// IMessage msg1_ctor = (IMessage)cR.getErrorMessages().get(0); -//// IMessage msg2_method = (IMessage)cR.getErrorMessages().get(1); -//// IMessage msg3_field = (IMessage)cR.getErrorMessages().get(2); -//// assertTrue("Expected message about ITDCs on annotations not allowed, but got: \n"+msg1_ctor, -//// msg1_ctor.toString().indexOf("can't make inter-type constructor declarations")!=-1); -//// assertTrue("Expected message about ITDMs on annotations not allowed, but got: \n"+msg2_method, -//// msg2_method.toString().indexOf("can't make inter-type method declarations")!=-1); -//// assertTrue("Expected message about ITDFs on annotations not allowed, but got: \n"+msg3_field, -//// msg3_field.toString().indexOf("can't make inter-type field declarations")!=-1); -//// verifyWeavingMessagesOutput(cR,new String[]{}); -// } + } } \ No newline at end of file diff --git a/weaver/src/org/aspectj/weaver/AnnotatedElement.java b/weaver/src/org/aspectj/weaver/AnnotatedElement.java index f83e551d0..445fda6b7 100644 --- a/weaver/src/org/aspectj/weaver/AnnotatedElement.java +++ b/weaver/src/org/aspectj/weaver/AnnotatedElement.java @@ -13,7 +13,7 @@ package org.aspectj.weaver; * Represents any element that may have annotations */ public interface AnnotatedElement { - boolean hasAnnotation(ResolvedTypeX ofType); + boolean hasAnnotation(TypeX ofType); ResolvedTypeX[] getAnnotationTypes(); // SomeType getAnnotation(TypeX ofType); diff --git a/weaver/src/org/aspectj/weaver/Member.java b/weaver/src/org/aspectj/weaver/Member.java index 9bd4fa7d4..eaf0c9217 100644 --- a/weaver/src/org/aspectj/weaver/Member.java +++ b/weaver/src/org/aspectj/weaver/Member.java @@ -468,7 +468,7 @@ public class Member implements Comparable, AnnotatedElement { * If you want a sensible answer, resolve the member and call * hasAnnotation() on the ResolvedMember. */ - public boolean hasAnnotation(ResolvedTypeX ofType) { + public boolean hasAnnotation(TypeX ofType) { throw new UnsupportedOperationException("You should resolve this member and call hasAnnotation() on the result..."); } diff --git a/weaver/src/org/aspectj/weaver/ResolvedTypeX.java b/weaver/src/org/aspectj/weaver/ResolvedTypeX.java index 413292468..7b9a60174 100644 --- a/weaver/src/org/aspectj/weaver/ResolvedTypeX.java +++ b/weaver/src/org/aspectj/weaver/ResolvedTypeX.java @@ -560,7 +560,7 @@ public abstract class ResolvedTypeX extends TypeX implements AnnotatedElement { return delegate.isClass(); } - public boolean hasAnnotation(ResolvedTypeX ofType) { + public boolean hasAnnotation(TypeX ofType) { return delegate.hasAnnotation(ofType); } @@ -725,7 +725,7 @@ public abstract class ResolvedTypeX extends TypeX implements AnnotatedElement { public abstract boolean isEnum(); public abstract boolean isAnnotation(); - public abstract boolean hasAnnotation(ResolvedTypeX ofType); + public abstract boolean hasAnnotation(TypeX ofType); public abstract ResolvedTypeX[] getAnnotationTypes(); public abstract ResolvedMember[] getDeclaredFields(); @@ -801,7 +801,7 @@ public abstract class ResolvedTypeX extends TypeX implements AnnotatedElement { return ResolvedMember.NONE; } - public boolean hasAnnotation(ResolvedTypeX ofType) { + public boolean hasAnnotation(TypeX ofType) { return false; } @@ -864,7 +864,7 @@ public abstract class ResolvedTypeX extends TypeX implements AnnotatedElement { public final boolean isPrimitive() { return true; } - public boolean hasAnnotation(ResolvedTypeX ofType) { + public boolean hasAnnotation(TypeX ofType) { return false; } public final boolean isAssignableFrom(TypeX other) { @@ -941,7 +941,7 @@ public abstract class ResolvedTypeX extends TypeX implements AnnotatedElement { public final String getName() { return MISSING_NAME; } - public boolean hasAnnotation(ResolvedTypeX ofType) { + public boolean hasAnnotation(TypeX ofType) { return false; } public final ResolvedMember[] getDeclaredFields() { diff --git a/weaver/src/org/aspectj/weaver/TypeX.java b/weaver/src/org/aspectj/weaver/TypeX.java index 020682322..2cc7528f3 100644 --- a/weaver/src/org/aspectj/weaver/TypeX.java +++ b/weaver/src/org/aspectj/weaver/TypeX.java @@ -552,7 +552,7 @@ public class TypeX implements AnnotatedElement { return world.resolve(this); } - public boolean hasAnnotation(ResolvedTypeX ofType) { + public boolean hasAnnotation(TypeX ofType) { throw new UnsupportedOperationException("You should resolve this member and call hasAnnotation() on the result..."); } diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelField.java b/weaver/src/org/aspectj/weaver/bcel/BcelField.java index 57fe60b59..230513585 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelField.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelField.java @@ -79,7 +79,7 @@ final class BcelField extends ResolvedMember { return isSynthetic; } - public boolean hasAnnotation(ResolvedTypeX ofType) { + public boolean hasAnnotation(TypeX ofType) { Annotation[] anns = field.getAnnotations(); for (int i = 0; i < anns.length; i++) { Annotation annotation = anns[i]; diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelMethod.java b/weaver/src/org/aspectj/weaver/bcel/BcelMethod.java index 360295c92..006f9d6a0 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelMethod.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelMethod.java @@ -141,7 +141,7 @@ final class BcelMethod extends ResolvedMember { } } - public boolean hasAnnotation(ResolvedTypeX ofType) { + public boolean hasAnnotation(TypeX ofType) { Annotation[] anns = method.getAnnotations(); for (int i = 0; i < anns.length; i++) { Annotation annotation = anns[i]; diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelObjectType.java b/weaver/src/org/aspectj/weaver/bcel/BcelObjectType.java index 321480ddc..ce8a253b2 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelObjectType.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelObjectType.java @@ -323,7 +323,7 @@ public class BcelObjectType extends ResolvedTypeX.ConcreteName { - public boolean hasAnnotation(ResolvedTypeX ofType) { + public boolean hasAnnotation(TypeX ofType) { Annotation[] annotationsOnThisType = javaClass.getAnnotations(); for (int i = 0; i < annotationsOnThisType.length; i++) { Annotation a = annotationsOnThisType[i]; diff --git a/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java b/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java index 8017ec757..d1ff327d1 100644 --- a/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java +++ b/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java @@ -49,10 +49,7 @@ public class ExactAnnotationTypePattern extends AnnotationTypePattern { } public FuzzyBoolean matches(AnnotatedElement annotated) { - if (!resolved) { - throw new IllegalStateException("Can't match on an unresolved annotation type pattern"); - } - return (annotated.hasAnnotation((ResolvedTypeX)annotationType) ? + return (annotated.hasAnnotation(annotationType) ? FuzzyBoolean.YES : FuzzyBoolean.NO); } diff --git a/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java b/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java index 52b6b98a6..c3ea08e4d 100644 --- a/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java +++ b/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java @@ -327,7 +327,7 @@ public class AnnotationPatternTestCase extends TestCase { this.annotationTypes = annotationTypes; } - public boolean hasAnnotation(ResolvedTypeX ofType) { + public boolean hasAnnotation(TypeX ofType) { for (int i = 0; i < annotationTypes.length; i++) { if (annotationTypes[i].equals(ofType.getName())) return true; } diff --git a/weaver/testsrc/org/aspectj/weaver/patterns/ParserTestCase.java b/weaver/testsrc/org/aspectj/weaver/patterns/ParserTestCase.java index bf1471667..1be581690 100644 --- a/weaver/testsrc/org/aspectj/weaver/patterns/ParserTestCase.java +++ b/weaver/testsrc/org/aspectj/weaver/patterns/ParserTestCase.java @@ -63,18 +63,20 @@ public class ParserTestCase extends TestCase { } public void testParseWithAnnotation() { - PatternParser parser = new PatternParser("execution(@p.SimpleAnnotation void Hello.*(..))"); + PatternParser parser = new PatternParser("execution(@SimpleAnnotation void Hello.*(..))"); KindedPointcut p = (KindedPointcut) parser.parsePointcut(); // XXX - needs finishing... - // p.resolveBindings(makeSimpleScope(),new Bindings(3)); -// System.err.println(p); + p.resolveBindings(makeSimpleScope(),new Bindings(3)); + System.err.println(p); // assertEquals(p.kind, BcelShadow.MethodExecution); // assertTrue(p.signature.getName().matches("foobar")); // p.signature.resolveBindings(makeSimpleScope(),new Bindings(3)); } public TestScope makeSimpleScope() { - return new TestScope(new String[] {"int", "java.lang.String"}, new String[] {"a", "b"}, world); + TestScope s = new TestScope(new String[] {"int", "java.lang.String"}, new String[] {"a", "b"}, world); + s.setImportedPrefixes(new String[]{"p."}); + return s; } } -- 2.39.5