import java.io.File;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
import junit.framework.Test;
import org.aspectj.asm.AsmManager;
import org.aspectj.asm.IHierarchy;
import org.aspectj.asm.IProgramElement;
+import org.aspectj.asm.IRelationshipMap;
import org.aspectj.asm.internal.Relationship;
import org.aspectj.testing.XMLBasedAjcTestCase;
import org.aspectj.util.CharOperation;
+import org.aspectj.weaver.World;
public class Ajc152Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
checkSignatureOfIPE("MyClass.MyClass()",IProgramElement.Kind.INTER_TYPE_CONSTRUCTOR);
}
-
+ // if not filling in the model for aspects contained in jar files then
+ // want to ensure that the relationship map is correct and has nodes
+ // which can be used in AJDT - ensure no NPE occurs for the end of
+ // the relationship with aspectpath
+ public void testAspectPathRelWhenNotFillingInModel_pr141730() {
+ World.createInjarHierarchy = false;
+ try {
+ runTest("ensure aspectpath injar relationships are correct when not filling in model");
+
+ // expecting:
+ // sourceOfRelationship main in MyFoo.java
+ // relationship advised by
+ // target MyBar.aj
+ //
+ // and
+ //
+ // sourceOfRelationship MyBar.aj
+ // relationship advises
+ // target main in MyFoo.java
+
+
+ IHierarchy top = AsmManager.getDefault().getHierarchy();
+ IRelationshipMap asmRelMap = AsmManager.getDefault().getRelationshipMap();
+ assertEquals("expected two sources of relationships but only found "
+ + asmRelMap.getEntries().size(),2,asmRelMap.getEntries().size());
+ for (Iterator iter = asmRelMap.getEntries().iterator(); iter.hasNext();) {
+ String sourceOfRelationship = (String) iter.next();
+ IProgramElement ipe = top.findElementForHandle(sourceOfRelationship);
+ List relationships = asmRelMap.get(ipe);
+ if (ipe.getName().equals("MyBar.aj")) {
+ assertEquals("expected MyBar.aj to have one relationships but found "
+ + relationships.size(),1,relationships.size());
+ Relationship rel = (Relationship)relationships.get(0);
+ assertEquals("expected relationship to be 'advises' but was "
+ + rel.getName(), "advises", rel.getName());
+ List targets = rel.getTargets();
+ assertEquals("expected one target but found " + targets.size(),1,targets.size());
+ IProgramElement link = top.findElementForHandle((String)targets.get(0));
+ assertEquals("expected target 'method-call(void foo.MyFoo.main())' but target " + link.getName(),
+ "method-call(void foo.MyFoo.main())",link.getName());
+ String fileName = link.getSourceLocation().getSourceFile().toString();
+ assertTrue("expected 'main' to be in class MyFoo.java but found it " +
+ "in " + fileName,fileName.indexOf("MyFoo.java") != -1);
+ } else if (ipe.getName().equals("method-call(void foo.MyFoo.main())")) {
+ String fileName = ipe.getSourceLocation().getSourceFile().toString();
+ assertTrue("expected 'method-call(void foo.MyFoo.main())' to be in " +
+ "class MyFoo.java but found it in"
+ + fileName,fileName.indexOf("MyFoo.java") != -1);
+ assertEquals("expected 'method-call(void foo.MyFoo.main())' " +
+ "to have one relationships but found "
+ + relationships.size(),1,relationships.size());
+ Relationship rel = (Relationship)relationships.get(0);
+ assertEquals("expected relationship to be 'advised by' but was "
+ + rel.getName(), "advised by", rel.getName());
+ List targets = rel.getTargets();
+ assertEquals("expected one target but found " + targets.size(),1,targets.size());
+ IProgramElement link = top.findElementForHandle((String)targets.get(0));
+ assertEquals("expected target 'MyBar.aj' but target " + link.getName(),
+ "MyBar.aj",link.getName());
+
+ } else {
+ fail("unexpected element " + ipe.getName() + " in the relationship map");
+ }
+ }
+ } finally {
+ World.createInjarHierarchy = true;
+ }
+ }
+ // if not filling in the model for classes contained in jar files then
+ // want to ensure that the relationship map is correct and has nodes
+ // which can be used in AJDT - ensure no NPE occurs for the end of
+ // the relationship with inpath
+ public void testNoNPEWithInpathWhenNotFillingInModel_pr141730() {
+ World.createInjarHierarchy = false;
+ try {
+ runTest("ensure inpath injar relationships are correct when not filling in model");
+ // the aspect used for this test has advice, declare parents, deow,
+ // and declare @type, @constructor, @field and @method. We only expect
+ // there to be relationships in the map for declare parents and declare @type
+ // (provided the model isn't being filled in) because the logic in the other
+ // addXXXRelationship methods use AspectJElementHierarchy.findElementForType().
+ // This method which returns null because there is no such ipe. The relationship is
+ // therefore not added to the model. Adding declare @type and declare parents
+ // uses AspectJElementHierarchy.findElementForHandle() which returns the file
+ // node ipe if it can't find one for the given handle. Therefore the relationships
+ // are added against the file node. Before change to using ipe's to create handles
+ // we also had the deow relationship, however, now we don't because this also
+ // uses findElementForType to find the targetNode which in the inpath case is null.
+
+ // just check that the number of entries is what we expect:
+ // We expect 3 (the declare @type and declare parents statements plus the filenode)
+ IRelationshipMap relMap = AsmManager.getDefault().getRelationshipMap();
+ assertEquals("expected 3 entries in the relationship map but found "
+ + relMap.getEntries().size(), 3, relMap.getEntries().size());
+ } finally {
+ World.createInjarHierarchy = true;
+ }
+ }
// public void testFunkyGenericErrorWithITDs_pr126355_2() {
// runTest("bizarre generic error with itds - 2");
if (shadow.getSourceLocation() == null || checker.getSourceLocation() == null) return;
// Ensure a node for the target exists
- IProgramElement targetNode = getNode(AsmManager.getDefault().getHierarchy(), shadow);
+ IProgramElement targetNode = getNode(AsmManager.getDefault().getHierarchy(),shadow);
+ if (targetNode == null) return;
+ String targetHandle = AsmManager.getDefault().getHandleProvider()
+ .createHandleIdentifier(targetNode);
+ if (targetHandle == null) return;
+
IProgramElement sourceNode = AsmManager.getDefault().getHierarchy()
.findElementForSourceLine(checker.getSourceLocation());
String sourceHandle = AsmManager.getDefault().getHandleProvider()
.createHandleIdentifier(sourceNode);
- String targetHandle = AsmManager.getDefault().getHandleProvider()
- .createHandleIdentifier(targetNode);
+ if (sourceHandle == null) return;
if (World.createInjarHierarchy) {
checker.createHierarchy();
}
IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
- if (sourceHandle != null && targetHandle != null) {
- IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE, MATCHED_BY,false,true);
- foreward.addTarget(targetHandle);
+ IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE, MATCHED_BY,false,true);
+ foreward.addTarget(targetHandle);
- IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE, MATCHES_DECLARE,false,true);
- if (back != null && back.getTargets() != null) {
- back.addTarget(sourceHandle);
- }
+ IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE, MATCHES_DECLARE,false,true);
+ if (back != null && back.getTargets() != null) {
+ back.addTarget(sourceHandle);
}
}
ResolvedType originatingAspect) {
if (!AsmManager.isCreatingModel()) return;
- String sourceHandle = "";
- if (munger.getSourceLocation()!=null && munger.getSourceLocation().getOffset()!=-1) {
- IProgramElement sourceNode = AsmManager.getDefault().getHierarchy()
- .findElementForSourceLine(munger.getSourceLocation());
- sourceHandle = AsmManager.getDefault().getHandleProvider()
- .createHandleIdentifier(sourceNode);
- } else {
- IProgramElement sourceNode = AsmManager.getDefault().getHierarchy()
- .findElementForSourceLine(originatingAspect.getSourceLocation());
- sourceHandle = AsmManager.getDefault().getHandleProvider()
- .createHandleIdentifier(sourceNode);
- }
if (originatingAspect.getSourceLocation() != null) {
+ String sourceHandle = "";
+ if (munger.getSourceLocation()!=null && munger.getSourceLocation().getOffset()!=-1) {
+ IProgramElement sourceNode = AsmManager.getDefault().getHierarchy()
+ .findElementForSourceLine(munger.getSourceLocation());
+ sourceHandle = AsmManager.getDefault().getHandleProvider()
+ .createHandleIdentifier(sourceNode);
+ } else {
+ IProgramElement sourceNode = AsmManager.getDefault().getHierarchy()
+ .findElementForSourceLine(originatingAspect.getSourceLocation());
+ sourceHandle = AsmManager.getDefault().getHandleProvider()
+ .createHandleIdentifier(sourceNode);
+ }
+ if (sourceHandle == null) return;
IProgramElement targetNode = AsmManager.getDefault().getHierarchy()
.findElementForSourceLine(onType.getSourceLocation());
String targetHandle = AsmManager.getDefault().getHandleProvider()
.createHandleIdentifier(targetNode);
-
+ if (targetHandle == null) return;
+
IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
- if (sourceHandle != null && targetHandle != null) {
- IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARES,false,true);
- foreward.addTarget(targetHandle);
-// foreward.getTargets().add(targetHandle);
-
- IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARED_BY,false,true);
- back.addTarget(sourceHandle);
-// back.getTargets().add(sourceHandle);
- }
+ IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARES,false,true);
+ foreward.addTarget(targetHandle);
+
+ IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARED_BY,false,true);
+ back.addTarget(sourceHandle);
}
}
.findElementForSourceLine(decp);
String sourceHandle = AsmManager.getDefault().getHandleProvider()
.createHandleIdentifier(sourceNode);
+ if (sourceHandle == null) return;
+
IProgramElement targetNode = AsmManager.getDefault().getHierarchy()
.findElementForSourceLine(targetType.getSourceLocation());
String targetHandle = AsmManager.getDefault().getHandleProvider()
.createHandleIdentifier(targetNode);
+ if (targetHandle == null) return;
IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
- if (sourceHandle != null && targetHandle != null) {
- IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARES,false,true);
- foreward.addTarget(targetHandle);
+ IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARES,false,true);
+ foreward.addTarget(targetHandle);
- IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARED_BY,false,true);
- back.addTarget(sourceHandle);
- }
-
+ IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARED_BY,false,true);
+ back.addTarget(sourceHandle);
}
/**
.findElementForSourceLine(declareAnnotationLocation);
String sourceHandle = AsmManager.getDefault().getHandleProvider()
.createHandleIdentifier(sourceNode);
+ if (sourceHandle == null) return;
IProgramElement targetNode = AsmManager.getDefault().getHierarchy()
.findElementForSourceLine(annotatedLocation);
String targetHandle = AsmManager.getDefault().getHandleProvider()
.createHandleIdentifier(targetNode);
-
+ if (targetHandle == null) return;
+
IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
- if (sourceHandle != null && targetHandle != null) {
- IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATES,false,true);
- foreward.addTarget(targetHandle);
+ IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATES,false,true);
+ foreward.addTarget(targetHandle);
- IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATED_BY,false,true);
- back.addTarget(sourceHandle);
- }
+ IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATED_BY,false,true);
+ back.addTarget(sourceHandle);
}
public void adviceMunger(IHierarchy model, Shadow shadow, ShadowMunger munger) {
IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
IProgramElement targetNode = getNode(AsmManager.getDefault().getHierarchy(), shadow);
+ if (targetNode == null) return;
boolean runtimeTest = ((BcelAdvice)munger).hasDynamicTests();
// Work out extra info to inform interested UIs !
IProgramElement.ExtraInformation ai = new IProgramElement.ExtraInformation();
String adviceHandle = advice.getHandle();
+ if (adviceHandle == null) return;
// What kind of advice is it?
// TODO: Prob a better way to do this but I just want to
if (adviceElement != null) {
adviceElement.setExtraInfo(ai);
}
-
- if (adviceHandle != null && targetNode != null) {
-
- if (targetNode != null) {
- String targetHandle = targetNode.getHandleIdentifier();
- if (advice.getKind().equals(AdviceKind.Softener)) {
- IRelationship foreward = mapper.get(adviceHandle, IRelationship.Kind.DECLARE_SOFT, SOFTENS,runtimeTest,true);
- if (foreward != null) foreward.addTarget(targetHandle);//foreward.getTargets().add(targetHandle);
+ String targetHandle = targetNode.getHandleIdentifier();
+ if (advice.getKind().equals(AdviceKind.Softener)) {
+ IRelationship foreward = mapper.get(adviceHandle, IRelationship.Kind.DECLARE_SOFT, SOFTENS,runtimeTest,true);
+ if (foreward != null) foreward.addTarget(targetHandle);//foreward.getTargets().add(targetHandle);
- IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE, SOFTENED_BY,runtimeTest,true);
- if (back != null) back.addTarget(adviceHandle);//back.getTargets().add(adviceHandle);
- } else {
- IRelationship foreward = mapper.get(adviceHandle, IRelationship.Kind.ADVICE, ADVISES,runtimeTest,true);
- if (foreward != null) foreward.addTarget(targetHandle);//foreward.getTargets().add(targetHandle);
+ IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE, SOFTENED_BY,runtimeTest,true);
+ if (back != null) back.addTarget(adviceHandle);//back.getTargets().add(adviceHandle);
+ } else {
+ IRelationship foreward = mapper.get(adviceHandle, IRelationship.Kind.ADVICE, ADVISES,runtimeTest,true);
+ if (foreward != null) foreward.addTarget(targetHandle);//foreward.getTargets().add(targetHandle);
- IRelationship back = mapper.get(targetHandle, IRelationship.Kind.ADVICE, ADVISED_BY,runtimeTest,true);
- if (back != null) back.addTarget(adviceHandle);//back.getTargets().add(adviceHandle);
- }
- }
- }
-
+ IRelationship back = mapper.get(targetHandle, IRelationship.Kind.ADVICE, ADVISED_BY,runtimeTest,true);
+ if (back != null) back.addTarget(adviceHandle);//back.getTargets().add(adviceHandle);
+ }
}
}
if (methodElem == null) return;
try {
+ String targetHandle = methodElem.getHandleIdentifier();
+ if (targetHandle == null) return;
IProgramElement sourceNode = AsmManager.getDefault().getHierarchy().findElementForSourceLine(sourceLocation);
String sourceHandle = AsmManager.getDefault().getHandleProvider().createHandleIdentifier(sourceNode);
-
- String targetHandle = methodElem.getHandleIdentifier();
- IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
- if (sourceHandle != null && targetHandle != null) {
- IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATES,false,true);
- foreward.addTarget(targetHandle);
+ if (sourceHandle == null) return;
+
+ IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
+ IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATES,false,true);
+ foreward.addTarget(targetHandle);
- IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATED_BY,false,true);
- back.addTarget(sourceHandle);
- }
- } catch (Throwable t) { // I'm worried about that code above, this will make sure we don't explode if it plays up
- t.printStackTrace(); // I know I know .. but I don't want to lose it!
- }
+ IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATED_BY,false,true);
+ back.addTarget(sourceHandle);
+ } catch (Throwable t) { // I'm worried about that code above, this will make sure we don't explode if it plays up
+ t.printStackTrace(); // I know I know .. but I don't want to lose it!
+ }
}
/**
IProgramElement fieldElem = AsmManager.getDefault().getHierarchy().findElementForSignature(typeElem,IProgramElement.Kind.FIELD,field.getName());
if (fieldElem== null) return;
- IProgramElement sourceNode = AsmManager.getDefault().getHierarchy().findElementForSourceLine(sourceLocation);
- String sourceHandle = AsmManager.getDefault().getHandleProvider().createHandleIdentifier(sourceNode);
-
String targetHandle = fieldElem.getHandleIdentifier();
+ if (targetHandle == null) return;
+
+ IProgramElement sourceNode = AsmManager.getDefault().getHierarchy().findElementForSourceLine(sourceLocation);
+ String sourceHandle = AsmManager.getDefault().getHandleProvider().createHandleIdentifier(sourceNode);
+ if (sourceHandle == null) return;
IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
- if (sourceHandle != null && targetHandle != null) {
- IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATES,false,true);
- foreward.addTarget(targetHandle);
+ IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATES,false,true);
+ foreward.addTarget(targetHandle);
- IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATED_BY,false,true);
- back.addTarget(sourceHandle);
- }
+ IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, ANNOTATED_BY,false,true);
+ back.addTarget(sourceHandle);
}
}