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.3KB

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