From: aclement Date: Tue, 25 Jan 2005 20:18:42 +0000 (+0000) Subject: Fix for Bug 83563: pertypewithin() handling of inner classes X-Git-Tag: Root_AspectJ5_Development~67 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1b01255892ef222c14fea25b5db77208f1f6bb13;p=aspectj.git Fix for Bug 83563: pertypewithin() handling of inner classes --- diff --git a/tests/bugs150/PR83563_1.java b/tests/bugs150/PR83563_1.java new file mode 100644 index 000000000..834e450fe --- /dev/null +++ b/tests/bugs150/PR83563_1.java @@ -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 index 000000000..718f1f4d6 --- /dev/null +++ b/tests/bugs150/PR83563_2.java @@ -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); + } +} diff --git a/tests/src/org/aspectj/systemtest/ajc150/Ajc150TestsNoHarness.java b/tests/src/org/aspectj/systemtest/ajc150/Ajc150TestsNoHarness.java index 6ecac5189..3a446127b 100644 --- a/tests/src/org/aspectj/systemtest/ajc150/Ajc150TestsNoHarness.java +++ b/tests/src/org/aspectj/systemtest/ajc150/Ajc150TestsNoHarness.java @@ -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 diff --git a/weaver/src/org/aspectj/weaver/PerTypeWithinTargetTypeMunger.java b/weaver/src/org/aspectj/weaver/PerTypeWithinTargetTypeMunger.java index 4da7f1116..6f81df592 100644 --- a/weaver/src/org/aspectj/weaver/PerTypeWithinTargetTypeMunger.java +++ b/weaver/src/org/aspectj/weaver/PerTypeWithinTargetTypeMunger.java @@ -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; } } diff --git a/weaver/src/org/aspectj/weaver/patterns/PerTypeWithin.java b/weaver/src/org/aspectj/weaver/patterns/PerTypeWithin.java index 4e29e138b..cdc7c3aa9 100644 --- a/weaver/src/org/aspectj/weaver/patterns/PerTypeWithin.java +++ b/weaver/src/org/aspectj/weaver/patterns/PerTypeWithin.java @@ -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(' - + // we *cannot* shortcut this to staticinitialization() 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));