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.

AspectDocImpl.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This file is part of the debugger and core tools for the AspectJ(tm)
  4. * programming language; see http://aspectj.org
  5. *
  6. * The contents of this file are subject to the Mozilla Public License
  7. * Version 1.1 (the "License"); you may not use this file except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is AspectJ.
  17. *
  18. * The Initial Developer of the Original Code is Xerox Corporation. Portions
  19. * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
  20. * All Rights Reserved.
  21. */
  22. package org.aspectj.tools.ajdoc;
  23. import org.aspectj.ajdoc.AdviceDoc;
  24. import org.aspectj.ajdoc.AspectDoc;
  25. import org.aspectj.ajdoc.IntroductionDoc;
  26. import org.aspectj.ajdoc.OfClauseDoc;
  27. import org.aspectj.compiler.base.ast.Decs;
  28. import org.aspectj.compiler.crosscuts.ast.AdviceDec;
  29. import org.aspectj.compiler.crosscuts.ast.AspectDec;
  30. import org.aspectj.compiler.crosscuts.ast.IntroducedSuperDec;
  31. import com.sun.javadoc.ClassDoc;
  32. import java.util.ArrayList;
  33. import java.util.Collection;
  34. import java.util.Collections;
  35. import java.util.HashSet;
  36. import java.util.Iterator;
  37. import java.util.List;
  38. public class AspectDocImpl extends ClassDocImpl implements AspectDoc {
  39. /** Array of AdviceDoc created from this AspectDecs AdviceDecs. */
  40. private final Collection advice;
  41. /** Array of IntroductionDec created from this AspectDecs introductions. */
  42. private final Collection introductions;
  43. /** The of clause this aspect has -- may be null. */
  44. private final OfClauseDoc ofClause;
  45. /** The aspects that dominate this aspect. */
  46. private final Collection dominators = new ArrayList();
  47. /** The aspects that are dominated by this aspect. */
  48. private final Collection dominatees = new ArrayList();
  49. /**
  50. * Constructs an AspectDoc with the containing ClassDoc
  51. * and underlying AspectDec.
  52. *
  53. * @param containingClass contained ClassDoc.
  54. * @param aspectDec underlying AspectDec.
  55. */
  56. public AspectDocImpl(ClassDoc containingClass, AspectDec aspectDec) {
  57. super(containingClass, aspectDec);
  58. introductions = createIntroductions();
  59. advice = createAdvice();
  60. ofClause = createOfClause();
  61. }
  62. /**
  63. * Returns an instance of AdviceDocImpl corresponding to
  64. * the AdviceDec passed in.
  65. *
  66. * @param dec the AdviceDec mapping to the desired
  67. * AdviceDocImpl.
  68. * @return an instance of AdviceDocImpl corresponding to
  69. * the AdviceDec passed in.
  70. */
  71. public AdviceDocImpl docForDec(AdviceDec dec) {
  72. for (Iterator i = advice.iterator(); i.hasNext();) {
  73. AdviceDocImpl ad = (AdviceDocImpl)i.next();
  74. if (ad.dec() == dec) return ad;
  75. }
  76. return null;
  77. }
  78. /**
  79. * Returns an instance of IntroducedSuperDocImpl corresponding to
  80. * the IntroducedSuperDec passed in.
  81. *
  82. * @param dec the IntroducedSuperDec mapping to the
  83. * desired IntroducedSuperDocImpl
  84. * @return an instance of IntroducedSuperDocImpl
  85. * corresponding to the IntroducedSuperDec
  86. * passed in.
  87. */
  88. public IntroducedSuperDocImpl introDocForDec(IntroducedSuperDec dec) {
  89. for (Iterator i = introductions.iterator(); i.hasNext();) {
  90. ProgramElementDocImpl id = (ProgramElementDocImpl)i.next();
  91. if (id.dec() == dec) return (IntroducedSuperDocImpl)id;
  92. }
  93. return null;
  94. }
  95. /**
  96. * Returns the underlying AspectDec.
  97. *
  98. * @return the underlying AspectDec.
  99. */
  100. protected AspectDec aspectDec() {
  101. return (AspectDec)typeDec();
  102. }
  103. /**
  104. * Returns the visible advice in this aspect.
  105. *
  106. * @return an array of AdviceDoc representing the
  107. * visible advice in this aspect.
  108. */
  109. public AdviceDoc[] advice() {
  110. return (AdviceDocImpl[])advice.toArray
  111. (new AdviceDocImpl[advice.size()]);
  112. }
  113. /**
  114. * Returns the aspects that are dominated by this aspect.
  115. *
  116. * @return an array of AspectDec representing the aspects
  117. * that are dominated by this aspect.
  118. */
  119. public AspectDoc[] dominatees() {
  120. return (AspectDoc[])dominatees.toArray
  121. (new AspectDoc[dominatees.size()]);
  122. }
  123. /**
  124. * Return the aspects that dominate this aspect.
  125. *
  126. * @return an array of AspectDoc representing the aspects
  127. * that dominate this aspect.
  128. */
  129. public AspectDoc[] dominators() {
  130. return (AspectDoc[])dominators.toArray
  131. (new AspectDoc[dominators.size()]);
  132. }
  133. /**
  134. * TODO
  135. * Returns the visible introductions of this aspect.
  136. *
  137. * @return an array of IntroductionDoc representing the
  138. * visible introductions in this aspect.
  139. */
  140. public IntroductionDoc[] introductions() {
  141. return (IntroductionDocImpl[])introductions.toArray
  142. (new IntroductionDocImpl[introductions.size()]);
  143. }
  144. /**
  145. * Returns the <i>of clause</i> of this aspect.
  146. *
  147. * @return the <i>of clause</i> of this aspect.
  148. */
  149. public OfClauseDoc ofClause() {
  150. return ofClause;
  151. }
  152. /**
  153. * Returns <code>true</code>.
  154. *
  155. * @return <code>true</code>.
  156. */
  157. public boolean isAspect() {
  158. return true;
  159. }
  160. /**
  161. * Returns <code>true</code> if this aspects dominates
  162. * the passed in aspect.
  163. *
  164. * @param other an AspectDoc that represents another
  165. * aspect in this world.
  166. * @return <code>true</code> if this aspects dominates
  167. * the passed in aspect.
  168. */
  169. public boolean dominates(AspectDoc other) {
  170. if (!(other instanceof AspectDocImpl)) {
  171. return false;
  172. }
  173. return aspectDec().dominates(((AspectDocImpl)other).aspectDec());
  174. }
  175. /**
  176. * Adds a dominates relation from <code>dominator</code> to
  177. * <code>this</code>. For example, somewhere in the code
  178. * the line
  179. * <code>
  180. * aspect dominator dominates this { ... }
  181. * </code>
  182. * exists.
  183. *
  184. * @param dominator an instance of AspectDocImpl that
  185. * dominates this.
  186. */
  187. public void addDominator(AspectDoc dominator) {
  188. dominators.add(dominator);
  189. }
  190. /**
  191. * Adds a dominates relation from <code>dominator</code> to
  192. * <code>this</code>. For example, somewhere in the code
  193. * the line
  194. * <code>
  195. * aspect this dominates dominatee { ... }
  196. * </code>
  197. * exists.
  198. *
  199. * @param dominatee an instance of AspectDocImpl that
  200. * is dominated by this.
  201. */
  202. public void addDominatee(AspectDoc dominatee) {
  203. dominatees.add(dominatee);
  204. }
  205. /**
  206. * Returns a Collection of IntroductionDocImpl representing
  207. * the introductions declared in this aspect.
  208. *
  209. * @return a Collection of IntroductionDocImpl representing
  210. * the introductions declared in this aspect.
  211. */
  212. private Collection createIntroductions() {
  213. Decs decs = aspectDec().getBody();
  214. if (decs.size() < 1) return Collections.EMPTY_LIST;
  215. Collection list = new HashSet();
  216. for (Iterator i = decs.getList().iterator(); i.hasNext();) {
  217. Object o = IntroductionDocImpl.getInstance(this, i.next());
  218. if (o != null) list.add(o);
  219. }
  220. return list;
  221. }
  222. /**
  223. * Returns a Collection of AdviceDocImpl representing
  224. * the advice declared in this aspect.
  225. *
  226. * @return a Collection of AdviceDocImpl representing
  227. * the advice declared in this aspect.
  228. */
  229. private Collection createAdvice() {
  230. // pluck the AdviceDec from the list of JpPlannerMakers
  231. List decs = aspectDec().getJpPlannerMakers();
  232. if (null != decs) {
  233. final int QUIT = 2;
  234. for (int tries = 0; tries < QUIT; tries++) {
  235. try {
  236. for (Iterator i = decs.iterator(); i.hasNext();) {
  237. Object o = i.next();
  238. if (!(o instanceof AdviceDec)) {
  239. i.remove();
  240. }
  241. }
  242. tries = QUIT;
  243. } catch (UnsupportedOperationException o) {
  244. if (0 != tries) {
  245. tries = QUIT;
  246. } else {
  247. ArrayList list = new ArrayList();
  248. list.addAll(decs);
  249. decs = list;
  250. }
  251. }
  252. }
  253. }
  254. if ((null == decs) || (decs.size() < 1)) {
  255. return Collections.EMPTY_LIST;
  256. }
  257. List list = new ArrayList();
  258. for (Iterator i = decs.iterator(); i.hasNext();) {
  259. list.add(new AdviceDocImpl(this, (AdviceDec)i.next()));
  260. }
  261. return list;
  262. }
  263. /**
  264. * Returns an instance of OfClauseDoc representing
  265. * the of clause declared by this aspect.
  266. *
  267. * @return an instance of OfClauseDoc representing
  268. * the of clause declared by this aspect.
  269. */
  270. private OfClauseDoc createOfClause() {
  271. return OfClauseDocImpl.getInstance(aspectDec().getPerClause());
  272. }
  273. }