From 9be4b2e3694e69244a003d1b279f91d79eb2abe8 Mon Sep 17 00:00:00 2001 From: mkersten Date: Wed, 18 Aug 2004 23:38:38 +0000 Subject: [PATCH] Added support for clients to extend the ASM relationships in order to add new relationships (such as the example in the sandbox that adds additional declared-parents relationships). There is no structural change to the compiler or weaver. The AsmRelationshipProvider is now accessed by getDefault() instead of statically, and its instance can be reset to be a subtype. --- docs/sandbox/api-clients/.classpath | 1 + .../AsmRelationshipMapExtensionTest.java | 88 +++++++++++++++++++ .../compiler/lookup/AjLookupEnvironment.java | 4 +- .../AsmInterTypeRelationshipProvider.java | 18 +++- .../weaver/AsmRelationshipProvider.java | 35 +++++--- weaver/src/org/aspectj/weaver/Checker.java | 2 +- weaver/src/org/aspectj/weaver/Shadow.java | 2 +- .../aspectj/weaver/bcel/BcelTypeMunger.java | 4 +- 8 files changed, 137 insertions(+), 17 deletions(-) create mode 100644 docs/sandbox/api-clients/org/aspectj/samples/AsmRelationshipMapExtensionTest.java diff --git a/docs/sandbox/api-clients/.classpath b/docs/sandbox/api-clients/.classpath index 66888ff8c..03a2855ea 100644 --- a/docs/sandbox/api-clients/.classpath +++ b/docs/sandbox/api-clients/.classpath @@ -10,5 +10,6 @@ + diff --git a/docs/sandbox/api-clients/org/aspectj/samples/AsmRelationshipMapExtensionTest.java b/docs/sandbox/api-clients/org/aspectj/samples/AsmRelationshipMapExtensionTest.java new file mode 100644 index 000000000..11b9ff0d4 --- /dev/null +++ b/docs/sandbox/api-clients/org/aspectj/samples/AsmRelationshipMapExtensionTest.java @@ -0,0 +1,88 @@ +package org.aspectj.samples; + +import java.util.Iterator; +import java.util.List; + +import org.aspectj.ajde.AjdeTestCase; +import org.aspectj.asm.*; +import org.aspectj.asm.internal.ProgramElement; +import org.aspectj.bridge.ISourceLocation; +import org.aspectj.weaver.AsmRelationshipProvider; +import org.aspectj.weaver.ResolvedTypeX; + +/** + * @author Mik Kersten + */ +public class AsmRelationshipMapExtensionTest extends AjdeTestCase { + + public void testDeclares() { + System.out.println("----------------------------------"); + System.out.println("Parents declared by declare parents statements: "); + HierarchyWalker walker = new HierarchyWalker() { + public void preProcess(IProgramElement node) { + if (node.getKind().equals(IProgramElement.Kind.DECLARE_PARENTS)) { + System.out.println(node); + + List relations = AsmManager.getDefault().getRelationshipMap().get(node); + if (relations != null) { + for (Iterator it = relations.iterator(); it.hasNext();) { + IRelationship relationship = (IRelationship) it.next(); + List targets = relationship.getTargets(); + for (Iterator iter = targets.iterator(); iter.hasNext();) { + IProgramElement currElt = AsmManager + .getDefault().getHierarchy() + .getElement((String) iter.next()); + System.out.println("--> " + relationship.getName() + ": " + currElt); + } + } + } + } + } + }; + AsmManager.getDefault().getHierarchy().getRoot().walk(walker); + } + + protected void setUp() throws Exception { + AsmRelationshipProvider.setDefault(new DeclareInfoProvider()); + super.setUp("examples"); + + assertTrue("build success", + doSynchronousBuild("../examples/coverage/coverage.lst")); + } +} + +class DeclareInfoProvider extends AsmRelationshipProvider { + + public void addDeclareParentsRelationship(ISourceLocation decp, + ResolvedTypeX targetType, List newParents) { + super.addDeclareParentsRelationship(decp, targetType, newParents); + for (Iterator it = newParents.iterator(); it.hasNext();) { + ResolvedTypeX superType = (ResolvedTypeX) it.next(); + + String sourceHandle = ProgramElement.createHandleIdentifier(decp.getSourceFile(),decp.getLine(),decp.getColumn()); + IProgramElement ipe = AsmManager.getDefault().getHierarchy().findElementForHandle(sourceHandle); + + String superHandle = ProgramElement.createHandleIdentifier( + superType.getSourceLocation().getSourceFile(), + superType.getSourceLocation().getLine(), + superType.getSourceLocation().getColumn()); + + if (sourceHandle != null && superHandle != null) { + IRelationship foreward = AsmManager.getDefault().getRelationshipMap().get( + sourceHandle, + IRelationship.Kind.DECLARE, + "super types declared", + false, + true); + foreward.addTarget(superHandle); + + IRelationship back = AsmManager.getDefault().getRelationshipMap().get( + superHandle, IRelationship.Kind.DECLARE, + "declared as super type by", + false, + true); + back.addTarget(sourceHandle); + } + } + } +} \ No newline at end of file diff --git a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/AjLookupEnvironment.java b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/AjLookupEnvironment.java index 0acbb6465..110d7539e 100644 --- a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/AjLookupEnvironment.java +++ b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/AjLookupEnvironment.java @@ -329,7 +329,7 @@ public class AjLookupEnvironment extends LookupEnvironment { // (When done at weave time, it is done by invoking addRelationship() on // AsmRelationshipProvider (see BCELTypeMunger) if (!ResolvedTypeMunger.persistSourceLocation) // Do it up front if we bloody have to - AsmInterTypeRelationshipProvider.addRelationship(onType, munger); + AsmInterTypeRelationshipProvider.getDefault().addRelationship(onType, munger); } } @@ -362,7 +362,7 @@ public class AjLookupEnvironment extends LookupEnvironment { onType + ": " + dangerousInterfaces.get(parent), onType.getSourceLocation(), null); } - AsmRelationshipProvider.addDeclareParentsRelationship(declareParents.getSourceLocation(),factory.fromEclipse(sourceType)); + AsmRelationshipProvider.getDefault().addDeclareParentsRelationship(declareParents.getSourceLocation(),factory.fromEclipse(sourceType), newParents); addParent(sourceType, parent); } } diff --git a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/AsmInterTypeRelationshipProvider.java b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/AsmInterTypeRelationshipProvider.java index bb97a6ddd..ad43d9ee9 100644 --- a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/AsmInterTypeRelationshipProvider.java +++ b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/AsmInterTypeRelationshipProvider.java @@ -19,14 +19,18 @@ import org.aspectj.asm.internal.ProgramElement; import org.aspectj.weaver.*; /** + * !!! is this class still being used? + * * @author Mik Kersten */ public class AsmInterTypeRelationshipProvider { + protected static AsmInterTypeRelationshipProvider INSTANCE = new AsmInterTypeRelationshipProvider(); + public static final String INTER_TYPE_DECLARES = "declared on"; public static final String INTER_TYPE_DECLARED_BY = "aspect declarations"; - public static void addRelationship( + public void addRelationship( ResolvedTypeX onType, EclipseTypeMunger munger) { @@ -63,4 +67,16 @@ public class AsmInterTypeRelationshipProvider { } } } + + public static AsmInterTypeRelationshipProvider getDefault() { + return INSTANCE; + } + + /** + * Reset the instance of this class, intended for extensibility. + * This enables a subclass to become used as the default instance. + */ + public static void setDefault(AsmInterTypeRelationshipProvider instance) { + INSTANCE = instance; + } } diff --git a/weaver/src/org/aspectj/weaver/AsmRelationshipProvider.java b/weaver/src/org/aspectj/weaver/AsmRelationshipProvider.java index 176fbad00..2d65bc14d 100644 --- a/weaver/src/org/aspectj/weaver/AsmRelationshipProvider.java +++ b/weaver/src/org/aspectj/weaver/AsmRelationshipProvider.java @@ -13,6 +13,7 @@ package org.aspectj.weaver; +import java.util.*; import java.util.ArrayList; import java.util.Iterator; @@ -28,6 +29,8 @@ import org.aspectj.weaver.bcel.BcelAdvice; public class AsmRelationshipProvider { + protected static AsmRelationshipProvider INSTANCE = new AsmRelationshipProvider(); + public static final String ADVISES = "advises"; public static final String ADVISED_BY = "advised by"; public static final String DECLARES_ON = "declares on"; @@ -36,8 +39,8 @@ public class AsmRelationshipProvider { public static final String MATCHES_DECLARE = "matches declare"; public static final String INTER_TYPE_DECLARES = "declared on"; public static final String INTER_TYPE_DECLARED_BY = "aspect declarations"; - - public static void checkerMunger(IHierarchy model, Shadow shadow, Checker checker) { + + public void checkerMunger(IHierarchy model, Shadow shadow, Checker checker) { if (shadow.getSourceLocation() == null || checker.getSourceLocation() == null) return; String sourceHandle = ProgramElement.createHandleIdentifier( @@ -65,7 +68,7 @@ public class AsmRelationshipProvider { } // For ITDs - public static void addRelationship( + public void addRelationship( ResolvedTypeX onType, ResolvedTypeMunger munger, ResolvedTypeX originatingAspect) { @@ -102,7 +105,7 @@ public class AsmRelationshipProvider { } } - public static void addDeclareParentsRelationship(ISourceLocation decp,ResolvedTypeX targetType) { + public void addDeclareParentsRelationship(ISourceLocation decp,ResolvedTypeX targetType, List newParents) { String sourceHandle = ProgramElement.createHandleIdentifier(decp.getSourceFile(),decp.getLine(),decp.getColumn()); @@ -125,7 +128,7 @@ public class AsmRelationshipProvider { } - public static void adviceMunger(IHierarchy model, Shadow shadow, ShadowMunger munger) { + public void adviceMunger(IHierarchy model, Shadow shadow, ShadowMunger munger) { if (munger instanceof Advice) { Advice advice = (Advice)munger; @@ -168,7 +171,7 @@ public class AsmRelationshipProvider { } } - private static IProgramElement getNode(IHierarchy model, Shadow shadow) { + private IProgramElement getNode(IHierarchy model, Shadow shadow) { Member enclosingMember = shadow.getEnclosingCodeSignature(); IProgramElement enclosingNode = lookupMember(model, enclosingMember); @@ -189,13 +192,13 @@ public class AsmRelationshipProvider { } } - private static boolean sourceLinesMatch(ISourceLocation loc1,ISourceLocation loc2) { + private boolean sourceLinesMatch(ISourceLocation loc1,ISourceLocation loc2) { if (loc1.getLine()!=loc2.getLine()) return false; return true; } - private static IProgramElement findOrCreateCodeNode(IProgramElement enclosingNode, Member shadowSig, Shadow shadow) + private IProgramElement findOrCreateCodeNode(IProgramElement enclosingNode, Member shadowSig, Shadow shadow) { for (Iterator it = enclosingNode.getChildren().iterator(); it.hasNext(); ) { IProgramElement node = (IProgramElement)it.next(); @@ -224,14 +227,14 @@ public class AsmRelationshipProvider { return peNode; } - private static IProgramElement lookupMember(IHierarchy model, Member member) { + private IProgramElement lookupMember(IHierarchy model, Member member) { TypeX declaringType = member.getDeclaringType(); IProgramElement classNode = model.findElementForType(declaringType.getPackageName(), declaringType.getClassName()); return findMemberInClass(classNode, member); } - private static IProgramElement findMemberInClass( + private IProgramElement findMemberInClass( IProgramElement classNode, Member member) { @@ -275,4 +278,16 @@ public class AsmRelationshipProvider { // } // } + public static AsmRelationshipProvider getDefault() { + return INSTANCE; + } + + /** + * Reset the instance of this class, intended for extensibility. + * This enables a subclass to become used as the default instance. + */ + public static void setDefault(AsmRelationshipProvider instance) { + INSTANCE = instance; + } + } diff --git a/weaver/src/org/aspectj/weaver/Checker.java b/weaver/src/org/aspectj/weaver/Checker.java index 8c8107e96..cfed27510 100644 --- a/weaver/src/org/aspectj/weaver/Checker.java +++ b/weaver/src/org/aspectj/weaver/Checker.java @@ -66,7 +66,7 @@ public class Checker extends ShadowMunger { } if (world.getModel() != null) { - AsmRelationshipProvider.checkerMunger(world.getModel(), shadow, this); + AsmRelationshipProvider.getDefault().checkerMunger(world.getModel(), shadow, this); } } return false; diff --git a/weaver/src/org/aspectj/weaver/Shadow.java b/weaver/src/org/aspectj/weaver/Shadow.java index 58ae6e723..de2348a4d 100644 --- a/weaver/src/org/aspectj/weaver/Shadow.java +++ b/weaver/src/org/aspectj/weaver/Shadow.java @@ -458,7 +458,7 @@ public abstract class Shadow { if (world.getModel() != null) { //System.err.println("munger: " + munger + " on " + this); - AsmRelationshipProvider.adviceMunger(world.getModel(), this, munger); + AsmRelationshipProvider.getDefault().adviceMunger(world.getModel(), this, munger); } } } diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelTypeMunger.java b/weaver/src/org/aspectj/weaver/bcel/BcelTypeMunger.java index c6f05aba5..b490e3d21 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelTypeMunger.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelTypeMunger.java @@ -87,9 +87,9 @@ public class BcelTypeMunger extends ConcreteTypeMunger { if (ResolvedTypeMunger.persistSourceLocation) { if (changed) { if (munger.getKind().equals(ResolvedTypeMunger.Parent)) { - AsmRelationshipProvider.addRelationship(weaver.getLazyClassGen().getType(), munger,getAspectType()); + AsmRelationshipProvider.getDefault().addRelationship(weaver.getLazyClassGen().getType(), munger,getAspectType()); } else { - AsmRelationshipProvider.addRelationship(weaver.getLazyClassGen().getType(), munger,getAspectType()); + AsmRelationshipProvider.getDefault().addRelationship(weaver.getLazyClassGen().getType(), munger,getAspectType()); } } } -- 2.39.5