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.

JDTLikeHandleProvider.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /********************************************************************
  2. * Copyright (c) 2006 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * under the terms of the Eclipse Public License v 2.0
  5. * which accompanies this distribution and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  7. *
  8. * Contributors: IBM Corporation - initial API and implementation
  9. * Helen Hawkins - initial version
  10. *******************************************************************/
  11. package org.aspectj.asm.internal;
  12. import java.io.File;
  13. import java.util.List;
  14. import org.aspectj.asm.AsmManager;
  15. import org.aspectj.asm.IElementHandleProvider;
  16. import org.aspectj.asm.IProgramElement;
  17. import org.aspectj.bridge.ISourceLocation;
  18. /**
  19. * Creates JDT-like handles, for example
  20. * <ul>
  21. * <li>method with string argument: {@code <tjp}<code>{</code>{@code Demo.java[Demo~main~\[QString;}</li>
  22. * <li>method with generic argument: {@code <pkg}<code>{</code>{@code MyClass.java[MyClass~myMethod~QList\<QString;>;}</li>
  23. * <li>aspect: {@code <pkg*A1.aj}<code>}</code>{@code A1}</li>
  24. * <li>advice with Integer arg: {@code <pkg*A8.aj}<code>}</code>{@code A8&afterReturning&QInteger;}</li>
  25. * <li>method call: {@code <pkg*A10.aj[C~m1?method-call(void pkg.C.m2())}</li>
  26. * </ul>
  27. */
  28. public class JDTLikeHandleProvider implements IElementHandleProvider {
  29. private final AsmManager asm;
  30. private static final char[] empty = new char[] {};
  31. private static final char[] countDelim = new char[] { HandleProviderDelimiter.COUNT.getDelimiter() };
  32. private static final String backslash = "\\";
  33. private static final String emptyString = "";
  34. public JDTLikeHandleProvider(AsmManager asm) {
  35. this.asm = asm;
  36. }
  37. public void initialize() {
  38. // nothing to do
  39. }
  40. public String createHandleIdentifier(IProgramElement ipe) {
  41. // AjBuildManager.setupModel --> top of the tree is either
  42. // <root> or the .lst file
  43. if (ipe == null || (ipe.getKind().equals(IProgramElement.Kind.FILE_JAVA) && ipe.getName().equals("<root>"))) {
  44. return "";
  45. } else if (ipe.getHandleIdentifier(false) != null) {
  46. // have already created the handle for this ipe
  47. // therefore just return it
  48. return ipe.getHandleIdentifier();
  49. } else if (ipe.getKind().equals(IProgramElement.Kind.FILE_LST)) {
  50. String configFile = asm.getHierarchy().getConfigFile();
  51. int start = configFile.lastIndexOf(File.separator);
  52. int end = configFile.lastIndexOf(".lst");
  53. if (end != -1) {
  54. configFile = configFile.substring(start + 1, end);
  55. } else {
  56. configFile = new StringBuilder("=").append(configFile.substring(start + 1)).toString();
  57. }
  58. ipe.setHandleIdentifier(configFile);
  59. return configFile;
  60. } else if (ipe.getKind() == IProgramElement.Kind.SOURCE_FOLDER) {
  61. StringBuilder sb = new StringBuilder();
  62. sb.append(createHandleIdentifier(ipe.getParent())).append("/");
  63. // pr249216 - escape any embedded slashes
  64. String folder = ipe.getName();
  65. if (folder.endsWith("/")) {
  66. folder = folder.substring(0, folder.length() - 1);
  67. }
  68. if (folder.contains("/")) {
  69. folder = folder.replace("/", "\\/");
  70. }
  71. sb.append(folder);
  72. String handle = sb.toString();
  73. ipe.setHandleIdentifier(handle);
  74. return handle;
  75. }
  76. IProgramElement parent = ipe.getParent();
  77. if (parent != null && parent.getKind().equals(IProgramElement.Kind.IMPORT_REFERENCE)) {
  78. // want to miss out '#import declaration' in the handle
  79. parent = ipe.getParent().getParent();
  80. }
  81. StringBuilder handle = new StringBuilder();
  82. // add the handle for the parent
  83. handle.append(createHandleIdentifier(parent));
  84. // add the correct delimiter for this ipe
  85. handle.append(HandleProviderDelimiter.getDelimiter(ipe));
  86. // add the name and any parameters unless we're an initializer
  87. // (initializer's names are '...')
  88. if (!ipe.getKind().equals(IProgramElement.Kind.INITIALIZER)) {
  89. if (ipe.getKind() == IProgramElement.Kind.CLASS && ipe.getName().endsWith("{..}")) {
  90. // format: 'new Runnable() {..}' but its anon-y-mouse
  91. // dont append anything, there may be a count to follow though (!<n>)
  92. } else {
  93. if (ipe.getKind() == IProgramElement.Kind.INTER_TYPE_CONSTRUCTOR) {
  94. handle.append(ipe.getName()).append("_new").append(getParameters(ipe));
  95. } else {
  96. // if (ipe.getKind() == IProgramElement.Kind.PACKAGE && ipe.getName().equals("DEFAULT")) {
  97. // // the delimiter will be in there, but skip the word DEFAULT as it is just a placeholder
  98. // } else {
  99. if (ipe.getKind().isDeclareAnnotation()) {
  100. // escape the @ (pr249216c9)
  101. handle.append("declare \\@").append(ipe.getName().substring(9)).append(getParameters(ipe));
  102. } else {
  103. if (ipe.getFullyQualifiedName() != null) {
  104. handle.append(ipe.getFullyQualifiedName());
  105. } else {
  106. handle.append(ipe.getName());
  107. }
  108. handle.append(getParameters(ipe));
  109. }
  110. }
  111. // }
  112. }
  113. }
  114. // add the count, for example '!2' if its the second ipe of its
  115. // kind in the aspect
  116. handle.append(getCount(ipe));
  117. ipe.setHandleIdentifier(handle.toString());
  118. return handle.toString();
  119. }
  120. private String getParameters(IProgramElement ipe) {
  121. if (ipe.getParameterSignatures() == null || ipe.getParameterSignatures().isEmpty()) {
  122. return "";
  123. }
  124. List<String> sourceRefs = ipe.getParameterSignaturesSourceRefs();
  125. List<char[]> parameterTypes = ipe.getParameterSignatures();
  126. StringBuilder sb = new StringBuilder();
  127. if (sourceRefs != null) {
  128. for (String sourceRef : sourceRefs) {
  129. sb.append(HandleProviderDelimiter.getDelimiter(ipe));
  130. sb.append(sourceRef);
  131. }
  132. } else {
  133. for (char[] element : parameterTypes) {
  134. sb.append(HandleProviderDelimiter.getDelimiter(ipe));
  135. sb.append(NameConvertor.createShortName(element, false, false));
  136. }
  137. }
  138. return sb.toString();
  139. }
  140. /**
  141. * Determine a count to be suffixed to the handle, this is only necessary for identical looking entries at the same level in the
  142. * model (for example two anonymous class declarations). The format is !<n> where n will be greater than 2.
  143. *
  144. * @param ipe the program element for which the handle is being constructed
  145. * @return a char suffix that will either be empty or of the form "!<n>"
  146. */
  147. private char[] getCount(IProgramElement ipe) {
  148. // TODO could optimize this code
  149. char[] byteCodeName = ipe.getBytecodeName().toCharArray();
  150. if (ipe.getKind().isInterTypeMember()) {
  151. int count = 1;
  152. List<IProgramElement> kids = ipe.getParent().getChildren();
  153. for (IProgramElement object : kids) {
  154. if (object.equals(ipe)) {
  155. break;
  156. }
  157. if (object.getKind().isInterTypeMember()) {
  158. if (object.getName().equals(ipe.getName()) && getParameters(object).equals(getParameters(ipe))) {
  159. String existingHandle = object.getHandleIdentifier();
  160. int suffixPosition = existingHandle.indexOf('!');
  161. if (suffixPosition != -1) {
  162. count = Integer.parseInt(existingHandle.substring(suffixPosition + 1)) + 1;
  163. } else {
  164. if (count == 1) {
  165. count = 2;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. if (count > 1) {
  172. return CharOperation.concat(countDelim, Integer.toString(count).toCharArray());
  173. }
  174. } else if (ipe.getKind().isDeclare()) {
  175. // // look at peer declares
  176. int count = computeCountBasedOnPeers(ipe);
  177. if (count > 1) {
  178. return CharOperation.concat(countDelim, Integer.toString(count).toCharArray());
  179. }
  180. } else if (ipe.getKind().equals(IProgramElement.Kind.ADVICE)) {
  181. // Look at any peer advice
  182. int count = 1;
  183. List<IProgramElement> kids = ipe.getParent().getChildren();
  184. String ipeSig = ipe.getBytecodeSignature();
  185. // remove return type from the signature - it should not be included in the comparison
  186. int idx = 0;
  187. ipeSig = shortenIpeSig(ipeSig);
  188. for (IProgramElement object : kids) {
  189. if (object.equals(ipe)) {
  190. break;
  191. }
  192. if (object.getKind() == ipe.getKind()) {
  193. if (object.getName().equals(ipe.getName())) {
  194. String sig1 = object.getBytecodeSignature();
  195. if (sig1 != null && (idx = sig1.indexOf(")")) != -1) {
  196. sig1 = sig1.substring(0, idx);
  197. }
  198. // this code needs a speed overhaul... and some proper tests
  199. // Two static parts because one may be enclosing jpsp (269522)
  200. if (sig1 != null) {
  201. if (sig1.contains("Lorg/aspectj/lang")) {
  202. if (sig1.endsWith("Lorg/aspectj/lang/JoinPoint$StaticPart;")) {
  203. sig1 = sig1.substring(0, sig1.lastIndexOf("Lorg/aspectj/lang/JoinPoint$StaticPart;"));
  204. }
  205. if (sig1.endsWith("Lorg/aspectj/lang/JoinPoint;")) {
  206. sig1 = sig1.substring(0, sig1.lastIndexOf("Lorg/aspectj/lang/JoinPoint;"));
  207. }
  208. if (sig1.endsWith("Lorg/aspectj/lang/JoinPoint$StaticPart;")) {
  209. sig1 = sig1.substring(0, sig1.lastIndexOf("Lorg/aspectj/lang/JoinPoint$StaticPart;"));
  210. }
  211. }
  212. }
  213. if (sig1 == null && ipeSig == null || (sig1 != null && sig1.equals(ipeSig))) {
  214. String existingHandle = object.getHandleIdentifier();
  215. int suffixPosition = existingHandle.indexOf('!');
  216. if (suffixPosition != -1) {
  217. count = Integer.parseInt(existingHandle.substring(suffixPosition + 1)) + 1;
  218. } else {
  219. if (count == 1) {
  220. count = 2;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. if (count > 1) {
  228. return CharOperation.concat(countDelim, Integer.toString(count).toCharArray());
  229. }
  230. } else if (ipe.getKind().equals(IProgramElement.Kind.INITIALIZER)) {
  231. // return String.valueOf(++initializerCounter).toCharArray();
  232. // Look at any peer advice
  233. int count = 1;
  234. List<IProgramElement> kids = ipe.getParent().getChildren();
  235. String ipeSig = ipe.getBytecodeSignature();
  236. // remove return type from the signature - it should not be included in the comparison
  237. int idx = 0;
  238. ipeSig = shortenIpeSig(ipeSig);
  239. for (IProgramElement object : kids) {
  240. if (object.equals(ipe)) {
  241. break;
  242. }
  243. if (object.getKind() == ipe.getKind()) {
  244. if (object.getName().equals(ipe.getName())) {
  245. String sig1 = object.getBytecodeSignature();
  246. if (sig1 != null && (idx = sig1.indexOf(")")) != -1) {
  247. sig1 = sig1.substring(0, idx);
  248. }
  249. // this code needs a speed overhaul... and some proper tests
  250. // Two static parts because one may be enclosing jpsp (269522)
  251. if (sig1 != null) {
  252. if (sig1.contains("Lorg/aspectj/lang")) {
  253. if (sig1.endsWith("Lorg/aspectj/lang/JoinPoint$StaticPart;")) {
  254. sig1 = sig1.substring(0, sig1.lastIndexOf("Lorg/aspectj/lang/JoinPoint$StaticPart;"));
  255. }
  256. if (sig1.endsWith("Lorg/aspectj/lang/JoinPoint;")) {
  257. sig1 = sig1.substring(0, sig1.lastIndexOf("Lorg/aspectj/lang/JoinPoint;"));
  258. }
  259. if (sig1.endsWith("Lorg/aspectj/lang/JoinPoint$StaticPart;")) {
  260. sig1 = sig1.substring(0, sig1.lastIndexOf("Lorg/aspectj/lang/JoinPoint$StaticPart;"));
  261. }
  262. }
  263. }
  264. if (sig1 == null && ipeSig == null || (sig1 != null && sig1.equals(ipeSig))) {
  265. String existingHandle = object.getHandleIdentifier();
  266. int suffixPosition = existingHandle.indexOf('!');
  267. if (suffixPosition != -1) {
  268. count = Integer.parseInt(existingHandle.substring(suffixPosition + 1)) + 1;
  269. } else {
  270. if (count == 1) {
  271. count = 2;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. // if (count > 1) {
  279. return Integer.toString(count).toCharArray();
  280. // return CharOperation.concat(countDelim, new Integer(count).toString().toCharArray());
  281. // }
  282. } else if (ipe.getKind().equals(IProgramElement.Kind.CODE)) {
  283. int index = CharOperation.lastIndexOf('!', byteCodeName);
  284. if (index != -1) {
  285. return convertCount(CharOperation.subarray(byteCodeName, index + 1, byteCodeName.length));
  286. }
  287. } else if (ipe.getKind() == IProgramElement.Kind.CLASS) {
  288. // depends on previous children
  289. int count = 1;
  290. List<IProgramElement> kids = ipe.getParent().getChildren();
  291. if (ipe.getName().endsWith("{..}")) {
  292. // only depends on previous anonymous children, name irrelevant
  293. for (IProgramElement object : kids) {
  294. if (object.equals(ipe)) {
  295. break;
  296. }
  297. if (object.getKind() == ipe.getKind()) {
  298. if (object.getName().endsWith("{..}")) {
  299. String existingHandle = object.getHandleIdentifier();
  300. int suffixPosition = existingHandle.lastIndexOf('!');
  301. int lastSquareBracket = existingHandle.lastIndexOf('['); // type delimiter
  302. if (suffixPosition != -1 && lastSquareBracket < suffixPosition) { // pr260384
  303. count = Integer.parseInt(existingHandle.substring(suffixPosition + 1)) + 1;
  304. } else {
  305. if (count == 1) {
  306. count = 2;
  307. }
  308. }
  309. }
  310. }
  311. }
  312. } else {
  313. for (IProgramElement object : kids) {
  314. if (object.equals(ipe)) {
  315. break;
  316. }
  317. if (object.getKind() == ipe.getKind()) {
  318. if (object.getName().equals(ipe.getName())) {
  319. String existingHandle = object.getHandleIdentifier();
  320. int suffixPosition = existingHandle.lastIndexOf('!');
  321. int lastSquareBracket = existingHandle.lastIndexOf('['); // type delimiter
  322. if (suffixPosition != -1 && lastSquareBracket < suffixPosition) { // pr260384
  323. count = Integer.parseInt(existingHandle.substring(suffixPosition + 1)) + 1;
  324. } else {
  325. if (count == 1) {
  326. count = 2;
  327. }
  328. }
  329. }
  330. }
  331. }
  332. }
  333. if (count > 1) {
  334. return CharOperation.concat(countDelim, Integer.toString(count).toCharArray());
  335. }
  336. }
  337. return empty;
  338. }
  339. private String shortenIpeSig(String ipeSig) {
  340. int idx;
  341. if (ipeSig != null && ((idx = ipeSig.indexOf(")")) != -1)) {
  342. ipeSig = ipeSig.substring(0, idx);
  343. }
  344. if (ipeSig != null) {
  345. if (ipeSig.contains("Lorg/aspectj/lang")) {
  346. if (ipeSig.endsWith("Lorg/aspectj/lang/JoinPoint$StaticPart;")) {
  347. ipeSig = ipeSig.substring(0, ipeSig.lastIndexOf("Lorg/aspectj/lang/JoinPoint$StaticPart;"));
  348. }
  349. if (ipeSig.endsWith("Lorg/aspectj/lang/JoinPoint;")) {
  350. ipeSig = ipeSig.substring(0, ipeSig.lastIndexOf("Lorg/aspectj/lang/JoinPoint;"));
  351. }
  352. if (ipeSig.endsWith("Lorg/aspectj/lang/JoinPoint$StaticPart;")) {
  353. ipeSig = ipeSig.substring(0, ipeSig.lastIndexOf("Lorg/aspectj/lang/JoinPoint$StaticPart;"));
  354. }
  355. }
  356. }
  357. return ipeSig;
  358. }
  359. private int computeCountBasedOnPeers(IProgramElement ipe) {
  360. int count = 1;
  361. for (IProgramElement object : ipe.getParent().getChildren()) {
  362. if (object.equals(ipe)) {
  363. break;
  364. }
  365. if (object.getKind() == ipe.getKind()) {
  366. if (object.getKind().toString().equals(ipe.getKind().toString())) {
  367. String existingHandle = object.getHandleIdentifier();
  368. int suffixPosition = existingHandle.indexOf('!');
  369. if (suffixPosition != -1) {
  370. count = Integer.parseInt(existingHandle.substring(suffixPosition + 1)) + 1;
  371. } else {
  372. if (count == 1) {
  373. count = 2;
  374. }
  375. }
  376. }
  377. }
  378. }
  379. return count;
  380. }
  381. /**
  382. * Only returns the count if it's not equal to 1
  383. */
  384. private char[] convertCount(char[] c) {
  385. if ((c.length == 1 && c[0] != ' ' && c[0] != '1') || c.length > 1) {
  386. return CharOperation.concat(countDelim, c);
  387. }
  388. return empty;
  389. }
  390. public String getFileForHandle(String handle) {
  391. IProgramElement node = asm.getHierarchy().getElement(handle);
  392. if (node != null) {
  393. return asm.getCanonicalFilePath(node.getSourceLocation().getSourceFile());
  394. } else if (handle.charAt(0) == HandleProviderDelimiter.ASPECT_CU.getDelimiter()
  395. || handle.charAt(0) == HandleProviderDelimiter.COMPILATIONUNIT.getDelimiter()) {
  396. // it's something like *MyAspect.aj or {MyClass.java. In other words
  397. // it's a file node that's been created with no children and no
  398. // parent
  399. return backslash + handle.substring(1);
  400. }
  401. return emptyString;
  402. }
  403. public int getLineNumberForHandle(String handle) {
  404. IProgramElement node = asm.getHierarchy().getElement(handle);
  405. if (node != null) {
  406. return node.getSourceLocation().getLine();
  407. } else if (handle.charAt(0) == HandleProviderDelimiter.ASPECT_CU.getDelimiter()
  408. || handle.charAt(0) == HandleProviderDelimiter.COMPILATIONUNIT.getDelimiter()) {
  409. // it's something like *MyAspect.aj or {MyClass.java. In other words
  410. // it's a file node that's been created with no children and no
  411. // parent
  412. return 1;
  413. }
  414. return -1;
  415. }
  416. public int getOffSetForHandle(String handle) {
  417. IProgramElement node = asm.getHierarchy().getElement(handle);
  418. if (node != null) {
  419. return node.getSourceLocation().getOffset();
  420. } else if (handle.charAt(0) == HandleProviderDelimiter.ASPECT_CU.getDelimiter()
  421. || handle.charAt(0) == HandleProviderDelimiter.COMPILATIONUNIT.getDelimiter()) {
  422. // it's something like *MyAspect.aj or {MyClass.java. In other words
  423. // it's a file node that's been created with no children and no
  424. // parent
  425. return 0;
  426. }
  427. return -1;
  428. }
  429. public String createHandleIdentifier(ISourceLocation location) {
  430. IProgramElement node = asm.getHierarchy().findElementForSourceLine(location);
  431. if (node != null) {
  432. return createHandleIdentifier(node);
  433. }
  434. return null;
  435. }
  436. public String createHandleIdentifier(File sourceFile, int line, int column, int offset) {
  437. IProgramElement node = asm.getHierarchy().findElementForOffSet(sourceFile.getAbsolutePath(), line, offset);
  438. if (node != null) {
  439. return createHandleIdentifier(node);
  440. }
  441. return null;
  442. }
  443. public boolean dependsOnLocation() {
  444. // handles are independent of soureLocations therefore return false
  445. return false;
  446. }
  447. }