You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsmElementFormatter.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. * Alexandre Vasseur support for @AJ style
  12. * ******************************************************************/
  13. package org.aspectj.ajdt.internal.core.builder;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.aspectj.ajdt.internal.compiler.ast.AdviceDeclaration;
  17. import org.aspectj.ajdt.internal.compiler.ast.DeclareDeclaration;
  18. import org.aspectj.ajdt.internal.compiler.ast.InterTypeConstructorDeclaration;
  19. import org.aspectj.ajdt.internal.compiler.ast.InterTypeDeclaration;
  20. import org.aspectj.ajdt.internal.compiler.ast.InterTypeFieldDeclaration;
  21. import org.aspectj.ajdt.internal.compiler.ast.InterTypeMethodDeclaration;
  22. import org.aspectj.ajdt.internal.compiler.ast.PointcutDeclaration;
  23. import org.aspectj.ajdt.internal.compiler.lookup.AjLookupEnvironment;
  24. import org.aspectj.asm.IProgramElement;
  25. import org.aspectj.weaver.AdviceKind;
  26. import org.aspectj.weaver.ResolvedTypeX;
  27. import org.aspectj.weaver.TypeX;
  28. import org.aspectj.weaver.patterns.AndPointcut;
  29. import org.aspectj.weaver.patterns.DeclareAnnotation;
  30. import org.aspectj.weaver.patterns.DeclareErrorOrWarning;
  31. import org.aspectj.weaver.patterns.DeclareParents;
  32. import org.aspectj.weaver.patterns.DeclarePrecedence;
  33. import org.aspectj.weaver.patterns.DeclareSoft;
  34. import org.aspectj.weaver.patterns.OrPointcut;
  35. import org.aspectj.weaver.patterns.ReferencePointcut;
  36. import org.aspectj.weaver.patterns.TypePattern;
  37. import org.aspectj.weaver.patterns.TypePatternList;
  38. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Argument;
  39. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
  40. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Annotation;
  41. /**
  42. * @author Mik Kersten
  43. */
  44. public class AsmElementFormatter {
  45. public static final String DECLARE_PRECEDENCE = "precedence";
  46. public static final String DECLARE_SOFT = "soft";
  47. public static final String DECLARE_PARENTS = "parents";
  48. public static final String DECLARE_WARNING = "warning";
  49. public static final String DECLARE_ERROR = "error";
  50. public static final String DECLARE_UNKNONWN = "<unknown declare>";
  51. public static final String POINTCUT_ABSTRACT = "<abstract pointcut>";
  52. public static final String POINTCUT_ANONYMOUS = "<anonymous pointcut>";
  53. public static final int MAX_MESSAGE_LENGTH = 18;
  54. public static final String DEC_LABEL = "declare";
  55. public void genLabelAndKind(MethodDeclaration methodDeclaration, IProgramElement node) {
  56. if (methodDeclaration instanceof AdviceDeclaration) {
  57. AdviceDeclaration ad = (AdviceDeclaration)methodDeclaration;
  58. node.setKind(IProgramElement.Kind.ADVICE);
  59. if (ad.kind == AdviceKind.Around) {
  60. node.setCorrespondingType(ad.returnType.toString()); //returnTypeToString(0));
  61. }
  62. String details = "";
  63. if (ad.pointcutDesignator != null) {
  64. if (ad.pointcutDesignator.getPointcut() instanceof ReferencePointcut) {
  65. ReferencePointcut rp = (ReferencePointcut)ad.pointcutDesignator.getPointcut();
  66. details += rp.name + "..";
  67. } else if (ad.pointcutDesignator.getPointcut() instanceof AndPointcut) {
  68. AndPointcut ap = (AndPointcut)ad.pointcutDesignator.getPointcut();
  69. if (ap.getLeft() instanceof ReferencePointcut) {
  70. details += ap.getLeft().toString() + "..";
  71. } else {
  72. details += POINTCUT_ANONYMOUS + "..";
  73. }
  74. } else if (ad.pointcutDesignator.getPointcut() instanceof OrPointcut) {
  75. OrPointcut op = (OrPointcut)ad.pointcutDesignator.getPointcut();
  76. if (op.getLeft() instanceof ReferencePointcut) {
  77. details += op.getLeft().toString() + "..";
  78. } else {
  79. details += POINTCUT_ANONYMOUS + "..";
  80. }
  81. } else {
  82. details += POINTCUT_ANONYMOUS;
  83. }
  84. } else {
  85. details += POINTCUT_ABSTRACT;
  86. }
  87. node.setName(ad.kind.toString());
  88. node.setDetails(details);
  89. setParameters(methodDeclaration, node);
  90. } else if (methodDeclaration instanceof PointcutDeclaration) {
  91. PointcutDeclaration pd = (PointcutDeclaration)methodDeclaration;
  92. node.setKind(IProgramElement.Kind.POINTCUT);
  93. node.setName(translatePointcutName(new String(methodDeclaration.selector)));
  94. setParameters(methodDeclaration, node);
  95. } else if (methodDeclaration instanceof DeclareDeclaration) {
  96. DeclareDeclaration declare = (DeclareDeclaration)methodDeclaration;
  97. String name = DEC_LABEL + " ";
  98. if (declare.declareDecl instanceof DeclareErrorOrWarning) {
  99. DeclareErrorOrWarning deow = (DeclareErrorOrWarning)declare.declareDecl;
  100. if (deow.isError()) {
  101. node.setKind( IProgramElement.Kind.DECLARE_ERROR);
  102. name += DECLARE_ERROR;
  103. } else {
  104. node.setKind( IProgramElement.Kind.DECLARE_WARNING);
  105. name += DECLARE_WARNING;
  106. }
  107. node.setName(name) ;
  108. node.setDetails("\"" + genDeclareMessage(deow.getMessage()) + "\"");
  109. } else if (declare.declareDecl instanceof DeclareParents) {
  110. node.setKind( IProgramElement.Kind.DECLARE_PARENTS);
  111. DeclareParents dp = (DeclareParents)declare.declareDecl;
  112. node.setName(name + DECLARE_PARENTS);
  113. String kindOfDP = null;
  114. StringBuffer details = new StringBuffer("");
  115. TypePattern[] newParents = dp.getParents().getTypePatterns();
  116. for (int i = 0; i < newParents.length; i++) {
  117. TypePattern tp = newParents[i];
  118. TypeX tx = tp.getExactType();
  119. if (kindOfDP == null) {
  120. kindOfDP = "implements ";
  121. try {
  122. ResolvedTypeX rtx = tx.resolve(((AjLookupEnvironment)declare.scope.environment()).factory.getWorld());
  123. if (!rtx.isInterface()) kindOfDP = "extends ";
  124. } catch (Throwable t) {
  125. // What can go wrong???? who knows!
  126. }
  127. }
  128. String typename= tp.toString();
  129. if (typename.lastIndexOf(".")!=-1) {
  130. typename=typename.substring(typename.lastIndexOf(".")+1);
  131. }
  132. details.append(typename);
  133. if ((i+1)<newParents.length) details.append(",");
  134. }
  135. node.setDetails(kindOfDP+details.toString());
  136. } else if (declare.declareDecl instanceof DeclareSoft) {
  137. node.setKind( IProgramElement.Kind.DECLARE_SOFT);
  138. DeclareSoft ds = (DeclareSoft)declare.declareDecl;
  139. node.setName(name + DECLARE_SOFT);
  140. node.setDetails(genTypePatternLabel(ds.getException()));
  141. } else if (declare.declareDecl instanceof DeclarePrecedence) {
  142. node.setKind( IProgramElement.Kind.DECLARE_PRECEDENCE);
  143. DeclarePrecedence ds = (DeclarePrecedence)declare.declareDecl;
  144. node.setName(name + DECLARE_PRECEDENCE);
  145. node.setDetails(genPrecedenceListLabel(ds.getPatterns()));
  146. } else if (declare.declareDecl instanceof DeclareAnnotation) {
  147. DeclareAnnotation deca = (DeclareAnnotation)declare.declareDecl;
  148. String thekind = deca.getKind().toString();
  149. node.setName(name+"@"+thekind.substring(3));
  150. if (deca.getKind()==DeclareAnnotation.AT_CONSTRUCTOR) {
  151. node.setKind(IProgramElement.Kind.DECLARE_ANNOTATION_AT_CONSTRUCTOR);
  152. } else if (deca.getKind()==DeclareAnnotation.AT_FIELD) {
  153. node.setKind(IProgramElement.Kind.DECLARE_ANNOTATION_AT_FIELD);
  154. } else if (deca.getKind()==DeclareAnnotation.AT_METHOD) {
  155. node.setKind(IProgramElement.Kind.DECLARE_ANNOTATION_AT_METHOD);
  156. } else if (deca.getKind()==DeclareAnnotation.AT_TYPE) {
  157. node.setKind(IProgramElement.Kind.DECLARE_ANNOTATION_AT_TYPE);
  158. }
  159. node.setDetails(genDecaLabel(deca));
  160. } else {
  161. node.setKind(IProgramElement.Kind.ERROR);
  162. node.setName(DECLARE_UNKNONWN);
  163. }
  164. } else if (methodDeclaration instanceof InterTypeDeclaration) {
  165. InterTypeDeclaration itd = (InterTypeDeclaration)methodDeclaration;
  166. String name = itd.onType.toString() + "." + new String(itd.getDeclaredSelector());
  167. if (methodDeclaration instanceof InterTypeFieldDeclaration) {
  168. node.setKind(IProgramElement.Kind.INTER_TYPE_FIELD);
  169. node.setName(name);
  170. } else if (methodDeclaration instanceof InterTypeMethodDeclaration) {
  171. node.setKind(IProgramElement.Kind.INTER_TYPE_METHOD);
  172. node.setName(name);
  173. } else if (methodDeclaration instanceof InterTypeConstructorDeclaration) {
  174. node.setKind(IProgramElement.Kind.INTER_TYPE_CONSTRUCTOR);
  175. // StringBuffer argumentsSignature = new StringBuffer("fubar");
  176. // argumentsSignature.append("(");
  177. // if (methodDeclaration.arguments!=null && methodDeclaration.arguments.length>1) {
  178. //
  179. // for (int i = 1;i<methodDeclaration.arguments.length;i++) {
  180. // argumentsSignature.append(methodDeclaration.arguments[i]);
  181. // if (i+1<methodDeclaration.arguments.length) argumentsSignature.append(",");
  182. // }
  183. // }
  184. // argumentsSignature.append(")");
  185. // InterTypeConstructorDeclaration itcd = (InterTypeConstructorDeclaration)methodDeclaration;
  186. node.setName(itd.onType.toString() + "." + itd.onType.toString()/*+argumentsSignature.toString()*/);
  187. } else {
  188. node.setKind(IProgramElement.Kind.ERROR);
  189. node.setName(name);
  190. }
  191. node.setCorrespondingType(itd.returnType.toString());
  192. if (node.getKind() != IProgramElement.Kind.INTER_TYPE_FIELD) {
  193. setParameters(methodDeclaration, node);
  194. }
  195. } else {
  196. if (methodDeclaration.isConstructor()) {
  197. node.setKind(IProgramElement.Kind.CONSTRUCTOR);
  198. } else {
  199. node.setKind(IProgramElement.Kind.METHOD);
  200. //TODO AV - could speed up if we could dig only for @Aspect declaring types (or aspect if mixed style allowed)
  201. //??? how to : node.getParent().getKind().equals(IProgramElement.Kind.ASPECT)) {
  202. if (true && methodDeclaration.annotations != null) {
  203. for (int i = 0; i < methodDeclaration.annotations.length; i++) {
  204. //Note: AV: implicit single advice type support here (should be enforced somewhere as well (APT etc))
  205. Annotation annotation = methodDeclaration.annotations[i];
  206. String annotationSig = new String(annotation.type.getTypeBindingPublic(methodDeclaration.scope).signature());
  207. if ("Lorg/aspectj/lang/annotation/Pointcut;".equals(annotationSig)) {
  208. node.setKind(IProgramElement.Kind.POINTCUT);
  209. break;
  210. } else if ("Lorg/aspectj/lang/annotation/Before;".equals(annotationSig)
  211. || "Lorg/aspectj/lang/annotation/After;".equals(annotationSig)
  212. || "Lorg/aspectj/lang/annotation/AfterReturning;".equals(annotationSig)
  213. || "Lorg/aspectj/lang/annotation/AfterThrowing;".equals(annotationSig)
  214. || "Lorg/aspectj/lang/annotation/Around;".equals(annotationSig)) {
  215. node.setKind(IProgramElement.Kind.ADVICE);
  216. //TODO AV - all are considered anonymous - is that ok?
  217. node.setDetails(POINTCUT_ANONYMOUS);
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. node.setName(new String(methodDeclaration.selector));
  224. setParameters(methodDeclaration, node);
  225. }
  226. }
  227. private String genDecaLabel(DeclareAnnotation deca) {
  228. StringBuffer sb = new StringBuffer("");
  229. sb.append(deca.getPatternAsString());
  230. sb.append(" : ");
  231. sb.append(deca.getAnnotationString());
  232. return sb.toString();
  233. }
  234. private String genPrecedenceListLabel(TypePatternList list) {
  235. String tpList = "";
  236. for (int i = 0; i < list.size(); i++) {
  237. tpList += genTypePatternLabel(list.get(i));
  238. if (i < list.size()-1) tpList += ", ";
  239. }
  240. return tpList;
  241. }
  242. // private String genArguments(MethodDeclaration md) {
  243. // String args = "";
  244. // Argument[] argArray = md.arguments;
  245. // if (argArray == null) return args;
  246. // for (int i = 0; i < argArray.length; i++) {
  247. // String argName = new String(argArray[i].name);
  248. // String argType = argArray[i].type.toString();
  249. // if (acceptArgument(argName, argType)) {
  250. // args += argType + ", ";
  251. // }
  252. // }
  253. // int lastSepIndex = args.lastIndexOf(',');
  254. // if (lastSepIndex != -1 && args.endsWith(", ")) args = args.substring(0, lastSepIndex);
  255. // return args;
  256. // }
  257. private void setParameters(MethodDeclaration md, IProgramElement pe) {
  258. Argument[] argArray = md.arguments;
  259. List names = new ArrayList();
  260. List types = new ArrayList();
  261. pe.setParameterNames(names);
  262. pe.setParameterTypes(types);
  263. if (argArray == null) return;
  264. for (int i = 0; i < argArray.length; i++) {
  265. String argName = new String(argArray[i].name);
  266. String argType = argArray[i].type.toString();
  267. if (acceptArgument(argName, argType)) {
  268. names.add(argName);
  269. types.add(argType);
  270. }
  271. }
  272. }
  273. // TODO: fix this way of determing ajc-added arguments, make subtype of Argument with extra info
  274. private boolean acceptArgument(String name, String type) {
  275. return !name.startsWith("ajc$this_")
  276. && !type.equals("org.aspectj.lang.JoinPoint.StaticPart")
  277. && !type.equals("org.aspectj.lang.JoinPoint")
  278. && !type.equals("org.aspectj.runtime.internal.AroundClosure");
  279. }
  280. public String genTypePatternLabel(TypePattern tp) {
  281. final String TYPE_PATTERN_LITERAL = "<type pattern>";
  282. String label;
  283. TypeX typeX = tp.getExactType();
  284. if (typeX != ResolvedTypeX.MISSING) {
  285. label = typeX.getName();
  286. if (tp.isIncludeSubtypes()) label += "+";
  287. } else {
  288. label = TYPE_PATTERN_LITERAL;
  289. }
  290. return label;
  291. }
  292. public String genDeclareMessage(String message) {
  293. int length = message.length();
  294. if (length < MAX_MESSAGE_LENGTH) {
  295. return message;
  296. } else {
  297. return message.substring(0, MAX_MESSAGE_LENGTH-1) + "..";
  298. }
  299. }
  300. // // TODO:
  301. // private String translateAdviceName(String label) {
  302. // if (label.indexOf("before") != -1) return "before";
  303. // if (label.indexOf("returning") != -1) return "after returning";
  304. // if (label.indexOf("after") != -1) return "after";
  305. // if (label.indexOf("around") != -1) return "around";
  306. // else return "<advice>";
  307. // }
  308. // // !!! move or replace
  309. // private String translateDeclareName(String name) {
  310. // int colonIndex = name.indexOf(":");
  311. // if (colonIndex != -1) {
  312. // return name.substring(0, colonIndex);
  313. // } else {
  314. // return name;
  315. // }
  316. // }
  317. // !!! move or replace
  318. // private String translateInterTypeDecName(String name) {
  319. // int index = name.lastIndexOf('$');
  320. // if (index != -1) {
  321. // return name.substring(index+1);
  322. // } else {
  323. // return name;
  324. // }
  325. // }
  326. // !!! move or replace
  327. private String translatePointcutName(String name) {
  328. int index = name.indexOf("$$")+2;
  329. int endIndex = name.lastIndexOf('$');
  330. if (index != -1 && endIndex != -1) {
  331. return name.substring(index, endIndex);
  332. } else {
  333. return name;
  334. }
  335. }
  336. }