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

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