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.

PackageDocImpl.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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.AspectDoc;
  24. import org.aspectj.ajdoc.PackageDoc;
  25. import org.aspectj.compiler.base.ast.Type;
  26. import org.aspectj.compiler.base.ast.TypeDec;
  27. import org.aspectj.compiler.crosscuts.AspectJCompiler;
  28. import com.sun.javadoc.ClassDoc;
  29. import java.io.File;
  30. import java.util.ArrayList;
  31. import java.util.Collection;
  32. import java.util.HashMap;
  33. import java.util.HashSet;
  34. import java.util.Iterator;
  35. import java.util.List;
  36. import java.util.Map;
  37. import java.util.Set;
  38. /**
  39. * Represents a package in the aspectj-world.
  40. * A package is a passive container of classes given to it.
  41. * It does not enforce visibility rules. However, you can
  42. * purge packages without classes from the static list of packages.
  43. * @author Jeff Palm
  44. */
  45. public class PackageDocImpl
  46. extends DocImpl
  47. implements org.aspectj.ajdoc.PackageDoc {
  48. public final static String UNNAMED_PACKAGE = ""; //unnamed-package";
  49. private static AjdocCompiler ajdoc = Ajdoc.instance();
  50. private static AspectJCompiler ajc = ajdoc;
  51. public AspectJCompiler ajc() { return ajc; }
  52. private Set allClasses = new HashSet();
  53. private Comment comment;
  54. private String name;
  55. /**
  56. * Only want to create these within the static access.
  57. *
  58. * @param name name of the new package.
  59. */
  60. private PackageDocImpl(String name) {
  61. this.name = name;
  62. findDocumentation();
  63. }
  64. /**
  65. * Adds a class to this package.
  66. *
  67. * @param classDoc The new class.
  68. */
  69. public void addClass(ClassDoc classDoc) {
  70. allClasses.add(classDoc);
  71. }
  72. /**
  73. * Attempts to find a class with name <code>className</code>
  74. * in this package.
  75. *
  76. * @param className name of the class to find.
  77. */
  78. public ClassDoc findClass(String className) {
  79. Type type = ajc.getTypeManager().
  80. findType(name(), className);
  81. if (type != null) {
  82. return ClassDocImpl.getInstance(type.getTypeDec());
  83. }
  84. for (Iterator i = allClasses.iterator(); i.hasNext();) {
  85. ClassDoc cd = (ClassDoc)i.next();
  86. if (className.equals(cd.name())) {
  87. return cd; //todo wes was null?
  88. }
  89. }
  90. return null;
  91. }
  92. /**
  93. * Returns all the classes in this package.
  94. *
  95. * @return an array of ClassDoc representing all
  96. * the classes in this package.
  97. */
  98. public ClassDoc[] allClasses() {
  99. return (ClassDoc[])allClasses.toArray(new ClassDoc[allClasses.size()]);
  100. }
  101. /**
  102. * Returns all the aspects in this package.
  103. *
  104. * @return an array of AspectDoc representing all
  105. * the aspects in this package.
  106. */
  107. public AspectDoc[] aspects() {
  108. List list = new ArrayList();
  109. for (Iterator i = allClasses.iterator(); i.hasNext();) {
  110. org.aspectj.ajdoc.ClassDoc doc = (org.aspectj.ajdoc.ClassDoc)i.next();
  111. if (doc.isAspect()) list.add(doc);
  112. }
  113. return (AspectDoc[])list.toArray
  114. (new AspectDoc[list.size()]);
  115. }
  116. /**
  117. * Returns all the ordinary classes in this package.
  118. *
  119. * @return an array of ClassDoc representing all
  120. * the ordinary classes in this package.
  121. */
  122. public ClassDoc[] ordinaryClasses() {
  123. List list = new ArrayList();
  124. for (Iterator i = allClasses.iterator(); i.hasNext();) {
  125. ClassDoc doc = (ClassDoc)i.next();
  126. if (doc.isOrdinaryClass()) list.add(doc);
  127. }
  128. return (ClassDoc[])list.toArray
  129. (new ClassDoc[list.size()]);
  130. }
  131. /**
  132. * Returns all the exceptions in this package.
  133. *
  134. * @return an array of ClassDoc representing all
  135. * the exceptions in this package.
  136. */
  137. public ClassDoc[] exceptions() {
  138. List list = new ArrayList();
  139. for (Iterator i = allClasses.iterator(); i.hasNext();) {
  140. ClassDoc doc = (ClassDoc)i.next();
  141. if (doc.isException()) list.add(doc);
  142. }
  143. return (ClassDoc[])list.toArray
  144. (new ClassDoc[list.size()]);
  145. }
  146. /**
  147. * Returns all the errors in this package.
  148. *
  149. * @return an array of ClassDoc representing all
  150. * the errors in this package.
  151. */
  152. public ClassDoc[] errors() {
  153. List list = new ArrayList();
  154. for (Iterator i = allClasses.iterator(); i.hasNext();) {
  155. ClassDoc doc = (ClassDoc)i.next();
  156. if (doc.isError()) list.add(doc);
  157. }
  158. return (ClassDoc[])list.toArray
  159. (new ClassDoc[list.size()]);
  160. }
  161. /**
  162. * Returns all the interfaces in this package.
  163. *
  164. * @return an array of ClassDoc representing all
  165. * the interfaces in this package.
  166. */
  167. public ClassDoc[] interfaces() {
  168. List list = new ArrayList();
  169. for (Iterator i = allClasses.iterator(); i.hasNext();) {
  170. ClassDoc doc = (ClassDoc)i.next();
  171. if (doc.isInterface()) list.add(doc);
  172. }
  173. return (ClassDoc[])list.toArray
  174. (new ClassDoc[list.size()]);
  175. }
  176. /**
  177. * Returns the name if included -- null otherwise.
  178. *
  179. * @return the name if included -- null otherwise.
  180. */
  181. public String name() {
  182. return isIncluded() ? name : null;
  183. }
  184. /**
  185. * Compare based on <code>name()</code>.
  186. *
  187. * @param other other Object.
  188. * @return <code>true</code> if the other Object is a
  189. * PackageDocImpl and has the same name.
  190. */
  191. public boolean equals(Object other) {
  192. return other instanceof PackageDocImpl && other != null
  193. ? name().equals(((PackageDocImpl)other).name())
  194. : super.equals(other);
  195. }
  196. /**
  197. * Returns the name.
  198. *
  199. * @return the name.
  200. */
  201. public String toString() {
  202. return name();
  203. }
  204. private void findDocumentation() {
  205. if (ajdoc == null) return;
  206. String filename = (name.equals("")
  207. ? name
  208. : name.replace('.',File.separatorChar)
  209. + File.separatorChar) + "package.html";
  210. File html = ajdoc.findFile(filename, false);
  211. if (html == null) return;
  212. String rawCommentText = Util.documentation(html, ajdoc.err());
  213. //TODO: should be done in aspect from Aspects.java
  214. //FormalComment comment = new FormalComment(rawCommentText);
  215. setComment(new Comment(this, rawCommentText));
  216. }
  217. /* ------------------------------------------------------------
  218. * Factory stuff
  219. * ------------------------------------------------------------
  220. */
  221. private static Map namesToPackages = new HashMap();
  222. /**
  223. * Returns the collection of known packages.
  224. *
  225. * @return a Collection representing the known packages.
  226. */
  227. public static Collection packages() {
  228. return namesToPackages.values();
  229. }
  230. /**
  231. * Inits the world and AspectJCompiler with these
  232. * packages.
  233. *
  234. * @param world current World.
  235. * @param ajc current compiler.
  236. */
  237. public static void init(AspectJCompiler ajc) {
  238. PackageDocImpl.ajc = ajc;
  239. if (ajc instanceof AjdocCompiler) {
  240. PackageDocImpl.ajdoc = (AjdocCompiler)ajc;
  241. }
  242. }
  243. /**
  244. * Returns a package for a TypeDec.
  245. *
  246. * @param typeDec TypeDec whose package is desired.
  247. * @return a PackageDocImpl for a given TypeDec.
  248. */
  249. public static PackageDocImpl getPackageDoc(TypeDec typeDec) {
  250. return getPackageDoc(typeDec.getPackageName());
  251. }
  252. /**
  253. * Returns a package for a name.
  254. *
  255. * @param packageName package name for which to look.
  256. * @return a PackageDocImpl for a given package name.
  257. */
  258. public static PackageDocImpl getPackageDoc(String packageName) {
  259. return addPackageDoc(packageName);
  260. }
  261. /**
  262. * Adds a package with name <code>name</code> if it
  263. * already doesn't exist, and returns the new package
  264. * (if own with the same name didn't exist) or the
  265. * existing package.
  266. *
  267. * @param name name of the new package.
  268. * @return current package mapping to
  269. * <code>name</code>.
  270. */
  271. public static PackageDocImpl addPackageDoc(String name) {
  272. if (name == null) name = UNNAMED_PACKAGE;
  273. PackageDocImpl packageDoc = (PackageDocImpl)namesToPackages.get(name);
  274. if (packageDoc == null) {
  275. packageDoc = new PackageDocImpl(name);
  276. addPackageDoc(packageDoc);
  277. }
  278. return packageDoc;
  279. }
  280. /**
  281. * Adds the PackageDoc if one doesn't already exist
  282. * with the same name, and returns the existing or created
  283. * package with the same name as the one passed in .
  284. *
  285. * @param packageDoc PackageDoc we want to add.
  286. * @return package in the world with the same
  287. * name as <code>packageDoc</code>.
  288. */
  289. public static PackageDocImpl addPackageDoc(PackageDoc packageDoc) {
  290. if (packageDoc == null) return null;
  291. return (PackageDocImpl)namesToPackages.put(packageDoc.name(),
  292. packageDoc);
  293. }
  294. /**
  295. * Sets the include flag to false for empty packages
  296. * todo: remove instead?
  297. */
  298. public static void excludeEmptyPackages() {
  299. for (Iterator i = packages().iterator(); i.hasNext();) {
  300. PackageDocImpl pkg = (PackageDocImpl)i.next();
  301. if (pkg.allClasses().length < 1) {
  302. pkg.setIncluded(false);
  303. }
  304. }
  305. }
  306. }