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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 (Iterator<IProgramElement> iter = node.getChildren().iterator(); iter.hasNext();) {
  46. IProgramElement child = (IProgramElement) iter.next();
  47. // if we're not a type, or if we are and the child is code, then
  48. // we want to get the relationships for this child - this means that the
  49. // correct relationships appear against the type in the ajdoc
  50. if (!node.getKind().isType() || child.getKind().equals(IProgramElement.Kind.CODE)) {
  51. List<IRelationship> childRelations = node.getModel().getRelationshipMap().get(child);
  52. if (childRelations != null) {
  53. for (Iterator<IRelationship> iterator = childRelations.iterator(); iterator.hasNext();) {
  54. IRelationship rel = (IRelationship) iterator.next();
  55. if (!relations.contains(rel)) {
  56. relations.add(rel);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. if (relations == null || relations.isEmpty())
  63. return null;
  64. List<String> targets = new ArrayList<String>();
  65. for (Iterator<IRelationship> it = relations.iterator(); it.hasNext();) {
  66. IRelationship rtn = (IRelationship) it.next();
  67. if (rtn.getKind().equals(kind) && ((relName != null && relName.equals(rtn.getName())) || relName == null)) {
  68. List<String> targs = rtn.getTargets();
  69. for (String element: targs) {
  70. if (!targets.contains(element)) {
  71. targets.add(element);
  72. }
  73. }
  74. }
  75. }
  76. return targets;
  77. }
  78. static List<IProgramElement> getDeclareInterTypeTargets(IProgramElement node, IProgramElement.Kind kind) {
  79. List<IProgramElement> targets = new ArrayList<IProgramElement>();
  80. List<String> stringTargets = StructureUtil.getTargets(node, IRelationship.Kind.DECLARE_INTER_TYPE);
  81. if (stringTargets == null) {
  82. return null;
  83. }
  84. for (String element: stringTargets) {
  85. IProgramElement ipe = node.getModel().getHierarchy().findElementForHandle(element);
  86. if (ipe != null && ipe.getKind().equals(kind)) {
  87. targets.add(ipe);
  88. }
  89. }
  90. return targets;
  91. }
  92. public static List<String> getDeclareTargets(IProgramElement node) {
  93. List<IRelationship> relations = node.getModel().getRelationshipMap().get(node);
  94. List<String> targets = null;
  95. if (relations == null)
  96. return null;
  97. for (IRelationship rtn: relations) {
  98. if (rtn.getKind().isDeclareKind()) {
  99. targets = rtn.getTargets();
  100. }
  101. }
  102. return targets;
  103. }
  104. public static String getPackageDeclarationFromFile(AsmManager model, File file) {
  105. IProgramElement fileNode = model.getHierarchy().findElementForSourceFile(file.getAbsolutePath());
  106. String packageName = ((IProgramElement) fileNode.getChildren().get(0)).getPackageName();
  107. return packageName;
  108. }
  109. public static String genSignature(IProgramElement node) {
  110. StringBuffer sb = new StringBuffer();
  111. String accessibility = node.getAccessibility().toString();
  112. if (!accessibility.equals("package")) {
  113. sb.append(accessibility);
  114. sb.append(' ');
  115. }
  116. String modifiers = "";
  117. for (Iterator modIt = node.getModifiers().iterator(); modIt.hasNext();) {
  118. modifiers += modIt.next() + " ";
  119. }
  120. if (node.getKind().equals(IProgramElement.Kind.METHOD) || node.getKind().equals(IProgramElement.Kind.FIELD)) {
  121. sb.append(node.getCorrespondingType());
  122. sb.append(' ');
  123. }
  124. if (node.getKind().equals(IProgramElement.Kind.CLASS)) {
  125. sb.append("class ");
  126. } else if (node.getKind().equals(IProgramElement.Kind.INTERFACE)) {
  127. sb.append("interface ");
  128. }
  129. sb.append(node.getName());
  130. if (node.getParameterTypes() != null) {
  131. sb.append('(');
  132. for (int i = 0; i < node.getParameterTypes().size(); i++) {
  133. sb.append(String.valueOf(node.getParameterTypes().get(i)));
  134. sb.append(' ');
  135. sb.append((String) node.getParameterNames().get(i));
  136. if (i < node.getParameterTypes().size() - 1) {
  137. sb.append(", ");
  138. }
  139. }
  140. sb.append(')');
  141. }
  142. return sb.toString();
  143. }
  144. public static boolean isAnonymous(IProgramElement node) {
  145. boolean isIntName = true;
  146. try {
  147. Integer.valueOf(node.getName());
  148. } catch (NumberFormatException nfe) {
  149. // !!! using exceptions for logic, fix
  150. isIntName = false;
  151. }
  152. // System.err.println(">>>>>>>> " + node.getName());
  153. return isIntName || node.getName().startsWith("new ");
  154. // return isIntName;
  155. // if (!isIntName) {
  156. //
  157. // return node.getName().startsWith("new ");
  158. // } else {
  159. // return false;
  160. // }
  161. }
  162. /**
  163. * @return same path, but ending in ".java" instead of ".aj"
  164. */
  165. public static String translateAjPathName(String path) {
  166. if (path.endsWith(".aj")) {
  167. path = path.substring(0, path.lastIndexOf(".aj")) + ".java";
  168. }
  169. return path;
  170. }
  171. }