From 6f6065383cf4909057f16b22e389a38871589c64 Mon Sep 17 00:00:00 2001 From: acolyer Date: Tue, 7 Dec 2004 10:34:37 +0000 Subject: [PATCH] more parser test cases and updates --- .../patterns/ExactAnnotationTypePattern.java | 22 ++-- .../weaver/patterns/SignaturePattern.java | 7 +- .../patterns/AnnotationPatternTestCase.java | 119 +++++++++++++++--- 3 files changed, 118 insertions(+), 30 deletions(-) diff --git a/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java b/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java index f60c67df3..7916fc98d 100644 --- a/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java +++ b/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java @@ -27,18 +27,21 @@ public class ExactAnnotationTypePattern extends AnnotationTypePattern { protected TypeX annotationType; protected String formalName; - private boolean resolved = true; + private boolean resolved = false; + private boolean bindingPattern = false; /** * */ public ExactAnnotationTypePattern(TypeX annotationType) { this.annotationType = annotationType; + this.resolved = false; } public ExactAnnotationTypePattern(String formalName) { this.formalName = formalName; this.resolved = false; + this.bindingPattern = true; // will be turned into BindingAnnotationTypePattern during resolution } @@ -78,6 +81,7 @@ public class ExactAnnotationTypePattern extends AnnotationTypePattern { return this; } } else { + annotationType = annotationType.resolve(scope.getWorld()); return this; } } @@ -89,11 +93,11 @@ public class ExactAnnotationTypePattern extends AnnotationTypePattern { public void write(DataOutputStream s) throws IOException { s.writeByte(AnnotationTypePattern.EXACT); s.writeByte(VERSION); - s.writeBoolean(resolved); - if (resolved) { - annotationType.write(s); - } else { + s.writeBoolean(bindingPattern); + if (bindingPattern) { s.writeUTF(formalName); + } else { + annotationType.write(s); } writeLocation(s); } @@ -104,11 +108,11 @@ public class ExactAnnotationTypePattern extends AnnotationTypePattern { if (version > VERSION) { throw new BCException("ExactAnnotationTypePattern was written by a newer version of AspectJ"); } - boolean isResolved = s.readBoolean(); - if (isResolved) { - ret = new ExactAnnotationTypePattern(TypeX.read(s)); - } else { + boolean isBindingPattern = s.readBoolean(); + if (isBindingPattern) { ret = new ExactAnnotationTypePattern(s.readUTF()); + } else { + ret = new ExactAnnotationTypePattern(TypeX.read(s)); } ret.readLocation(context,s); return ret; diff --git a/weaver/src/org/aspectj/weaver/patterns/SignaturePattern.java b/weaver/src/org/aspectj/weaver/patterns/SignaturePattern.java index eda12bada..1bdd48820 100644 --- a/weaver/src/org/aspectj/weaver/patterns/SignaturePattern.java +++ b/weaver/src/org/aspectj/weaver/patterns/SignaturePattern.java @@ -424,8 +424,11 @@ public class SignaturePattern extends PatternNode { StringBuffer buf = new StringBuffer(); if (annotationPattern != AnnotationTypePattern.ANY) { - buf.append(annotationPattern.toString()); - buf.append(' '); + if (! (annotationPattern instanceof ExactAnnotationTypePattern )) { + buf.append('@'); + } + buf.append(annotationPattern.toString()); + buf.append(' '); } if (modifiers != ModifiersPattern.ANY) { diff --git a/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java b/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java index 73eab7c5c..e11415c80 100644 --- a/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java +++ b/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java @@ -11,6 +11,7 @@ package org.aspectj.weaver.patterns; import org.aspectj.weaver.AnnotatedElement; import org.aspectj.weaver.TypeX; +import org.aspectj.weaver.bcel.BcelWorld; import junit.framework.TestCase; @@ -148,7 +149,7 @@ public class AnnotationPatternTestCase extends TestCase { } public void testMaybeParseAnnotationPattern() { - PatternParser p = new PatternParser("@Foo && @Boo"); + PatternParser p = new PatternParser("@Foo"); AnnotationTypePattern a = p.maybeParseAnnotationPattern(); assertNotNull("Should find annotation pattern",a); p = new PatternParser("Foo && Boo"); @@ -184,66 +185,146 @@ public class AnnotationPatternTestCase extends TestCase { } public void testParseMethodOrConstructorSigNoAP() { - + PatternParser p = new PatternParser("* *.*(..)"); + SignaturePattern s = p.parseMethodOrConstructorSignaturePattern(); + assertEquals("Any annotation",AnnotationTypePattern.ANY,s.getAnnotationPattern()); + assertEquals("Any return","*",s.getReturnType().toString()); + assertEquals("Any dec type","*",s.getDeclaringType().toString()); + assertEquals("Any name","*",s.getName().toString()); + assertEquals("* *.*(..)",s.toString()); } public void testParseMethodOrConstructorSigSimpleAP() { - + PatternParser p = new PatternParser("@Foo * *.*(..)"); + SignaturePattern s = p.parseMethodOrConstructorSignaturePattern(); + assertEquals("Exact annotation","@Foo",((ExactAnnotationTypePattern)s.getAnnotationPattern()).toString()); + assertEquals("Any return","*",s.getReturnType().toString()); + assertEquals("Any dec type","*",s.getDeclaringType().toString()); + assertEquals("Any name","*",s.getName().toString()); + assertEquals("@Foo * *.*(..)",s.toString()); } public void testParseMethodOrConstructorSigComplexAP() { - + PatternParser p = new PatternParser("@(!@Foo || @Goo) * *.*(..)"); + SignaturePattern s = p.parseMethodOrConstructorSignaturePattern(); + assertEquals("complex annotation","((!@Foo) || @Goo)",s.getAnnotationPattern().toString()); + assertEquals("Any return","*",s.getReturnType().toString()); + assertEquals("Any dec type","*",s.getDeclaringType().toString()); + assertEquals("Any name","*",s.getName().toString()); + assertEquals("@((!@Foo) || @Goo) * *.*(..)",s.toString()); } public void testParseMethodFieldSigNoAP() { - + PatternParser p = new PatternParser("* *.*"); + SignaturePattern s = p.parseFieldSignaturePattern(); + assertEquals("Any annotation",AnnotationTypePattern.ANY,s.getAnnotationPattern()); + assertEquals("Any field type","*",s.getReturnType().toString()); + assertEquals("Any dec type","*",s.getDeclaringType().toString()); + assertEquals("Any name","*",s.getName().toString()); + assertEquals("* *.*",s.toString()); } public void testParseFieldSigSimpleAP() { - + PatternParser p = new PatternParser("@Foo * *.*"); + SignaturePattern s = p.parseFieldSignaturePattern(); + assertEquals("Exact annotation","@Foo",((ExactAnnotationTypePattern)s.getAnnotationPattern()).toString()); + assertEquals("Any field type","*",s.getReturnType().toString()); + assertEquals("Any dec type","*",s.getDeclaringType().toString()); + assertEquals("Any name","*",s.getName().toString()); + assertEquals("@Foo * *.*",s.toString()); } public void testParseFieldSigComplexAP() { - + PatternParser p = new PatternParser("@(!@Foo || @Goo) * *.*"); + SignaturePattern s = p.parseFieldSignaturePattern(); + assertEquals("complex annotation","((!@Foo) || @Goo)",s.getAnnotationPattern().toString()); + assertEquals("Any field type","*",s.getReturnType().toString()); + assertEquals("Any dec type","*",s.getDeclaringType().toString()); + assertEquals("Any name","*",s.getName().toString()); + assertEquals("@((!@Foo) || @Goo) * *.*",s.toString()); } public void testExactAnnotationPatternMatching() { - // matching, non-matching + PatternParser p = new PatternParser("@Foo"); + AnnotationTypePattern ap = p.parseAnnotationTypePattern(); + AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[]{"Foo"}); + assertTrue("matches element with Foo",ap.matches(ae).alwaysTrue()); + AnnotatedElementImpl ae2 = new AnnotatedElementImpl(new String[]{"Boo"}); + assertTrue("does not match element with Boo",ap.matches(ae2).alwaysFalse()); } public void testBindingAnnotationPatternMatching() { - // matching, non-matching + PatternParser p = new PatternParser("foo"); + AnnotationTypePattern ap = p.parseAnnotationNameOrVarTypePattern(); + ap = ap.resolveBindings(makeSimpleScope(),new Bindings(3),true); + AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[]{"Foo"}); + assertTrue("matches element with Foo",ap.matches(ae).alwaysTrue()); + AnnotatedElementImpl ae2 = new AnnotatedElementImpl(new String[]{"Boo"}); + assertTrue("does not match element with Boo",ap.matches(ae2).alwaysFalse()); } public void testAndAnnotationPatternMatching() { - + PatternParser p = new PatternParser("@Foo && @Boo"); + AnnotationTypePattern ap = p.parseAnnotationTypePattern(); + AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] {"Foo","Boo"}); + assertTrue("matches foo and boo",ap.matches(ae).alwaysTrue()); + ae = new AnnotatedElementImpl(new String[] {"Foo"}); + assertTrue("does not match foo",ap.matches(ae).alwaysFalse()); + ae = new AnnotatedElementImpl(new String[] {"Boo"}); + assertTrue("does not match boo",ap.matches(ae).alwaysFalse()); + ae = new AnnotatedElementImpl(new String[] {"Goo"}); + assertTrue("does not match goo",ap.matches(ae).alwaysFalse()); } public void testOrAnnotationPatternMatching() { - + PatternParser p = new PatternParser("@Foo || @Boo"); + AnnotationTypePattern ap = p.parseAnnotationTypePattern(); + AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] {"Foo","Boo"}); + assertTrue("matches foo and boo",ap.matches(ae).alwaysTrue()); + ae = new AnnotatedElementImpl(new String[] {"Foo"}); + assertTrue("matches foo",ap.matches(ae).alwaysTrue()); + ae = new AnnotatedElementImpl(new String[] {"Boo"}); + assertTrue("matches boo",ap.matches(ae).alwaysTrue()); + ae = new AnnotatedElementImpl(new String[] {"Goo"}); + assertTrue("does not match goo",ap.matches(ae).alwaysFalse()); } public void testNotAnnotationPatternMatching() { - + PatternParser p = new PatternParser("!@Foo"); + AnnotationTypePattern ap = p.parseAnnotationTypePattern(); + AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] {"Foo","Boo"}); + assertTrue("does not match foo and boo",ap.matches(ae).alwaysFalse()); + ae = new AnnotatedElementImpl(new String[] {"Boo"}); + assertTrue("matches boo",ap.matches(ae).alwaysTrue()); } public void testAnyAnnotationPatternMatching() { - + AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] {"Foo","Boo"}); + assertTrue("always matches",AnnotationTypePattern.ANY.matches(ae).alwaysTrue()); + ae = new AnnotatedElementImpl(new String[] {}); + assertTrue("always matches",AnnotationTypePattern.ANY.matches(ae).alwaysTrue()); + } + + + public TestScope makeSimpleScope() { + return new TestScope(new String[] {"int", "java.lang.String","Foo"}, new String[] {"a", "b","foo"}, new BcelWorld()); } // put test cases for AnnotationPatternList matching in separate test class... static class AnnotatedElementImpl implements AnnotatedElement { - private boolean answer; - - public static final AnnotatedElementImpl YES = new AnnotatedElementImpl(true); - public static final AnnotatedElementImpl NO = new AnnotatedElementImpl(false); + private String[] annotationTypes; - public AnnotatedElementImpl(boolean answer) { this.answer = answer; } + public AnnotatedElementImpl(String[] annotationTypes) { + this.annotationTypes = annotationTypes; + } public boolean hasAnnotation(TypeX ofType) { - return answer; + for (int i = 0; i < annotationTypes.length; i++) { + if (annotationTypes[i].equals(ofType.getName())) return true; + } + return false; } } -- 2.39.5