From abc9a58ac59f0ee427039e26cb44952cbef5e24b Mon Sep 17 00:00:00 2001 From: aclement Date: Wed, 4 May 2005 09:22:43 +0000 Subject: [PATCH] Impl of enhancement 92906 from Andrew Huff. -showWeaveInfo for declare annotation --- .../AspectDeclareAnnotations.aj | 25 +++++++ ajde/testdata/WeaveInfoMessagesTest/Seven.lst | 2 + ajde/testdata/WeaveInfoMessagesTest/Six.lst | 3 + .../expected/declare.annotation.txt | 4 ++ .../declare.annotationNoWeaveInfo.txt | 0 .../ajde/ShowWeaveMessagesTestCase.java | 26 +++++++- .../src/org/aspectj/bridge/WeaveMessage.java | 3 +- .../aspectj/weaver/bcel/BcelClassWeaver.java | 66 +++++++++++++++++++ .../org/aspectj/weaver/bcel/BcelWeaver.java | 20 +++++- .../src/org/aspectj/weaver/bcel/Utility.java | 21 ++++++ 10 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 ajde/testdata/WeaveInfoMessagesTest/AspectDeclareAnnotations.aj create mode 100644 ajde/testdata/WeaveInfoMessagesTest/Seven.lst create mode 100644 ajde/testdata/WeaveInfoMessagesTest/Six.lst create mode 100644 ajde/testdata/WeaveInfoMessagesTest/expected/declare.annotation.txt create mode 100644 ajde/testdata/WeaveInfoMessagesTest/expected/declare.annotationNoWeaveInfo.txt diff --git a/ajde/testdata/WeaveInfoMessagesTest/AspectDeclareAnnotations.aj b/ajde/testdata/WeaveInfoMessagesTest/AspectDeclareAnnotations.aj new file mode 100644 index 000000000..46bda48f8 --- /dev/null +++ b/ajde/testdata/WeaveInfoMessagesTest/AspectDeclareAnnotations.aj @@ -0,0 +1,25 @@ +// little aspect and class for testing declaration of annotations + +@interface anInterface {} + +public aspect AspectDeclareAnnotations{ + + declare @type : Test : @anInterface; + declare @constructor : Test.new(String,int) : @anInterface; + declare @method : int Test.fac(int) : @anInterface; + declare @field : int Test.a : @anInterface; + +} + +class Test{ + + public Test(String say, int something){ + System.out.println(say + something); + } + + public int fac(int n){ + return (n == 0)? 1 : n * fac(n-1); + } + + public int a = 1; +} diff --git a/ajde/testdata/WeaveInfoMessagesTest/Seven.lst b/ajde/testdata/WeaveInfoMessagesTest/Seven.lst new file mode 100644 index 000000000..b75e472ad --- /dev/null +++ b/ajde/testdata/WeaveInfoMessagesTest/Seven.lst @@ -0,0 +1,2 @@ +-1.5 +AspectDeclareAnnotations.aj diff --git a/ajde/testdata/WeaveInfoMessagesTest/Six.lst b/ajde/testdata/WeaveInfoMessagesTest/Six.lst new file mode 100644 index 000000000..51eaf0703 --- /dev/null +++ b/ajde/testdata/WeaveInfoMessagesTest/Six.lst @@ -0,0 +1,3 @@ +-showWeaveInfo +-1.5 +AspectDeclareAnnotations.aj diff --git a/ajde/testdata/WeaveInfoMessagesTest/expected/declare.annotation.txt b/ajde/testdata/WeaveInfoMessagesTest/expected/declare.annotation.txt new file mode 100644 index 000000000..7daacb75d --- /dev/null +++ b/ajde/testdata/WeaveInfoMessagesTest/expected/declare.annotation.txt @@ -0,0 +1,4 @@ +'Test' (AspectDeclareAnnotations.aj:14) is annotated with @anInterface type annotation from 'AspectDeclareAnnotations' (AspectDeclareAnnotations.aj:7) +'public void Test.new(String,int)' (AspectDeclareAnnotations.aj:16) is annotated with @anInterface constructor annotation from 'AspectDeclareAnnotations' (AspectDeclareAnnotations.aj:8) +'public int Test.fac(int)' (AspectDeclareAnnotations.aj:20) is annotated with @anInterface method annotation from 'AspectDeclareAnnotations' (AspectDeclareAnnotations.aj:9) +'public int a' of type 'Test' (AspectDeclareAnnotations.aj) is annotated with @anInterface field annotation from 'AspectDeclareAnnotations' (AspectDeclareAnnotations.aj:10) diff --git a/ajde/testdata/WeaveInfoMessagesTest/expected/declare.annotationNoWeaveInfo.txt b/ajde/testdata/WeaveInfoMessagesTest/expected/declare.annotationNoWeaveInfo.txt new file mode 100644 index 000000000..e69de29bb diff --git a/ajde/testsrc/org/aspectj/ajde/ShowWeaveMessagesTestCase.java b/ajde/testsrc/org/aspectj/ajde/ShowWeaveMessagesTestCase.java index e8da5e1ad..a4add872f 100644 --- a/ajde/testsrc/org/aspectj/ajde/ShowWeaveMessagesTestCase.java +++ b/ajde/testsrc/org/aspectj/ajde/ShowWeaveMessagesTestCase.java @@ -95,8 +95,7 @@ public class ShowWeaveMessagesTestCase extends AjdeTestCase { if (rogueSymFile.exists()) rogueSymFile.delete(); } - - + /** * Weave all the possible kinds of advice and verify the messages that come out. @@ -158,6 +157,28 @@ public class ShowWeaveMessagesTestCase extends AjdeTestCase { verifyWeavingMessages("declare.soft",true); } + /** + * Weave 'declare @type, @constructor, @method and @field' and check the weave messages that come out. + */ + public void testWeaveMessagesDeclareAnnotation() { + if (debugTests) System.out.println("\ntestWeaveMessagesDeclareAnnotation: Building with Six.lst"); + compilerAdapter = new CompilerAdapter(); + compilerAdapter.showInfoMessages(true); + compilerAdapter.compile((String) openFile("Six.lst").getAbsolutePath(),new BPM(),false); + verifyWeavingMessages("declare.annotation",true); + } + + /** + * Weave 'declare @type, @constructor, @method and @field' and check the weave messages don't come out without the -showWeaveInfo arg. + */ + public void testWeaveMessagesDeclareAnnotationWeaveInfoOff() { + if (debugTests) System.out.println("\ntestWeaveMessagesDeclareAnnotation: Building with Seven.lst"); + compilerAdapter = new CompilerAdapter(); + compilerAdapter.showInfoMessages(true); + compilerAdapter.compile((String) openFile("Seven.lst").getAbsolutePath(),new BPM(),false); + verifyWeavingMessages("declare.annotationNoWeaveInfo",true); + } + // BINARY WEAVING TESTS @@ -328,6 +349,7 @@ public class ShowWeaveMessagesTestCase extends AjdeTestCase { List l = ideManager.getCompilationSourceLineTasks(); for (Iterator iter = l.iterator(); iter.hasNext();) { IMessage msg = ((NullIdeTaskListManager.SourceLineTask) iter.next()).message; + if (debugTests) System.out.println("Looking at ["+msg+"]"); if (msg.getKind().equals(IMessage.WEAVEINFO)) { if (!fileContents.contains(msg.getMessage())) { fail("Could not find message '"+msg.getMessage()+"' in the expected results\n"+ diff --git a/bridge/src/org/aspectj/bridge/WeaveMessage.java b/bridge/src/org/aspectj/bridge/WeaveMessage.java index ceaa82283..3ddb6de00 100644 --- a/bridge/src/org/aspectj/bridge/WeaveMessage.java +++ b/bridge/src/org/aspectj/bridge/WeaveMessage.java @@ -33,8 +33,7 @@ public class WeaveMessage extends Message { new WeaveMessageKind(5,"Softening exceptions in type '%1' (%2) as defined by aspect '%3' (%4)"); public static WeaveMessageKind WEAVEMESSAGE_ANNOTATES = - new WeaveMessageKind(6,"'%1' (%2) is annoted with %3 from '%4' (%5)"); - + new WeaveMessageKind(6,"'%1' (%2) is annotated with %3 %4 annotation from '%5' (%6)"); private String affectedtypename; private String aspectname; diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelClassWeaver.java b/weaver/src/org/aspectj/weaver/bcel/BcelClassWeaver.java index fc5668c24..c966e346c 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelClassWeaver.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelClassWeaver.java @@ -51,6 +51,7 @@ import org.aspectj.apache.bcel.generic.Select; import org.aspectj.apache.bcel.generic.Type; import org.aspectj.bridge.IMessage; import org.aspectj.bridge.ISourceLocation; +import org.aspectj.bridge.WeaveMessage; import org.aspectj.util.FuzzyBoolean; import org.aspectj.util.PartialOrder; import org.aspectj.weaver.AjAttribute; @@ -457,6 +458,8 @@ class BcelClassWeaver implements IClassWeaver { if (doesAlreadyHaveAnnotation(mg.getMemberView(),decaM,reportedProblems)) continue; // skip this one... mg.addAnnotation(decaM.getAnnotationX()); AsmRelationshipProvider.getDefault().addDeclareAnnotationRelationship(decaM.getSourceLocation(),clazz.getName(),mg.getMethod()); + + reportMethodCtorWeavingMessage(clazz, mg, decaM); isChanged = true; modificationOccured = true; } else { @@ -488,6 +491,52 @@ class BcelClassWeaver implements IClassWeaver { } return isChanged; } + + // TAG: WeavingMessage + private void reportMethodCtorWeavingMessage(LazyClassGen clazz, LazyMethodGen mg, DeclareAnnotation decaM) { + if (!getWorld().getMessageHandler().isIgnoring(IMessage.WEAVEINFO)){ + StringBuffer parmString = new StringBuffer("("); + Type[] args = mg.getMethod().getArgumentTypes(); + for (int i = 0; i < args.length; i++) { + Type type2 = args[i]; + String s = org.aspectj.apache.bcel.classfile.Utility.signatureToString(type2.getSignature()); + if (s.lastIndexOf(".")!=-1) s =s.substring(s.lastIndexOf(".")+1); + parmString.append(s); + if ((i+1)")?"new":methodName); + sig.append(parmString); + + StringBuffer loc = new StringBuffer(); + if (clazz.getFileName()==null) { + loc.append("no debug info available"); + } else { + loc.append(clazz.getFileName()); + if (mg.getDeclarationLineNumber()!=-1) { + loc.append(":"+mg.getDeclarationLineNumber()); + } + } + getWorld().getMessageHandler().handleMessage( + WeaveMessage.constructWeavingMessage(WeaveMessage.WEAVEMESSAGE_ANNOTATES, + new String[]{ + sig.toString(), + loc.toString(), + decaM.getAnnotationString(), + methodName.startsWith("")?"constructor":"method", + decaM.getAspect().toString(), + Utility.beautifyLocation(decaM.getSourceLocation()) + })); + } + } /** * Looks through a list of declare annotation statements and only returns @@ -710,6 +759,7 @@ class BcelClassWeaver implements IClassWeaver { if (doesAlreadyHaveAnnotation(aBcelField,decaF,reportedProblems)) continue; // skip this one... aBcelField.addAnnotation(decaF.getAnnotationX()); AsmRelationshipProvider.getDefault().addDeclareAnnotationRelationship(decaF.getSourceLocation(),clazz.getName(),fields[fieldCounter]); + reportFieldAnnotationWeavingMessage(clazz, fields, fieldCounter, decaF); isChanged = true; modificationOccured = true; } else { @@ -741,6 +791,22 @@ class BcelClassWeaver implements IClassWeaver { } return isChanged; } + + // TAG: WeavingMessage + private void reportFieldAnnotationWeavingMessage(LazyClassGen clazz, Field[] fields, int fieldCounter, DeclareAnnotation decaF) { + if (!getWorld().getMessageHandler().isIgnoring(IMessage.WEAVEINFO)){ + Field theField = fields[fieldCounter]; + world.getMessageHandler().handleMessage( + WeaveMessage.constructWeavingMessage(WeaveMessage.WEAVEMESSAGE_ANNOTATES, + new String[]{ + theField.toString() + "' of type '" + clazz.getName(), + clazz.getFileName(), + decaF.getAnnotationString(), + "field", + decaF.getAspect().toString(), + Utility.beautifyLocation(decaF.getSourceLocation())})); + } + } /** * Check if a resolved member (field/method/ctor) already has an annotation, if it diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java b/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java index 29e57b1b2..028b229b0 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java @@ -51,6 +51,7 @@ import org.aspectj.bridge.ISourceLocation; import org.aspectj.bridge.Message; import org.aspectj.bridge.MessageUtil; import org.aspectj.bridge.SourceLocation; +import org.aspectj.bridge.WeaveMessage; import org.aspectj.util.FileUtil; import org.aspectj.util.FuzzyBoolean; import org.aspectj.weaver.Advice; @@ -1199,6 +1200,7 @@ public class BcelWeaver implements IWeaver { } } + /** * Apply a declare @type - return true if we change the type */ @@ -1206,9 +1208,25 @@ public class BcelWeaver implements IWeaver { boolean didSomething = false; if (decA.matches(onType)) { - //FIXME asc important this should be guarded by the 'already has annotation' check below but isn't since the compiler is producing classfiles with deca affected things in... + // FIXME asc important this should be guarded by the 'already has annotation' check below but isn't since the compiler is producing classfiles with deca affected things in... AsmRelationshipProvider.getDefault().addDeclareAnnotationRelationship(decA.getSourceLocation(),onType.getSourceLocation()); + // FIXME asc same comment above applies here + // TAG: WeavingMessage + if (!getWorld().getMessageHandler().isIgnoring(IMessage.WEAVEINFO)){ + getWorld().getMessageHandler().handleMessage( + WeaveMessage.constructWeavingMessage(WeaveMessage.WEAVEMESSAGE_ANNOTATES, + new String[]{ + onType.toString(), + Utility.beautifyLocation(onType.getSourceLocation()), + decA.getAnnotationString(), + "type", + decA.getAspect().toString(), + Utility.beautifyLocation(decA.getSourceLocation()) + })); + } + + if (onType.hasAnnotation(decA.getAnnotationX().getSignature())) { // FIXME asc Could put out a lint here for an already annotated type - the problem is that it may have // picked up the annotation during 'source weaving' in which case the message is misleading. Leaving it diff --git a/weaver/src/org/aspectj/weaver/bcel/Utility.java b/weaver/src/org/aspectj/weaver/bcel/Utility.java index 48e23e90f..e2a6c74d7 100644 --- a/weaver/src/org/aspectj/weaver/bcel/Utility.java +++ b/weaver/src/org/aspectj/weaver/bcel/Utility.java @@ -49,6 +49,7 @@ import org.aspectj.apache.bcel.generic.SWITCH; import org.aspectj.apache.bcel.generic.Select; import org.aspectj.apache.bcel.generic.TargetLostException; import org.aspectj.apache.bcel.generic.Type; +import org.aspectj.bridge.ISourceLocation; import org.aspectj.weaver.AnnotationX; import org.aspectj.weaver.BCException; import org.aspectj.weaver.Member; @@ -60,6 +61,26 @@ public class Utility { private Utility() { super(); } + + /* + * Ensure we report a nice source location - particular in the case + * where the source info is missing (binary weave). + */ + public static String beautifyLocation(ISourceLocation isl) { + StringBuffer nice = new StringBuffer(); + if (isl==null || isl.getSourceFile()==null || isl.getSourceFile().getName().indexOf("no debug info available")!=-1) { + nice.append("no debug info available"); + } else { + // can't use File.getName() as this fails when a Linux box encounters a path created on Windows and vice-versa + int takeFrom = isl.getSourceFile().getPath().lastIndexOf('/'); + if (takeFrom == -1) { + takeFrom = isl.getSourceFile().getPath().lastIndexOf('\\'); + } + nice.append(isl.getSourceFile().getPath().substring(takeFrom +1)); + if (isl.getLine()!=0) nice.append(":").append(isl.getLine()); + } + return nice.toString(); + } public static Instruction createSuperInvoke( -- 2.39.5