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.

Declaration.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Mik Kersten port to AspectJ 1.1+ code base
  13. * ******************************************************************/
  14. package org.aspectj.tools.ajdoc;
  15. import java.io.IOException;
  16. import java.io.ObjectOutputStream;
  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Iterator;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import org.aspectj.asm.IProgramElement;
  24. /**
  25. * @author Mik Kersten
  26. * @deprecated org.aspectj.asm.IProgramElement should be used instead
  27. */
  28. public class Declaration implements Serializable {
  29. private int beginLine;
  30. private int endLine;
  31. private int beginColumn;
  32. private int endColumn;
  33. private String modifiers;
  34. private String fullSignature;
  35. private String signature;
  36. private String crosscutDesignator;
  37. private String packageName;
  38. private String kind;
  39. private String declaringType;
  40. private String filename;
  41. private String formalComment;
  42. private Declaration[] declarations;
  43. private Handle crosscutDeclarationHandle;
  44. private Handle[] pointedToByHandles;
  45. private Handle[] pointsToHandles;
  46. transient private Declaration crosscutDeclaration;
  47. transient private Declaration[] pointedToBy = null;
  48. transient private Declaration[] pointsTo = null;
  49. private Declaration parentDeclaration = null;
  50. private IProgramElement node;
  51. public Declaration(int beginLine, int endLine, int beginColumn, int endColumn,
  52. String modifiers, String signature, String fullSignature,
  53. String crosscutDesignator,
  54. String declaringType, String kind,
  55. String filename, String formalComment,
  56. String packageName,
  57. IProgramElement node)
  58. {
  59. this.beginLine = beginLine;
  60. this.endLine = endLine;
  61. this.beginColumn = beginColumn;
  62. this.endColumn = endColumn;
  63. this.modifiers = modifiers;
  64. this.signature = signature;
  65. this.fullSignature = fullSignature;
  66. this.crosscutDesignator = crosscutDesignator;
  67. this.declaringType = declaringType;
  68. this.kind = kind;
  69. this.filename = filename;
  70. this.formalComment = formalComment;
  71. this.packageName = packageName;
  72. this.pointedToByHandles = new Handle[0];
  73. this.pointsToHandles = new Handle[0];
  74. //???
  75. this.declarations = new Declaration[0];
  76. this.node = node;
  77. }
  78. public int getBeginLine() { return beginLine; }
  79. public int getEndLine() { return endLine; }
  80. public int getBeginColumn() { return beginColumn; }
  81. public int getEndColumn() { return endColumn; }
  82. public String getModifiers() { return modifiers; }
  83. public String getFullSignature() { return fullSignature; }
  84. public String getSignature() { return signature; }
  85. public String getPackageName() { return packageName; }
  86. public String getCrosscutDesignator() { return crosscutDesignator; }
  87. public Declaration getParentDeclaration() { return parentDeclaration; }
  88. public Declaration getCrosscutDeclaration() {
  89. if (crosscutDeclaration == null && crosscutDeclarationHandle != null) {
  90. crosscutDeclaration = crosscutDeclarationHandle.resolve();
  91. }
  92. return crosscutDeclaration;
  93. }
  94. public void setCrosscutDeclaration(Declaration _crosscutDeclaration) {
  95. crosscutDeclaration = _crosscutDeclaration;
  96. }
  97. public String getDeclaringType() { return declaringType; }
  98. public String getKind() {
  99. if (kind.startsWith("introduced-")) {
  100. return kind.substring(11);
  101. } else {
  102. return kind;
  103. }
  104. }
  105. public String getFilename() { return filename; }
  106. public String getFormalComment() { return formalComment; }
  107. public Declaration[] getDeclarations() {
  108. return declarations;
  109. }
  110. public void setDeclarations(Declaration[] decs) {
  111. declarations = decs;
  112. if (decs != null) {
  113. for (int i = 0; i < decs.length; i++) {
  114. decs[i].parentDeclaration = this;
  115. }
  116. }
  117. }
  118. public void setPointedToBy(Declaration[] decs) { pointedToBy = decs; }
  119. public void setPointsTo(Declaration[] decs) { pointsTo = decs; }
  120. public Declaration[] getPointedToBy() {
  121. if (pointedToBy == null) {
  122. pointedToBy = resolveHandles(pointedToByHandles);
  123. }
  124. return pointedToBy; //.elements();
  125. }
  126. public Declaration[] getPointsTo() {
  127. if (pointsTo == null) {
  128. pointsTo = resolveHandles(pointsToHandles);
  129. }
  130. return pointsTo; //.elements();
  131. }
  132. private Declaration[] filterTypes(Declaration[] a_decs) {
  133. List decs = new LinkedList(Arrays.asList(a_decs));
  134. for(Iterator i = decs.iterator(); i.hasNext(); ) {
  135. Declaration dec = (Declaration)i.next();
  136. if (!dec.isType()) i.remove();
  137. }
  138. return (Declaration[])decs.toArray(new Declaration[decs.size()]);
  139. }
  140. public Declaration[] getTargets() {
  141. Declaration[] pointsTo = getPointsTo();
  142. if (kind.equals("advice")) {
  143. return pointsTo;
  144. } else if (kind.equals("introduction")) {
  145. return filterTypes(pointsTo);
  146. } else {
  147. return new Declaration[0];
  148. }
  149. }
  150. // Handles are used to deal with dependencies between Declarations in different files
  151. private Handle getHandle() {
  152. return new Handle(filename, beginLine, beginColumn);
  153. }
  154. private Declaration[] resolveHandles(Handle[] handles) {
  155. Declaration[] declarations = new Declaration[handles.length];
  156. int missed = 0;
  157. for(int i=0; i<handles.length; i++) {
  158. //if (handles[i] == null) continue;
  159. declarations[i] = handles[i].resolve();
  160. if (declarations[i] == null) missed++;
  161. }
  162. if (missed > 0) {
  163. Declaration[] decs = new Declaration[declarations.length - missed];
  164. for (int i=0, j=0; i < declarations.length; i++) {
  165. if (declarations[i] != null) decs[j++] = declarations[i];
  166. }
  167. declarations = decs;
  168. }
  169. return declarations;
  170. }
  171. private Handle[] getHandles(Declaration[] declarations) {
  172. Handle[] handles = new Handle[declarations.length];
  173. for(int i=0; i<declarations.length; i++) {
  174. //if (declarations[i] == null) continue;
  175. handles[i] = declarations[i].getHandle();
  176. }
  177. return handles;
  178. }
  179. // Make sure that all decs are convertted to handles before serialization
  180. private void writeObject(ObjectOutputStream out) throws IOException {
  181. pointedToByHandles = getHandles(getPointedToBy());
  182. pointsToHandles = getHandles(getPointsTo());
  183. if (crosscutDeclaration != null) {
  184. crosscutDeclarationHandle = crosscutDeclaration.getHandle();
  185. }
  186. out.defaultWriteObject();
  187. }
  188. // support functions
  189. public Declaration[] getCrosscutDeclarations() {
  190. return getDeclarationsHelper("pointcut");
  191. }
  192. public Declaration[] getAdviceDeclarations() {
  193. return getDeclarationsHelper("advice");
  194. }
  195. public Declaration[] getIntroductionDeclarations() {
  196. return getDeclarationsHelper("introduction");
  197. }
  198. private Declaration[] getDeclarationsHelper(String kind) {
  199. Declaration[] decls = getDeclarations();
  200. List result = new ArrayList();
  201. for ( int i = 0; i < decls.length; i++ ) {
  202. Declaration decl = decls[i];
  203. if ( decl.getKind().equals(kind) ) {
  204. result.add(decl);
  205. }
  206. }
  207. return (Declaration[])result.toArray(new Declaration[result.size()]);
  208. }
  209. public boolean isType() {
  210. return getKind().equals("interface") || getKind().equals("class") || getKind().equals("aspect") || getKind().equals("enum");
  211. }
  212. public boolean hasBody() {
  213. String kind = getKind();
  214. return kind.equals("class") || kind.endsWith("constructor") ||
  215. (kind.endsWith("method") && getModifiers().indexOf("abstract") == -1 &&
  216. getModifiers().indexOf("native") == -1);
  217. }
  218. public boolean isIntroduced() {
  219. return kind.startsWith("introduced-");
  220. }
  221. public boolean hasSignature() {
  222. String kind = getKind();
  223. if ( kind.equals( "class" ) ||
  224. kind.equals( "interface" ) ||
  225. kind.equals( "initializer" ) ||
  226. kind.equals( "field" ) ||
  227. kind.equals( "constructor" ) ||
  228. kind.equals( "method" ) ) {
  229. return true;
  230. }
  231. else {
  232. return false;
  233. }
  234. }
  235. private static class Handle implements Serializable {
  236. public String filename;
  237. public int line, column;
  238. public Handle(String filename, int line, int column) {
  239. this.filename = filename;
  240. this.line = line;
  241. this.column = column;
  242. }
  243. public Declaration resolve() {
  244. SymbolManager manager = SymbolManager.getDefault();
  245. return manager.getDeclarationAtPoint(filename, line, column);
  246. }
  247. }
  248. public IProgramElement getNode() {
  249. return node;
  250. }
  251. }