]> source.dussan.org Git - aspectj.git/commitdiff
more parser test cases and updates
authoracolyer <acolyer>
Tue, 7 Dec 2004 10:34:37 +0000 (10:34 +0000)
committeracolyer <acolyer>
Tue, 7 Dec 2004 10:34:37 +0000 (10:34 +0000)
weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java
weaver/src/org/aspectj/weaver/patterns/SignaturePattern.java
weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java

index f60c67df3f315e5516eeb1804195a4f40be6c91c..7916fc98dba622eb1a56c92c58ca91e6c1012b9a 100644 (file)
@@ -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;
index eda12badafca6eeaf02646e8c5ae88c5b8c32f36..1bdd488201386aa03775587e7f257facd4383cae 100644 (file)
@@ -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) {
index 73eab7c5c2325788c725ac188644098b549187ae..e11415c803601dd3bb988338c89e473a6872a246 100644 (file)
@@ -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;
                }
                
        }