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

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