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

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