Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AsmRelationshipProvider.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.util.ArrayList;
  14. import java.util.Iterator;
  15. import org.aspectj.asm.*;
  16. import org.aspectj.asm.internal.AspectJElementHierarchy;
  17. import org.aspectj.asm.internal.ProgramElement;
  18. import org.aspectj.bridge.ISourceLocation;
  19. import org.aspectj.bridge.SourceLocation;
  20. import org.aspectj.weaver.bcel.BcelAdvice;
  21. public class AsmRelationshipProvider {
  22. public static final String ADVISES = "advises";
  23. public static final String ADVISED_BY = "advised by";
  24. public static final String DECLARES_ON = "declares on";
  25. public static final String DECLAREDY_BY = "declared by";
  26. public static final String MATCHED_BY = "matched by";
  27. public static final String MATCHES_DECLARE = "matches declare";
  28. public static final String INTER_TYPE_DECLARES = "declared on";
  29. public static final String INTER_TYPE_DECLARED_BY = "aspect declarations";
  30. public static void checkerMunger(IHierarchy model, Shadow shadow, Checker checker) {
  31. if (shadow.getSourceLocation() == null || checker.getSourceLocation() == null) return;
  32. String sourceHandle = ProgramElement.createHandleIdentifier(
  33. checker.getSourceLocation().getSourceFile(),
  34. checker.getSourceLocation().getLine(),
  35. checker.getSourceLocation().getColumn());
  36. String targetHandle = ProgramElement.createHandleIdentifier(
  37. shadow.getSourceLocation().getSourceFile(),
  38. shadow.getSourceLocation().getLine(),
  39. shadow.getSourceLocation().getColumn());
  40. IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
  41. if (sourceHandle != null && targetHandle != null) {
  42. IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE, MATCHED_BY,false,true);
  43. foreward.addTarget(targetHandle);
  44. // foreward.getTargets().add(targetHandle);
  45. IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE, MATCHES_DECLARE,false,true);
  46. if (back != null && back.getTargets() != null) {
  47. back.addTarget(sourceHandle);
  48. //back.getTargets().add(sourceHandle);
  49. }
  50. }
  51. }
  52. // For ITDs
  53. public static void addRelationship(
  54. ResolvedTypeX onType,
  55. ResolvedTypeMunger munger,
  56. ResolvedTypeX originatingAspect) {
  57. String sourceHandle = "";
  58. if (munger.getSourceLocation()!=null) {
  59. sourceHandle = ProgramElement.createHandleIdentifier(
  60. munger.getSourceLocation().getSourceFile(),
  61. munger.getSourceLocation().getLine(),
  62. munger.getSourceLocation().getColumn());
  63. } else {
  64. sourceHandle = ProgramElement.createHandleIdentifier(
  65. originatingAspect.getSourceLocation().getSourceFile(),
  66. originatingAspect.getSourceLocation().getLine(),
  67. originatingAspect.getSourceLocation().getColumn());
  68. }
  69. if (originatingAspect.getSourceLocation() != null) {
  70. String targetHandle = ProgramElement.createHandleIdentifier(
  71. onType.getSourceLocation().getSourceFile(),
  72. onType.getSourceLocation().getLine(),
  73. onType.getSourceLocation().getColumn());
  74. IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
  75. if (sourceHandle != null && targetHandle != null) {
  76. IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARES,false,true);
  77. foreward.addTarget(targetHandle);
  78. // foreward.getTargets().add(targetHandle);
  79. IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARED_BY,false,true);
  80. back.addTarget(sourceHandle);
  81. // back.getTargets().add(sourceHandle);
  82. }
  83. }
  84. }
  85. public static void addDeclareParentsRelationship(ISourceLocation decp,ResolvedTypeX targetType) {
  86. String sourceHandle = ProgramElement.createHandleIdentifier(decp.getSourceFile(),decp.getLine(),decp.getColumn());
  87. IProgramElement ipe = AsmManager.getDefault().getHierarchy().findElementForHandle(sourceHandle);
  88. String targetHandle = ProgramElement.createHandleIdentifier(
  89. targetType.getSourceLocation().getSourceFile(),
  90. targetType.getSourceLocation().getLine(),
  91. targetType.getSourceLocation().getColumn());
  92. IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
  93. if (sourceHandle != null && targetHandle != null) {
  94. IRelationship foreward = mapper.get(sourceHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARES,false,true);
  95. foreward.addTarget(targetHandle);
  96. IRelationship back = mapper.get(targetHandle, IRelationship.Kind.DECLARE_INTER_TYPE, INTER_TYPE_DECLARED_BY,false,true);
  97. back.addTarget(sourceHandle);
  98. }
  99. }
  100. public static void adviceMunger(IHierarchy model, Shadow shadow, ShadowMunger munger) {
  101. if (munger instanceof Advice) {
  102. Advice advice = (Advice)munger;
  103. if (advice.getKind().isPerEntry() || advice.getKind().isCflow()) {
  104. // TODO: might want to show these in the future
  105. return;
  106. }
  107. IRelationshipMap mapper = AsmManager.getDefault().getRelationshipMap();
  108. IProgramElement targetNode = getNode(AsmManager.getDefault().getHierarchy(), shadow);
  109. boolean runtimeTest = ((BcelAdvice)munger).hasDynamicTests();
  110. // Work out extra info to inform interested UIs !
  111. IProgramElement.ExtraInformation ai = new IProgramElement.ExtraInformation();
  112. String adviceHandle = advice.getHandle();
  113. // What kind of advice is it?
  114. // TODO: Prob a better way to do this but I just want to
  115. // get it into CVS !!!
  116. AdviceKind ak = ((Advice)munger).getKind();
  117. ai.setExtraAdviceInformation(ak.getName());
  118. IProgramElement adviceElement = AsmManager.getDefault().getHierarchy().findElementForHandle(adviceHandle);
  119. adviceElement.setExtraInfo(ai);
  120. if (adviceHandle != null && targetNode != null) {
  121. if (targetNode != null) {
  122. String targetHandle = targetNode.getHandleIdentifier();
  123. IRelationship foreward = mapper.get(adviceHandle, IRelationship.Kind.ADVICE, ADVISES,runtimeTest,true);
  124. if (foreward != null) foreward.addTarget(targetHandle);//foreward.getTargets().add(targetHandle);
  125. IRelationship back = mapper.get(targetHandle, IRelationship.Kind.ADVICE, ADVISED_BY,runtimeTest,true);
  126. if (back != null) back.addTarget(adviceHandle);//back.getTargets().add(adviceHandle);
  127. }
  128. }
  129. }
  130. }
  131. private static IProgramElement getNode(IHierarchy model, Shadow shadow) {
  132. Member enclosingMember = shadow.getEnclosingCodeSignature();
  133. IProgramElement enclosingNode = lookupMember(model, enclosingMember);
  134. if (enclosingNode == null) {
  135. Lint.Kind err = shadow.getIWorld().getLint().shadowNotInStructure;
  136. if (err.isEnabled()) {
  137. err.signal(shadow.toString(), shadow.getSourceLocation());
  138. }
  139. return null;
  140. }
  141. Member shadowSig = shadow.getSignature();
  142. if (!shadowSig.equals(enclosingMember)) {
  143. IProgramElement bodyNode = findOrCreateCodeNode(enclosingNode, shadowSig, shadow);
  144. return bodyNode;
  145. } else {
  146. return enclosingNode;
  147. }
  148. }
  149. private static boolean sourceLinesMatch(ISourceLocation loc1,ISourceLocation loc2) {
  150. if (loc1.getLine()!=loc2.getLine()) return false;
  151. return true;
  152. }
  153. private static IProgramElement findOrCreateCodeNode(IProgramElement enclosingNode, Member shadowSig, Shadow shadow)
  154. {
  155. for (Iterator it = enclosingNode.getChildren().iterator(); it.hasNext(); ) {
  156. IProgramElement node = (IProgramElement)it.next();
  157. if (shadowSig.getName().equals(node.getBytecodeName()) &&
  158. shadowSig.getSignature().equals(node.getBytecodeSignature()) &&
  159. sourceLinesMatch(node.getSourceLocation(),shadow.getSourceLocation()))
  160. {
  161. return node;
  162. }
  163. }
  164. ISourceLocation sl = shadow.getSourceLocation();
  165. IProgramElement peNode = new ProgramElement(
  166. shadow.toString(),
  167. IProgramElement.Kind.CODE,
  168. //XXX why not use shadow file? new SourceLocation(sl.getSourceFile(), sl.getLine()),
  169. new SourceLocation(enclosingNode.getSourceLocation().getSourceFile(), sl.getLine()),
  170. 0,
  171. "",
  172. new ArrayList());
  173. peNode.setBytecodeName(shadowSig.getName());
  174. peNode.setBytecodeSignature(shadowSig.getSignature());
  175. enclosingNode.addChild(peNode);
  176. return peNode;
  177. }
  178. private static IProgramElement lookupMember(IHierarchy model, Member member) {
  179. TypeX declaringType = member.getDeclaringType();
  180. IProgramElement classNode =
  181. model.findElementForType(declaringType.getPackageName(), declaringType.getClassName());
  182. return findMemberInClass(classNode, member);
  183. }
  184. private static IProgramElement findMemberInClass(
  185. IProgramElement classNode,
  186. Member member)
  187. {
  188. if (classNode == null) return null; // XXX remove this check
  189. for (Iterator it = classNode.getChildren().iterator(); it.hasNext(); ) {
  190. IProgramElement node = (IProgramElement)it.next();
  191. if (member.getName().equals(node.getBytecodeName()) &&
  192. member.getSignature().equals(node.getBytecodeSignature()))
  193. {
  194. return node;
  195. }
  196. }
  197. // if we can't find the member, we'll just put it in the class
  198. return classNode;
  199. }
  200. // private static IProgramElement.Kind genShadowKind(Shadow shadow) {
  201. // IProgramElement.Kind shadowKind;
  202. // if (shadow.getKind() == Shadow.MethodCall
  203. // || shadow.getKind() == Shadow.ConstructorCall
  204. // || shadow.getKind() == Shadow.FieldGet
  205. // || shadow.getKind() == Shadow.FieldSet
  206. // || shadow.getKind() == Shadow.ExceptionHandler) {
  207. // return IProgramElement.Kind.CODE;
  208. //
  209. // } else if (shadow.getKind() == Shadow.MethodExecution) {
  210. // return IProgramElement.Kind.METHOD;
  211. //
  212. // } else if (shadow.getKind() == Shadow.ConstructorExecution) {
  213. // return IProgramElement.Kind.CONSTRUCTOR;
  214. //
  215. // } else if (shadow.getKind() == Shadow.PreInitialization
  216. // || shadow.getKind() == Shadow.Initialization) {
  217. // return IProgramElement.Kind.CLASS;
  218. //
  219. // } else if (shadow.getKind() == Shadow.AdviceExecution) {
  220. // return IProgramElement.Kind.ADVICE;
  221. //
  222. // } else {
  223. // return IProgramElement.Kind.ERROR;
  224. // }
  225. // }
  226. }