1 2 25 26 package org.aspectj.asm.associations; 27 28 import java.util.*; 29 import org.aspectj.compiler.base.*; 30 import org.aspectj.compiler.base.ast.*; 31 import org.aspectj.compiler.base.cst.*; 32 import org.aspectj.compiler.crosscuts.*; 33 import org.aspectj.compiler.crosscuts.ast.*; 34 import org.aspectj.asm.*; 35 import org.aspectj.asm.internal.*; 36 37 40 public class Advice implements Association { 41 42 public static final String NAME = "Advice"; 43 public static final Relation METHOD_RELATION = new Relation("affects methods", "method affected by", NAME, true, false); 44 public static final Relation METHOD_CALL_SITE_RELATION = new Relation("affects method call sites", "method call site affected by", NAME, true, false); 45 public static final Relation CONSTRUCTOR_RELATION = new Relation("affects constructors", "constructor affected by", NAME, true, false); 46 public static final Relation CONSTRUCTOR_CALL_SITE_RELATION = new Relation("affects constructions", "construction affected by", NAME, true, false); 47 public static final Relation HANDLER_RELATION = new Relation("affects handlers", "handler affected by", NAME, true, false); 48 public static final Relation INITIALIZER_RELATION = new Relation("affects initializers", "initializer affected by", NAME, true, false); 49 public static final Relation FIELD_ACCESS_RELATION = new Relation("affects field access", "field access affected by", NAME, true, false); 50 public static final Relation INTRODUCTION_RELATION = new Relation("affects introduction", "introduction affected by", NAME, true, false); 51 52 private List relations = new ArrayList(); 53 54 public Advice() { 55 relations.add(METHOD_RELATION); 56 relations.add(METHOD_CALL_SITE_RELATION); 57 relations.add(CONSTRUCTOR_RELATION); 58 relations.add(CONSTRUCTOR_CALL_SITE_RELATION); 59 relations.add(HANDLER_RELATION); 60 relations.add(INITIALIZER_RELATION); 61 relations.add(FIELD_ACCESS_RELATION); 62 relations.add(INTRODUCTION_RELATION); 63 } 64 65 public List getRelations() { 66 return relations; 67 } 68 69 public List getRelationNodes(ASTObject astObject) { 70 List relations = new ArrayList(); 71 List methods = new ArrayList(); 72 List methodCallSites = new ArrayList(); 73 List constructors = new ArrayList(); 74 List constructorCallSites = new ArrayList(); 75 List handlers = new ArrayList(); 76 List initializers = new ArrayList(); 77 List fieldAccesses = new ArrayList(); 78 List introductions = new ArrayList(); 79 Set forwardCorrs = StructureModelManager.correspondences.getAffects(astObject); 80 Set backCorrs = StructureModelManager.correspondences.getAffectedBy(astObject); 81 82 if (astObject instanceof AdviceDec) { 83 for (Iterator it = forwardCorrs.iterator(); it.hasNext(); ) { 84 ASTObject node = (ASTObject)it.next(); 85 LinkNode link = StructureNodeFactory.makeLink(node, false); 86 if (node instanceof MethodDec) { 87 if (((MethodDec)node).isSynthetic()) { 88 ASTObject resolvedNode = resolveSyntheticMethodToIntroduction((MethodDec)node); 89 introductions.add(StructureNodeFactory.makeLink(resolvedNode, false)); 90 } else { 91 methods.add(link); 92 } 93 } else if (node instanceof CallExpr) { 94 methodCallSites.add(link); 95 } else if (node instanceof ConstructorDec) { 96 constructors.add(link); 97 } else if (node instanceof NewInstanceExpr) { 98 constructorCallSites.add(link); 99 } else if (node instanceof CatchClause) { 100 handlers.add(link); 101 } else if (node instanceof InitializerDec) { 102 initializers.add(link); 103 } else if (node instanceof FieldDec) { 104 fieldAccesses.add(link); 105 } else if (node instanceof BasicAssignExpr || node instanceof FieldAccessExpr) { 106 fieldAccesses.add(link); 107 } 108 } 109 if (!methods.isEmpty()) relations.add(new RelationNode(METHOD_RELATION, METHOD_RELATION.getForwardNavigationName(), methods)); 110 if (!methodCallSites.isEmpty()) relations.add(new RelationNode(METHOD_RELATION, METHOD_CALL_SITE_RELATION.getForwardNavigationName(), methodCallSites)); 111 if (!constructors.isEmpty()) relations.add(new RelationNode(CONSTRUCTOR_RELATION, CONSTRUCTOR_RELATION.getForwardNavigationName(), constructors)); 112 if (!constructorCallSites.isEmpty()) relations.add(new RelationNode(CONSTRUCTOR_CALL_SITE_RELATION, CONSTRUCTOR_CALL_SITE_RELATION.getForwardNavigationName(), constructorCallSites)); 113 if (!handlers.isEmpty()) relations.add(new RelationNode(HANDLER_RELATION, HANDLER_RELATION.getForwardNavigationName(), handlers)); 114 if (!initializers.isEmpty()) relations.add(new RelationNode(INITIALIZER_RELATION, INITIALIZER_RELATION.getForwardNavigationName(), initializers)); 115 if (!fieldAccesses.isEmpty()) relations.add(new RelationNode(FIELD_ACCESS_RELATION, FIELD_ACCESS_RELATION.getForwardNavigationName(), fieldAccesses)); 116 if (!introductions.isEmpty()) relations.add(new RelationNode(INTRODUCTION_RELATION, INTRODUCTION_RELATION.getForwardNavigationName(), introductions)); 117 } else { 118 if (astObject instanceof IntroducedDec) { 119 Set adviceDecs = resolveAdviceAffectingIntroduction((IntroducedDec)astObject); 120 if (adviceDecs != null) { 121 for (Iterator adIt = adviceDecs.iterator(); adIt.hasNext(); ) { 122 introductions.add(StructureNodeFactory.makeLink((ASTObject)adIt.next(), false)); 123 } 124 } 125 } 126 127 for (Iterator it = backCorrs.iterator(); it.hasNext(); ) { 128 ASTObject node = (ASTObject)it.next(); 129 if (node instanceof AdviceDec) { 130 if (astObject instanceof MethodDec) { 131 methods.add(StructureNodeFactory.makeLink(node, false)); 132 } else if (astObject instanceof CallExpr) { 133 methodCallSites.add(StructureNodeFactory.makeLink(node, false)); 134 } else if (astObject instanceof ConstructorDec) { 135 constructors.add(StructureNodeFactory.makeLink(node, false)); 136 } else if (astObject instanceof NewInstanceExpr) { 137 constructorCallSites.add(StructureNodeFactory.makeLink(node, false)); 138 } else if (astObject instanceof CatchClause) { 139 handlers.add(StructureNodeFactory.makeLink(node, false)); 140 } else if (astObject instanceof InitializerDec) { 141 initializers.add(StructureNodeFactory.makeLink(node, false)); 142 } else if (astObject instanceof FieldDec) { 143 fieldAccesses.add(StructureNodeFactory.makeLink(node, false)); 144 } else if (astObject instanceof BasicAssignExpr 145 || astObject instanceof FieldAccessExpr) { 146 fieldAccesses.add(StructureNodeFactory.makeLink(node, false)); 147 } 148 } 149 } 150 if (!methods.isEmpty()) relations.add(new RelationNode(METHOD_RELATION, METHOD_RELATION.getBackNavigationName(), methods)); 151 if (!methodCallSites.isEmpty()) relations.add(new RelationNode(METHOD_CALL_SITE_RELATION, METHOD_CALL_SITE_RELATION.getBackNavigationName(), methodCallSites)); 152 if (!constructors.isEmpty()) relations.add(new RelationNode(CONSTRUCTOR_RELATION, CONSTRUCTOR_RELATION.getBackNavigationName(), constructors)); 153 if (!constructorCallSites.isEmpty()) relations.add(new RelationNode(CONSTRUCTOR_CALL_SITE_RELATION, CONSTRUCTOR_CALL_SITE_RELATION.getBackNavigationName(), constructorCallSites)); 154 if (!handlers.isEmpty()) relations.add(new RelationNode(HANDLER_RELATION, HANDLER_RELATION.getBackNavigationName(), handlers)); 155 if (!initializers.isEmpty()) relations.add(new RelationNode(INITIALIZER_RELATION, INITIALIZER_RELATION.getBackNavigationName(), initializers)); 156 if (!fieldAccesses.isEmpty()) relations.add(new RelationNode(FIELD_ACCESS_RELATION, FIELD_ACCESS_RELATION.getBackNavigationName(), fieldAccesses)); 157 if (!introductions.isEmpty()) relations.add(new RelationNode(INTRODUCTION_RELATION, INTRODUCTION_RELATION.getBackNavigationName(), introductions)); 158 } 159 return relations; 160 } 161 162 public String getName() { 163 return NAME; 164 } 165 166 169 private ASTObject resolveSyntheticMethodToIntroduction(MethodDec node) { 170 Set backCorrs = StructureModelManager.correspondences.getAffectedBy(node.getDeclaringType().getTypeDec()); 171 Method method = node.getMethod(); 172 String name = method.getDeclaringType().getName() + '.' + method.getName(); 173 for (Iterator it = backCorrs.iterator(); it.hasNext(); ) { 174 Object next = it.next(); 175 if (next instanceof IntroducedDec) { 176 IntroducedDec introducedDec = (IntroducedDec)next; 177 if (name.equals(introducedDec.toShortString())) return introducedDec; 178 } 179 } 180 return node; 181 } 182 183 186 private Set resolveAdviceAffectingIntroduction(IntroducedDec node) { 187 Set forwardCorrs = StructureModelManager.correspondences.getAffects(node); 188 String name = node.getId(); 189 for (Iterator it = forwardCorrs.iterator(); it.hasNext(); ) { 190 Object next = it.next(); 191 if (next instanceof TypeDec) { 192 TypeDec typeDec = (TypeDec)next; 193 List decs = typeDec.getBody().getList(); 194 for (Iterator it2 = decs.iterator(); it2.hasNext(); ) { 195 Dec bodyDec = (Dec)it2.next(); 196 if (bodyDec != null && !(bodyDec instanceof InitializerDec)) { 197 if (bodyDec instanceof MethodDec && bodyDec.isSynthetic() && name.equals(bodyDec.getName())) { 198 MethodDec methodDec = (MethodDec)bodyDec; 199 return StructureModelManager.correspondences.getAffectedBy(methodDec); 200 } 201 } 202 } 203 } 204 } 205 return null; 206 } 207 } 208 209 | Popular Tags |