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.tags/V1_2_1
<classpathentry kind="var" path="JUNIT_HOME/junit.jar"/> | <classpathentry kind="var" path="JUNIT_HOME/junit.jar"/> | ||||
<classpathentry kind="lib" path="/org.aspectj.ajdt.core/out/lib.jar"/> | <classpathentry kind="lib" path="/org.aspectj.ajdt.core/out/lib.jar"/> | ||||
<classpathentry kind="src" path="/org.eclipse.jdt.core"/> | <classpathentry kind="src" path="/org.eclipse.jdt.core"/> | ||||
<classpathentry kind="src" path="/weaver"/> | |||||
<classpathentry kind="output" path=""/> | <classpathentry kind="output" path=""/> | ||||
</classpath> | </classpath> |
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); | |||||
} | |||||
} | |||||
} | |||||
} |
// (When done at weave time, it is done by invoking addRelationship() on | // (When done at weave time, it is done by invoking addRelationship() on | ||||
// AsmRelationshipProvider (see BCELTypeMunger) | // AsmRelationshipProvider (see BCELTypeMunger) | ||||
if (!ResolvedTypeMunger.persistSourceLocation) // Do it up front if we bloody have to | if (!ResolvedTypeMunger.persistSourceLocation) // Do it up front if we bloody have to | ||||
AsmInterTypeRelationshipProvider.addRelationship(onType, munger); | |||||
AsmInterTypeRelationshipProvider.getDefault().addRelationship(onType, munger); | |||||
} | } | ||||
} | } | ||||
onType + ": " + dangerousInterfaces.get(parent), | onType + ": " + dangerousInterfaces.get(parent), | ||||
onType.getSourceLocation(), null); | onType.getSourceLocation(), null); | ||||
} | } | ||||
AsmRelationshipProvider.addDeclareParentsRelationship(declareParents.getSourceLocation(),factory.fromEclipse(sourceType)); | |||||
AsmRelationshipProvider.getDefault().addDeclareParentsRelationship(declareParents.getSourceLocation(),factory.fromEclipse(sourceType), newParents); | |||||
addParent(sourceType, parent); | addParent(sourceType, parent); | ||||
} | } | ||||
} | } |
import org.aspectj.weaver.*; | import org.aspectj.weaver.*; | ||||
/** | /** | ||||
* !!! is this class still being used? | |||||
* | |||||
* @author Mik Kersten | * @author Mik Kersten | ||||
*/ | */ | ||||
public class AsmInterTypeRelationshipProvider { | public class AsmInterTypeRelationshipProvider { | ||||
protected static AsmInterTypeRelationshipProvider INSTANCE = new AsmInterTypeRelationshipProvider(); | |||||
public static final String INTER_TYPE_DECLARES = "declared on"; | public static final String INTER_TYPE_DECLARES = "declared on"; | ||||
public static final String INTER_TYPE_DECLARED_BY = "aspect declarations"; | public static final String INTER_TYPE_DECLARED_BY = "aspect declarations"; | ||||
public static void addRelationship( | |||||
public void addRelationship( | |||||
ResolvedTypeX onType, | ResolvedTypeX onType, | ||||
EclipseTypeMunger munger) { | EclipseTypeMunger munger) { | ||||
} | } | ||||
} | } | ||||
} | } | ||||
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; | |||||
} | |||||
} | } |
package org.aspectj.weaver; | package org.aspectj.weaver; | ||||
import java.util.*; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Iterator; | import java.util.Iterator; | ||||
public class AsmRelationshipProvider { | public class AsmRelationshipProvider { | ||||
protected static AsmRelationshipProvider INSTANCE = new AsmRelationshipProvider(); | |||||
public static final String ADVISES = "advises"; | public static final String ADVISES = "advises"; | ||||
public static final String ADVISED_BY = "advised by"; | public static final String ADVISED_BY = "advised by"; | ||||
public static final String DECLARES_ON = "declares on"; | public static final String DECLARES_ON = "declares on"; | ||||
public static final String MATCHES_DECLARE = "matches declare"; | public static final String MATCHES_DECLARE = "matches declare"; | ||||
public static final String INTER_TYPE_DECLARES = "declared on"; | public static final String INTER_TYPE_DECLARES = "declared on"; | ||||
public static final String INTER_TYPE_DECLARED_BY = "aspect declarations"; | 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; | if (shadow.getSourceLocation() == null || checker.getSourceLocation() == null) return; | ||||
String sourceHandle = ProgramElement.createHandleIdentifier( | String sourceHandle = ProgramElement.createHandleIdentifier( | ||||
} | } | ||||
// For ITDs | // For ITDs | ||||
public static void addRelationship( | |||||
public void addRelationship( | |||||
ResolvedTypeX onType, | ResolvedTypeX onType, | ||||
ResolvedTypeMunger munger, | ResolvedTypeMunger munger, | ||||
ResolvedTypeX originatingAspect) { | ResolvedTypeX originatingAspect) { | ||||
} | } | ||||
} | } | ||||
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()); | String sourceHandle = ProgramElement.createHandleIdentifier(decp.getSourceFile(),decp.getLine(),decp.getColumn()); | ||||
} | } | ||||
public static void adviceMunger(IHierarchy model, Shadow shadow, ShadowMunger munger) { | |||||
public void adviceMunger(IHierarchy model, Shadow shadow, ShadowMunger munger) { | |||||
if (munger instanceof Advice) { | if (munger instanceof Advice) { | ||||
Advice advice = (Advice)munger; | Advice advice = (Advice)munger; | ||||
} | } | ||||
} | } | ||||
private static IProgramElement getNode(IHierarchy model, Shadow shadow) { | |||||
private IProgramElement getNode(IHierarchy model, Shadow shadow) { | |||||
Member enclosingMember = shadow.getEnclosingCodeSignature(); | Member enclosingMember = shadow.getEnclosingCodeSignature(); | ||||
IProgramElement enclosingNode = lookupMember(model, enclosingMember); | IProgramElement enclosingNode = lookupMember(model, enclosingMember); | ||||
} | } | ||||
} | } | ||||
private static boolean sourceLinesMatch(ISourceLocation loc1,ISourceLocation loc2) { | |||||
private boolean sourceLinesMatch(ISourceLocation loc1,ISourceLocation loc2) { | |||||
if (loc1.getLine()!=loc2.getLine()) return false; | if (loc1.getLine()!=loc2.getLine()) return false; | ||||
return true; | 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(); ) { | for (Iterator it = enclosingNode.getChildren().iterator(); it.hasNext(); ) { | ||||
IProgramElement node = (IProgramElement)it.next(); | IProgramElement node = (IProgramElement)it.next(); | ||||
return peNode; | return peNode; | ||||
} | } | ||||
private static IProgramElement lookupMember(IHierarchy model, Member member) { | |||||
private IProgramElement lookupMember(IHierarchy model, Member member) { | |||||
TypeX declaringType = member.getDeclaringType(); | TypeX declaringType = member.getDeclaringType(); | ||||
IProgramElement classNode = | IProgramElement classNode = | ||||
model.findElementForType(declaringType.getPackageName(), declaringType.getClassName()); | model.findElementForType(declaringType.getPackageName(), declaringType.getClassName()); | ||||
return findMemberInClass(classNode, member); | return findMemberInClass(classNode, member); | ||||
} | } | ||||
private static IProgramElement findMemberInClass( | |||||
private IProgramElement findMemberInClass( | |||||
IProgramElement classNode, | IProgramElement classNode, | ||||
Member member) | Member member) | ||||
{ | { | ||||
// } | // } | ||||
// } | // } | ||||
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; | |||||
} | |||||
} | } |
} | } | ||||
if (world.getModel() != null) { | if (world.getModel() != null) { | ||||
AsmRelationshipProvider.checkerMunger(world.getModel(), shadow, this); | |||||
AsmRelationshipProvider.getDefault().checkerMunger(world.getModel(), shadow, this); | |||||
} | } | ||||
} | } | ||||
return false; | return false; |
if (world.getModel() != null) { | if (world.getModel() != null) { | ||||
//System.err.println("munger: " + munger + " on " + this); | //System.err.println("munger: " + munger + " on " + this); | ||||
AsmRelationshipProvider.adviceMunger(world.getModel(), this, munger); | |||||
AsmRelationshipProvider.getDefault().adviceMunger(world.getModel(), this, munger); | |||||
} | } | ||||
} | } | ||||
} | } |
if (ResolvedTypeMunger.persistSourceLocation) { | if (ResolvedTypeMunger.persistSourceLocation) { | ||||
if (changed) { | if (changed) { | ||||
if (munger.getKind().equals(ResolvedTypeMunger.Parent)) { | if (munger.getKind().equals(ResolvedTypeMunger.Parent)) { | ||||
AsmRelationshipProvider.addRelationship(weaver.getLazyClassGen().getType(), munger,getAspectType()); | |||||
AsmRelationshipProvider.getDefault().addRelationship(weaver.getLazyClassGen().getType(), munger,getAspectType()); | |||||
} else { | } else { | ||||
AsmRelationshipProvider.addRelationship(weaver.getLazyClassGen().getType(), munger,getAspectType()); | |||||
AsmRelationshipProvider.getDefault().addRelationship(weaver.getLazyClassGen().getType(), munger,getAspectType()); | |||||
} | } | ||||
} | } | ||||
} | } |