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.

StructureUtil.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* *******************************************************************
  2. * Copyright (c) 2003 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Mik Kersten initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.tools.ajdoc;
  13. import java.io.File;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.aspectj.asm.AsmManager;
  17. import org.aspectj.asm.IProgramElement;
  18. import org.aspectj.asm.IRelationship;
  19. /**
  20. * @author Mik Kersten
  21. */
  22. public class StructureUtil {
  23. /**
  24. * Calculate the targets for a given IProgramElement (and it's immediate children if its not a type or if the child is CODE) and
  25. * relationship kind
  26. *
  27. * @return null if a relationship of that kind is not found
  28. */
  29. public static List<String> getTargets(IProgramElement node, IRelationship.Kind kind) {
  30. return getTargets(node, kind, null);
  31. }
  32. /**
  33. * Calculate the targets for a given IProgramElement (and it's immediate children if its not a type or if the child is CODE) and
  34. * relationship kind with the specified relationship name.
  35. *
  36. * @return null if a relationship of that kind is not found
  37. */
  38. public static List<String> getTargets(IProgramElement node, IRelationship.Kind kind, String relName) {
  39. List<IRelationship> relations = new ArrayList<>();
  40. List<IRelationship> rels = node.getModel().getRelationshipMap().get(node);
  41. if (rels != null) {
  42. relations.addAll(rels);
  43. }
  44. for (IProgramElement child : node.getChildren()) {
  45. // if we're not a type, or if we are and the child is code, then
  46. // we want to get the relationships for this child - this means that the
  47. // correct relationships appear against the type in the ajdoc
  48. if (!node.getKind().isType() || child.getKind().equals(IProgramElement.Kind.CODE)) {
  49. List<IRelationship> childRelations = node.getModel().getRelationshipMap().get(child);
  50. if (childRelations != null) {
  51. for (IRelationship rel : childRelations) {
  52. if (!relations.contains(rel)) {
  53. relations.add(rel);
  54. }
  55. }
  56. }
  57. }
  58. }
  59. if (relations == null || relations.isEmpty())
  60. return null;
  61. List<String> targets = new ArrayList<>();
  62. for (IRelationship rtn : relations) {
  63. if (rtn.getKind().equals(kind) && ((relName != null && relName.equals(rtn.getName())) || relName == null)) {
  64. List<String> targs = rtn.getTargets();
  65. for (String element : targs) {
  66. if (!targets.contains(element)) {
  67. targets.add(element);
  68. }
  69. }
  70. }
  71. }
  72. return targets;
  73. }
  74. static List<IProgramElement> getDeclareInterTypeTargets(IProgramElement node, IProgramElement.Kind kind) {
  75. List<IProgramElement> targets = new ArrayList<>();
  76. List<String> stringTargets = StructureUtil.getTargets(node, IRelationship.Kind.DECLARE_INTER_TYPE);
  77. if (stringTargets == null) {
  78. return null;
  79. }
  80. for (String element: stringTargets) {
  81. IProgramElement ipe = node.getModel().getHierarchy().findElementForHandle(element);
  82. if (ipe != null && ipe.getKind().equals(kind)) {
  83. targets.add(ipe);
  84. }
  85. }
  86. return targets;
  87. }
  88. public static List<String> getDeclareTargets(IProgramElement node) {
  89. List<IRelationship> relations = node.getModel().getRelationshipMap().get(node);
  90. List<String> targets = null;
  91. if (relations == null)
  92. return null;
  93. for (IRelationship rtn: relations) {
  94. if (rtn.getKind().isDeclareKind()) {
  95. targets = rtn.getTargets();
  96. }
  97. }
  98. return targets;
  99. }
  100. public static String getPackageDeclarationFromFile(AsmManager model, File file) {
  101. IProgramElement fileNode = model.getHierarchy().findElementForSourceFile(file.getAbsolutePath());
  102. String packageName = ((IProgramElement) fileNode.getChildren().get(0)).getPackageName();
  103. return packageName;
  104. }
  105. public static String genSignature(IProgramElement node) {
  106. StringBuilder sb = new StringBuilder();
  107. String accessibility = node.getAccessibility().toString();
  108. if (!accessibility.equals("package")) {
  109. sb.append(accessibility);
  110. sb.append(' ');
  111. }
  112. String modifiers = "";
  113. for (IProgramElement.Modifiers value : node.getModifiers()) {
  114. modifiers += value + " ";
  115. }
  116. if (node.getKind().equals(IProgramElement.Kind.METHOD) || node.getKind().equals(IProgramElement.Kind.FIELD)) {
  117. sb.append(node.getCorrespondingType());
  118. sb.append(' ');
  119. }
  120. if (node.getKind().equals(IProgramElement.Kind.CLASS)) {
  121. sb.append("class ");
  122. } else if (node.getKind().equals(IProgramElement.Kind.INTERFACE)) {
  123. sb.append("interface ");
  124. }
  125. sb.append(node.getName());
  126. if (node.getParameterTypes() != null) {
  127. sb.append('(');
  128. for (int i = 0; i < node.getParameterTypes().size(); i++) {
  129. sb.append(String.valueOf(node.getParameterTypes().get(i)));
  130. sb.append(' ');
  131. sb.append((String) node.getParameterNames().get(i));
  132. if (i < node.getParameterTypes().size() - 1) {
  133. sb.append(", ");
  134. }
  135. }
  136. sb.append(')');
  137. }
  138. return sb.toString();
  139. }
  140. public static boolean isAnonymous(IProgramElement node) {
  141. boolean isIntName = true;
  142. try {
  143. Integer.valueOf(node.getName());
  144. } catch (NumberFormatException nfe) {
  145. // !!! using exceptions for logic, fix
  146. isIntName = false;
  147. }
  148. // System.err.println(">>>>>>>> " + node.getName());
  149. return isIntName || node.getName().startsWith("new ");
  150. // return isIntName;
  151. // if (!isIntName) {
  152. //
  153. // return node.getName().startsWith("new ");
  154. // } else {
  155. // return false;
  156. // }
  157. }
  158. /**
  159. * @return same path, but ending in ".java" instead of ".aj"
  160. */
  161. public static String translateAjPathName(String path) {
  162. if (path.endsWith(".aj")) {
  163. path = path.substring(0, path.lastIndexOf(".aj")) + ".java";
  164. }
  165. return path;
  166. }
  167. }