]> source.dussan.org Git - aspectj.git/commitdiff
Fix for Bug 83563: pertypewithin() handling of inner classes
authoraclement <aclement>
Tue, 25 Jan 2005 20:18:42 +0000 (20:18 +0000)
committeraclement <aclement>
Tue, 25 Jan 2005 20:18:42 +0000 (20:18 +0000)
tests/bugs150/PR83563_1.java [new file with mode: 0644]
tests/bugs150/PR83563_2.java [new file with mode: 0644]
tests/src/org/aspectj/systemtest/ajc150/Ajc150TestsNoHarness.java
weaver/src/org/aspectj/weaver/PerTypeWithinTargetTypeMunger.java
weaver/src/org/aspectj/weaver/patterns/PerTypeWithin.java

diff --git a/tests/bugs150/PR83563_1.java b/tests/bugs150/PR83563_1.java
new file mode 100644 (file)
index 0000000..834e450
--- /dev/null
@@ -0,0 +1,23 @@
+public class PR83563_1 {
+    public static void main(String[] args) {
+        new NestedTest().run();
+        int c = PertypewithinTest.aspectOf(PR83563_1.class).cnt;
+        if (c!=2)
+          throw new RuntimeException("Expected 2 advice executions: "+c);
+    }
+       
+    static class NestedTest implements Runnable {
+        public void run() {
+            System.out.println("Running...");
+        }
+    }
+}
+aspect PertypewithinTest pertypewithin(PR83563_1) {
+    public static int cnt = 0;
+
+    before() : execution(* *.*(..)) {
+        cnt++;
+        System.out.println(thisJoinPointStaticPart);
+    }  
+}
diff --git a/tests/bugs150/PR83563_2.java b/tests/bugs150/PR83563_2.java
new file mode 100644 (file)
index 0000000..718f1f4
--- /dev/null
@@ -0,0 +1,25 @@
+public class PR83563_2 {
+    public void bar() {
+        new Runnable() {
+            public void run() {
+                System.out.println("Running...");
+            }
+        }.run();
+    }
+        
+    public static void main(String[] args) {
+        new PR83563_2().bar();
+        int c = PertypewithinTest.aspectOf(PR83563_2.class).cnt;
+        if (c!=3)
+          throw new RuntimeException("Expected 3 advice executions but got:"+c);
+    }
+}
+aspect PertypewithinTest pertypewithin(PR83563_2) {
+    public static int cnt = 0;
+
+    before() : execution(* *.*(..)) {
+        cnt++;
+        System.out.println(thisJoinPoint);
+    }    
+}
index 6ecac51899478d0bc69480ddc0eaabed22d3699a..3a446127b353d77b38ddbc1db1581e7374db2833 100644 (file)
@@ -75,4 +75,16 @@ public class Ajc150TestsNoHarness extends TestUtils {
        CompilationResult cR = ajc(baseDir,new String[]{"PR83303.java"});
        assertTrue("Should be no errors:"+cR,!cR.hasErrorMessages());
   }
+  
+  public void testPerTypeWithinMissesNamedInnerTypes() {
+       CompilationResult cR = ajc(baseDir,new String[]{"PR83563_1.java"});
+       assertTrue("Should be no errors:"+cR,!cR.hasErrorMessages());
+       RunResult rR = run("PR83563_1");
+  }
+  
+  public void testPerTypeWithinMissesAnonymousInnerTypes() {
+       CompilationResult cR = ajc(baseDir,new String[]{"PR83563_2.java"});
+       assertTrue("Should be no errors:"+cR,!cR.hasErrorMessages());
+       RunResult rR = run("PR83563_2");
+  }
 }
\ No newline at end of file
index 4da7f1116ab22b0ce38f6c69de06c93fe326885a..6f81df592253018c846562da7613c92e8a646233 100644 (file)
@@ -15,9 +15,9 @@ package org.aspectj.weaver;
 import java.io.DataOutputStream;
 import java.io.IOException;
 
+import org.aspectj.util.FuzzyBoolean;
 import org.aspectj.weaver.patterns.PerTypeWithin;
 import org.aspectj.weaver.patterns.Pointcut;
-import org.aspectj.weaver.patterns.TypePattern;
 
 // PTWIMPL Target type munger adds the localAspectOf() method
 public class PerTypeWithinTargetTypeMunger extends ResolvedTypeMunger {
@@ -45,8 +45,20 @@ public class PerTypeWithinTargetTypeMunger extends ResolvedTypeMunger {
                return testPointcut;
        }
        
+       // This is a lexical within() so if you say PerTypeWithin(Test) and matchType is an
+       // inner type (e.g. Test$NestedType) then it should match successfully
        public boolean matches(ResolvedTypeX matchType, ResolvedTypeX aspectType) {
-               return testPointcut.getTypePattern().matches(matchType,TypePattern.STATIC).alwaysTrue();
+               return isWithinType(matchType).alwaysTrue();
+       }
+       
+       private FuzzyBoolean isWithinType(ResolvedTypeX type) {
+               while (type != null) {
+                       if (testPointcut.getTypePattern().matchesStatically(type)) {
+                               return FuzzyBoolean.YES;
+                       }
+                       type = type.getDeclaringType();
+               }
+               return FuzzyBoolean.NO;
        }
 
 }
index 4e29e138b47ee4d55f15b2aa4653e1c794ef9a0e..cdc7c3aa9173ecef6c8295e96896e2ca3e44bfa1 100644 (file)
@@ -126,16 +126,22 @@ public class PerTypeWithin extends PerClause {
                                Member.STATIC_INITIALIZATION,
                                ModifiersPattern.ANY,
                                TypePattern.ANY,
-                               typePattern,
+                               TypePattern.ANY,//typePattern,
                                NamePattern.ANY,
                                TypePatternList.ANY,
                                ThrowsPattern.ANY,
                                AnnotationTypePattern.ANY
                                );
-               Pointcut testPc = new KindedPointcut(Shadow.StaticInitialization,sigpat);
-               Pointcut testPc2= new WithinPointcut(typePattern);
+               
+               Pointcut staticInitStar = new KindedPointcut(Shadow.StaticInitialization,sigpat);
+               Pointcut withinTp= new WithinPointcut(typePattern);
+               Pointcut andPcut = new AndPointcut(staticInitStar,withinTp);
+               // We want the pointcut to be 'staticinitialization(*) && within(<typepattern>' -
+               // we *cannot* shortcut this to staticinitialization(<typepattern>) because it
+               // doesnt mean the same thing.
+
                // This munger will initialize the aspect instance field in the matched type
-               inAspect.crosscuttingMembers.addConcreteShadowMunger(Advice.makePerTypeWithinEntry(world, testPc, inAspect));
+               inAspect.crosscuttingMembers.addConcreteShadowMunger(Advice.makePerTypeWithinEntry(world, andPcut, inAspect));
                
                ResolvedTypeMunger munger = new PerTypeWithinTargetTypeMunger(inAspect, ret);
                inAspect.crosscuttingMembers.addTypeMunger(world.concreteTypeMunger(munger, inAspect));