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.

Statics.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.doclets.standard;
  23. import org.aspectj.ajdoc.AdviceDoc;
  24. import org.aspectj.ajdoc.AspectDoc;
  25. import com.sun.javadoc.ClassDoc;
  26. import com.sun.javadoc.ProgramElementDoc;
  27. import com.sun.tools.doclets.Util;
  28. import java.util.ArrayList;
  29. import java.util.Collections;
  30. import java.util.Iterator;
  31. import java.util.List;
  32. /**
  33. * A splattering of misc. functionality.
  34. *
  35. * @author Jeff Palm
  36. */
  37. public class Statics {
  38. /**
  39. * Returns a aspectj-world type String of <code>cd</code>.
  40. *
  41. * @return either aspect interface or class depending
  42. * on the type of <code>cd</code>.
  43. */
  44. public static String type(ClassDoc cd) {
  45. return cd instanceof AspectDoc
  46. ? "aspect" : cd.isInterface()
  47. ? "interface" : "class";
  48. }
  49. /**
  50. * Returns the link target for <code>member</code>.
  51. *
  52. * @param member the ProgramElementDoc in question.
  53. * @return the link target for <code>member</code>.
  54. */
  55. public static String where(ProgramElementDoc member) {
  56. return member.name();
  57. }
  58. /**
  59. * Returns the link label for <code>member</code>.
  60. *
  61. * @param member the ProgramElementDoc in question.
  62. * @return the link label for <code>member</code>.
  63. */
  64. public static String label(ProgramElementDoc member) {
  65. return member.name();
  66. }
  67. /**
  68. * Returns the link target for <code>member</code> from
  69. * <code>cd</code>.
  70. *
  71. * @param cd the class from which we're linking.
  72. * @param member the ProgramElementDoc in question.
  73. * @return the link target for <code>member</code>.
  74. */
  75. public static String where(ClassDoc cd, ProgramElementDoc member) {
  76. if (member instanceof AdviceDoc) {
  77. return name(cd, (AdviceDoc)member).replace(' ','_').replace('#','-');
  78. }
  79. return member.name();
  80. }
  81. /**
  82. * Returns the link label for <code>member</code> from
  83. * <code>cd</code>.
  84. *
  85. * @param cd the class from which we're linking.
  86. * @param member the ProgramElementDoc in question.
  87. * @return the link target for <code>member</code>.
  88. */
  89. public static String label(ClassDoc cd, ProgramElementDoc member) {
  90. return name(cd, member);
  91. }
  92. /**
  93. * Returns the name for <code>member</code> from
  94. * <code>cd</code>. This is here because we don't
  95. * want really print the name of advice.
  96. *
  97. * @param cd the class from which we're printing.
  98. * @param member the ProgramElementDoc in question.
  99. * @return the name for <code>member</code>.
  100. */
  101. public static String name(ClassDoc cd, ProgramElementDoc member) {
  102. if (member instanceof AdviceDoc) {
  103. return name(cd, (AdviceDoc)member);
  104. }
  105. return member.name();
  106. }
  107. /**
  108. * Returns the String that should be printed for
  109. * an advice's name.
  110. *
  111. * @param cd the ClassDoc from where we're printing.
  112. * @param advice the member in question.
  113. * @return correct printing name for <code>advice</code>.
  114. */
  115. public static String name(ClassDoc cd, AdviceDoc advice) {
  116. String name = advice.name();
  117. int num = 1;
  118. for (Iterator i = advice(cd).iterator(); i.hasNext();) {
  119. AdviceDoc ad = (AdviceDoc)i.next();
  120. if (ad.equals(advice)) {
  121. break;
  122. }
  123. if (ad.name().equals(name)) {
  124. num++;
  125. }
  126. }
  127. return name + " #" + num;
  128. }
  129. /**
  130. * Returns the advice contained in <code>classdoc</code>.
  131. *
  132. * @param cd ClassDoc in question.
  133. * @return a List with the {@link AdviceDoc}s
  134. * contained in <code>cd</code>.
  135. */
  136. public static List advice(ClassDoc classdoc) {
  137. if (!(classdoc instanceof AspectDoc)) return Collections.EMPTY_LIST;
  138. AdviceDoc[] advice = ((AspectDoc)classdoc).advice();
  139. return advice == null ? Collections.EMPTY_LIST : Util.asList(advice);
  140. }
  141. /**
  142. * Returns an array of classes only.
  143. *
  144. * @param arr source array from where the ClassDocs in
  145. * the result will come.
  146. * @return an array of ClassDoc containing only
  147. * classes, <b>no aspects</b>.
  148. */
  149. public static ClassDoc[] classes(ClassDoc[] arr) {
  150. List list = new ArrayList();
  151. for (int i = 0; i < arr.length; i++) {
  152. if (!(arr[i] instanceof AspectDoc)) {
  153. list.add(arr[i]);
  154. }
  155. }
  156. return (ClassDoc[])list.toArray(new ClassDoc[list.size()]);
  157. }
  158. /**
  159. * Returns a list of the classes found in the
  160. * passed in list of types.
  161. *
  162. * @param types List of ClassDocs.
  163. * @return a List containing those ClassDocs in
  164. * <code>types</code> that <i>are not</i> aspects.
  165. * @see #types(List,boolean)
  166. */
  167. public static List classes(List types) {
  168. return types(types, false);
  169. }
  170. /**
  171. * Returns a list of the classes found in the
  172. * passed in list of types.
  173. *
  174. * @param types List of ClassDocs.
  175. * @return a List containing those ClassDocs in
  176. * <code>types</code> that <i>are</i> aspects.
  177. * @see #types(List,boolean)
  178. */
  179. public static List aspects(List types) {
  180. return types(types, true);
  181. }
  182. /**
  183. * Returns a list of ClassDocs taken from <code>types</code>
  184. * that are aspects iff <code>wantAspects</code>.
  185. *
  186. * @param types source List of ClassDocs.
  187. * @param wantAspects ClassDocs <i>c<i>in the resulting List will
  188. * conform to the test:
  189. * <code>c instanceof AspectDoc) == wantAspects<code>.
  190. * @return a List of ClassDocs all who conform to the test:
  191. * <code>c instanceof AspectDoc) == wantAspects<code>.
  192. */
  193. public static List types(List types, boolean wantAspects) {
  194. List list = new ArrayList();
  195. for (Iterator i = types.iterator(); i.hasNext();) {
  196. ClassDoc cd = (ClassDoc)i.next();
  197. if ((cd instanceof AspectDoc) == wantAspects) {
  198. list.add(cd);
  199. }
  200. }
  201. return list;
  202. }
  203. /**
  204. * Returns a prequalified class link to <code>cd</code> using
  205. * the link target <code>where</code>.
  206. *
  207. * @param writer base writer to use.
  208. * @param cd class to where we're linking.
  209. * @param where link target.
  210. * @return prequalified class link using
  211. * <code>cd</code> and <code>where</code>.
  212. */
  213. public static String getPreQualifiedClassLink
  214. (com.sun.tools.doclets.standard.SubWriterHolderWriter writer,
  215. ClassDoc cd,
  216. String where) {
  217. return writer.getPkgName(cd) + writer.getClassLink(cd, where, cd.name());
  218. }
  219. /**
  220. * Returns a prequalified class link to <code>cd</code> using
  221. * the link target <code>where</code> returned by
  222. * calling <code>getPreQualifiedClassLink</code>.
  223. *
  224. * @param writer base writer to use.
  225. * @param cd class to where we're linking.
  226. * @param where link target.
  227. * @see #getPreQualifiedClassLink
  228. */
  229. public static void printPreQualifiedClassLink
  230. (com.sun.tools.doclets.standard.SubWriterHolderWriter writer,
  231. ClassDoc cd,
  232. String where) {
  233. writer.print(getPreQualifiedClassLink(writer, cd, where));
  234. }
  235. }