From eb673019179c372c6c17dd403e53d96feae434fd Mon Sep 17 00:00:00 2001 From: aclement Date: Thu, 9 Dec 2004 16:15:38 +0000 Subject: [PATCH] Complete varargs support. --- tests/java5/varargs/SimpleVarargs.java | 1 + tests/java5/varargs/VarargsAspect05.aj | 5 +++ tests/java5/varargs/VarargsAspect06.aj | 5 +++ tests/java5/varargs/testcode.jar | Bin 618 -> 618 bytes .../ajc150/AllTestsJava5_binaryWeaving.java | 4 +- ...uts.java => AnnotationPointcutsTests.java} | 22 ++-------- .../{Varargs.java => VarargsTests.java} | 38 ++++++++++++++++-- .../weaver/patterns/ExactTypePattern.java | 2 + .../weaver/patterns/PatternParser.java | 8 +++- .../weaver/patterns/SignaturePattern.java | 7 +++- 10 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 tests/java5/varargs/VarargsAspect05.aj create mode 100644 tests/java5/varargs/VarargsAspect06.aj rename tests/src/org/aspectj/systemtest/ajc150/{AnnotationPointcuts.java => AnnotationPointcutsTests.java} (51%) rename tests/src/org/aspectj/systemtest/ajc150/{Varargs.java => VarargsTests.java} (57%) diff --git a/tests/java5/varargs/SimpleVarargs.java b/tests/java5/varargs/SimpleVarargs.java index 4060443ee..bcd1e1bdf 100644 --- a/tests/java5/varargs/SimpleVarargs.java +++ b/tests/java5/varargs/SimpleVarargs.java @@ -21,5 +21,6 @@ public class SimpleVarargs { s.fooInt(2,new Integer(45),new Integer(45)); s.fooInt(3,new Integer[]{new Integer(45),new Integer(45)}); // s.barInt(4,new Integer[]{new Integer(45),new Integer(45)}); + } } diff --git a/tests/java5/varargs/VarargsAspect05.aj b/tests/java5/varargs/VarargsAspect05.aj new file mode 100644 index 000000000..d170e3b1e --- /dev/null +++ b/tests/java5/varargs/VarargsAspect05.aj @@ -0,0 +1,5 @@ +public aspect VarargsAspect05 { + + before(): call(* *(Integer...)) { } + +} diff --git a/tests/java5/varargs/VarargsAspect06.aj b/tests/java5/varargs/VarargsAspect06.aj new file mode 100644 index 000000000..3ea869aa2 --- /dev/null +++ b/tests/java5/varargs/VarargsAspect06.aj @@ -0,0 +1,5 @@ +public aspect VarargsAspect06 { + + before(): call(* *(int,Integer...)) { } + +} diff --git a/tests/java5/varargs/testcode.jar b/tests/java5/varargs/testcode.jar index 86331bbeb486110aaf9dd0f235b04fc55d5c76d4..f4ea2d124a31d97bd3f24c81137e23be03b93150 100644 GIT binary patch delta 537 zcmV+!0_Oed1nLA0P)h>@6aWYS2mrNkiIEK@f0D~@5>XV!zcUY}9ciI0w)pJBs#8%L z7cNYVE=){FCc40o5LW|^^ftqc8EE(XE3VzRk;FzfcI6*s{O&*~6x2w z&tG4^19$|_#R6_R*mSVv!o;=<13TK>(q`AeZ9(p-idFBKV8eUs9|b4DeiX#*{c79` zf7@YtuvZsYFOqkmpx~=Ge0AJuhUuH28PT)qC#@i=2dUEck<;oOs;9^r=-`iHbdoXeIi(!6DEL4 z+*)#+w!)Vx8)r)KLtT=`BJK#5rXqrse;HXi@vb&*9;@1{p(H4t=e=$og)L5HdYLe< zx>v_GUtt0IW1+8WA-Kfb6P^Z7``!TZnLiBVX>)Xg@3=&}K1{$e3K_sg5i5-Mb@vhG zaaM_0ggt?fF+#=&Y9AqS!=VRg&1Qy2~Fe;dq( zc}i1!Fo0DX!1gN_onMfv*cW^a>;K}`EYzvkf3I>Xxql>_?_(!>!evfbz z>~j|%xs4yVGO_M6-mda2;}fp2f`3C?&saBp15ir`2$BpWRMG(e0JsAH08mQ-0u%rg b00;mG0JU(5F_H`@6aWYS2mo<#gOLp-e^O0v(@+qc^+(*Av`O30hR=Rbz$HM% zfdeXn0}@gs3l2e%B2G>)>8l(YIce|tE1bD-Arci_=!qYNnCC=E(*zKfJTKnaot@p6 zpTE9-2k;1{Lf7sHAcQv?qtZG<8Nl={Ry>1?bEly>6nK7@r zSI0JQVFCKF(ATvPT;kUgo(50*-T?BMKMdrFIflV|Tq3Ry6R?azCSaq874m)EeI)ZZ ztHi8?eNIBg5;B&c_mRZ&tl=Dq#Y|#rNS>3pOj#L=8O+;2c_=0&4y#+9ox*5Xf8Ss> z%u_<~!2nim0Nbxz41PhbVqb_gtpDS!Sy88A|Gmnofcu=<=iCt& z*yk=javML$OxAsdE4a$Dj8C}63jPgoJ=41J8&FFJ2s;O@8qxs(0JsAH08mQ-0u%rg b00;mG0C8}GF*^sX8qxs(0JsAHlP&^0Z_51* diff --git a/tests/src/org/aspectj/systemtest/ajc150/AllTestsJava5_binaryWeaving.java b/tests/src/org/aspectj/systemtest/ajc150/AllTestsJava5_binaryWeaving.java index f03ceef4f..f2df9add9 100644 --- a/tests/src/org/aspectj/systemtest/ajc150/AllTestsJava5_binaryWeaving.java +++ b/tests/src/org/aspectj/systemtest/ajc150/AllTestsJava5_binaryWeaving.java @@ -29,8 +29,8 @@ public class AllTestsJava5_binaryWeaving { suite.addTestSuite(CovarianceTests.class); suite.addTestSuite(Enums.class); suite.addTestSuite(Annotations.class); - suite.addTestSuite(AnnotationPointcuts.class); - suite.addTestSuite(Varargs.class); + suite.addTestSuite(AnnotationPointcutsTests.class); + suite.addTestSuite(VarargsTests.class); //$JUnit-END$ return suite; } diff --git a/tests/src/org/aspectj/systemtest/ajc150/AnnotationPointcuts.java b/tests/src/org/aspectj/systemtest/ajc150/AnnotationPointcutsTests.java similarity index 51% rename from tests/src/org/aspectj/systemtest/ajc150/AnnotationPointcuts.java rename to tests/src/org/aspectj/systemtest/ajc150/AnnotationPointcutsTests.java index 1f87b10c5..517b300ae 100644 --- a/tests/src/org/aspectj/systemtest/ajc150/AnnotationPointcuts.java +++ b/tests/src/org/aspectj/systemtest/ajc150/AnnotationPointcutsTests.java @@ -20,7 +20,7 @@ import org.aspectj.tools.ajc.CompilationResult; /** * Tests the use of Annotations in pointcuts */ -public class AnnotationPointcuts extends TestUtils { +public class AnnotationPointcutsTests extends TestUtils { protected void setUp() throws Exception { super.setUp(); @@ -34,23 +34,9 @@ public class AnnotationPointcuts extends TestUtils { 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[]{}); + "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)"}); } public void test002_AtAnnotationMatching() { diff --git a/tests/src/org/aspectj/systemtest/ajc150/Varargs.java b/tests/src/org/aspectj/systemtest/ajc150/VarargsTests.java similarity index 57% rename from tests/src/org/aspectj/systemtest/ajc150/Varargs.java rename to tests/src/org/aspectj/systemtest/ajc150/VarargsTests.java index 075790c9b..7845f6bd1 100644 --- a/tests/src/org/aspectj/systemtest/ajc150/Varargs.java +++ b/tests/src/org/aspectj/systemtest/ajc150/VarargsTests.java @@ -22,7 +22,7 @@ import org.aspectj.tools.ajc.CompilationResult; * 1. cannot match on a varargs method by using 'Object[]' in your signature, * this affects call/execution/initialization/withincode */ -public class Varargs extends TestUtils { +public class VarargsTests extends TestUtils { protected void setUp() throws Exception { super.setUp(); @@ -56,11 +56,43 @@ public class Varargs extends TestUtils { } // check when signature is from an withincode PCD + // In this test, it can be tricky to understand the results!! The reason being that the shadow + // isn't included in the error message (it really should be, but thats a bit hard to do cleanly) public void test003_cantMatchVarargsWithObjectArray_withincodePCD() { - CompilationResult cR = binaryWeave("testcode.jar","VarargsAspect04.aj",0,1,true); - assertTrue("Did not get expected message about a varags mismatch, instead got: "+cR.getWarningMessages(), + CompilationResult cR = binaryWeave("testcode.jar","VarargsAspect04.aj",0,7,true); + + // There are 7. Each piece of the pointcut is matched against all the shadows, so both + // the 'withincode' PCD and the 'call' PCD are matched against every join point. + assertTrue("Did not get expected message about a varargs mismatch, instead got: "+cR.getWarningMessages(), ((IMessage)cR.getWarningMessages().get(0)).toString().indexOf("varargs declared method")!=-1); + verifyWeavingMessagesOutput(cR,new String[]{}); } + + // before(): call(* *(Integer...)) { } + public void test_usingVarargsInPointcuts1() { + CompilationResult cR = binaryWeave("testcode.jar","VarargsAspect05.aj",0,0,true); + System.err.println(cR.getStandardError()); + System.err.println(cR.getErrorMessages()); + System.err.println(cR.getInfoMessages()); + verifyWeavingMessagesOutput(cR,new String[]{ + "weaveinfo Type 'SimpleVarargs' (SimpleVarargs.java:15) advised by before advice from 'VarargsAspect05' (VarargsAspect05.aj:3)", + "weaveinfo Type 'SimpleVarargs' (SimpleVarargs.java:16) advised by before advice from 'VarargsAspect05' (VarargsAspect05.aj:3)", + "weaveinfo Type 'SimpleVarargs' (SimpleVarargs.java:17) advised by before advice from 'VarargsAspect05' (VarargsAspect05.aj:3)"}); + } + + // before(): call(* *(int,Integer...)) { } - slightly more complex pcut + public void test_usingVarargsInPointcuts2() { + CompilationResult cR = binaryWeave("testcode.jar","VarargsAspect06.aj",0,0,true); + System.err.println(cR.getStandardError()); + System.err.println(cR.getErrorMessages()); + System.err.println(cR.getInfoMessages()); + + verifyWeavingMessagesOutput(cR,new String[]{ + "weaveinfo Type 'SimpleVarargs' (SimpleVarargs.java:20) advised by before advice from 'VarargsAspect06' (VarargsAspect06.aj:3)", + "weaveinfo Type 'SimpleVarargs' (SimpleVarargs.java:21) advised by before advice from 'VarargsAspect06' (VarargsAspect06.aj:3)", + "weaveinfo Type 'SimpleVarargs' (SimpleVarargs.java:22) advised by before advice from 'VarargsAspect06' (VarargsAspect06.aj:3)"}); + } + } \ No newline at end of file diff --git a/weaver/src/org/aspectj/weaver/patterns/ExactTypePattern.java b/weaver/src/org/aspectj/weaver/patterns/ExactTypePattern.java index 6da79c779..945b76ba7 100644 --- a/weaver/src/org/aspectj/weaver/patterns/ExactTypePattern.java +++ b/weaver/src/org/aspectj/weaver/patterns/ExactTypePattern.java @@ -190,6 +190,8 @@ public class ExactTypePattern extends TypePattern { } buff.append(type.toString()); if (includeSubtypes) buff.append('+'); + // Note, there will be a rogue [] in the pattern here in the case of varargs ... + if (isVarArgs) buff.append("..."); if (annotationPattern != AnnotationTypePattern.ANY) { buff.append(')'); } diff --git a/weaver/src/org/aspectj/weaver/patterns/PatternParser.java b/weaver/src/org/aspectj/weaver/patterns/PatternParser.java index 684377120..1eab94f92 100644 --- a/weaver/src/org/aspectj/weaver/patterns/PatternParser.java +++ b/weaver/src/org/aspectj/weaver/patterns/PatternParser.java @@ -587,7 +587,13 @@ public class PatternParser { //??? what about the source location of any's???? if (names.size() == 1 && ((NamePattern)names.get(0)).isAny() && dim == 0) return TypePattern.ANY; - return new WildTypePattern(names, includeSubtypes, dim, endPos,isVarArgs); + // Notice we increase the dimensions if varargs is set. this is to allow type matching to + // succeed later: The actual signature at runtime of a method declared varargs is an array type of + // the original declared type (so Integer... becomes Integer[] in the bytecode). So, here for the + // pattern 'Integer...' we create a WildTypePattern 'Integer[]' with varargs set. If this matches + // during shadow matching, we confirm that the varargs flags match up before calling it a successful + // match. + return new WildTypePattern(names, includeSubtypes, dim+(isVarArgs?1:0), endPos,isVarArgs); } diff --git a/weaver/src/org/aspectj/weaver/patterns/SignaturePattern.java b/weaver/src/org/aspectj/weaver/patterns/SignaturePattern.java index 31cf6545a..c34a3ee7f 100644 --- a/weaver/src/org/aspectj/weaver/patterns/SignaturePattern.java +++ b/weaver/src/org/aspectj/weaver/patterns/SignaturePattern.java @@ -190,10 +190,14 @@ public class SignaturePattern extends PatternNode { // If we have matched on parameters, let's just check it isn't because the last parameter in the pattern // is an array type and the method is declared with varargs + // XXX - Ideally the shadow would be included in the msg but we don't know it... if (isNotMatchBecauseOfVarargsIssue(parameterTypes,sig.getModifiers())) { world.getLint().cantMatchArrayTypeOnVarargs.signal(sig.toString(),getSourceLocation()); return false; } + + if (parameterTypes.size()>0 && (sig.isVarargsMethod()^parameterTypes.get(parameterTypes.size()-1).isVarArgs)) + return false; // Check the throws pattern if (!throwsPattern.matches(sig.getExceptions(), world)) return false; @@ -206,6 +210,7 @@ public class SignaturePattern extends PatternNode { // If we have matched on parameters, let's just check it isn't because the last parameter in the pattern // is an array type and the method is declared with varargs + // XXX - Ideally the shadow would be included in the msg but we don't know it... if (isNotMatchBecauseOfVarargsIssue(parameterTypes,sig.getModifiers())) { world.getLint().cantMatchArrayTypeOnVarargs.signal(sig.toString(),getSourceLocation()); return false; @@ -557,7 +562,7 @@ public class SignaturePattern extends PatternNode { */ private boolean isNotMatchBecauseOfVarargsIssue(TypePatternList params,int modifiers) { if (params.size()>0 && (modifiers & Constants.ACC_VARARGS)!=0 && // XXX Promote this to an isVarargs() on MethodSignature? - params.get(params.size()-1).getExactType().isArray()) { + !params.get(params.size()-1).isVarArgs) { return true; } return false; -- 2.39.5