View Javadoc
1   /*
2    * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.tools.javadoc;
27  
28  import java.io.IOException;
29  import java.util.Collection;
30  import java.util.Locale;
31  
32  import javax.tools.JavaFileManager;
33  import javax.tools.JavaFileObject;
34  import javax.tools.StandardJavaFileManager;
35  
36  import com.sun.javadoc.*;
37  import com.sun.tools.javac.tree.JCTree.JCClassDecl;
38  import com.sun.tools.javac.util.List;
39  import com.sun.tools.javac.util.ListBuffer;
40  import com.sun.tools.javac.util.Position;
41  
42  /**
43   * This class holds the information from one run of javadoc.
44   * Particularly the packages, classes and options specified
45   * by the user.
46   *
47   *  <p><b>This is NOT part of any supported API.
48   *  If you write code that depends on this, you do so at your own risk.
49   *  This code and its internal interfaces are subject to change or
50   *  deletion without notice.</b>
51   *
52   * @since 1.2
53   * @author Robert Field
54   * @author Atul M Dambalkar
55   * @author Neal Gafter (rewrite)
56   */
57  public class RootDocImpl extends DocImpl implements RootDoc {
58  
59      /**
60       * list of classes specified on the command line.
61       */
62      private List<ClassDocImpl> cmdLineClasses;
63  
64      /**
65       * list of packages specified on the command line.
66       */
67      private List<PackageDocImpl> cmdLinePackages;
68  
69      /**
70       * a collection of all options.
71       */
72      private List<String[]> options;
73  
74      /**
75       * Constructor used when reading source files.
76       *
77       * @param env the documentation environment, state for this javadoc run
78       * @param classes list of classes specified on the commandline
79       * @param packages list of package names specified on the commandline
80       * @param options list of options
81       */
82      public RootDocImpl(DocEnv env, List<JCClassDecl> classes, List<String> packages, List<String[]> options) {
83          super(env, null);
84          this.options = options;
85          setPackages(env, packages);
86          setClasses(env, classes);
87      }
88  
89      /**
90       * Constructor used when reading class files.
91       *
92       * @param env the documentation environment, state for this javadoc run
93       * @param classes list of class names specified on the commandline
94       * @param options list of options
95       */
96      public RootDocImpl(DocEnv env, List<String> classes, List<String[]> options) {
97          super(env, null);
98          this.options = options;
99          cmdLinePackages = List.nil();
100         ListBuffer<ClassDocImpl> classList = new ListBuffer<ClassDocImpl>();
101         for (String className : classes) {
102             ClassDocImpl c = env.loadClass(className);
103             if (c == null)
104                 env.error(null, "javadoc.class_not_found", className);
105             else
106                 classList = classList.append(c);
107         }
108         cmdLineClasses = classList.toList();
109     }
110 
111     /**
112      * Initialize classes information. Those classes are input from
113      * command line.
114      *
115      * @param env the compilation environment
116      * @param classes a list of ClassDeclaration
117      */
118     private void setClasses(DocEnv env, List<JCClassDecl> classes) {
119         ListBuffer<ClassDocImpl> result = new ListBuffer<ClassDocImpl>();
120         for (JCClassDecl def : classes) {
121             //### Do we want modifier check here?
122             if (env.shouldDocument(def.sym)) {
123                 ClassDocImpl cd = env.getClassDoc(def.sym);
124                 if (cd != null) {
125                     cd.isIncluded = true;
126                     result.append(cd);
127                 } //else System.out.println(" (classdoc is null)");//DEBUG
128             } //else System.out.println(" (env.shouldDocument() returned false)");//DEBUG
129         }
130         cmdLineClasses = result.toList();
131     }
132 
133     /**
134      * Initialize packages information.
135      *
136      * @param env the compilation environment
137      * @param packages a list of package names (String)
138      */
139     private void setPackages(DocEnv env, List<String> packages) {
140         ListBuffer<PackageDocImpl> packlist = new ListBuffer<PackageDocImpl>();
141         for (String name : packages) {
142             PackageDocImpl pkg = env.lookupPackage(name);
143             if (pkg != null) {
144                 pkg.isIncluded = true;
145                 packlist.append(pkg);
146             } else {
147                 env.warning(null, "main.no_source_files_for_package", name);
148             }
149         }
150         cmdLinePackages = packlist.toList();
151     }
152 
153     /**
154      * Command line options.
155      *
156      * <pre>
157      * For example, given:
158      *     javadoc -foo this that -bar other ...
159      *
160      * This method will return:
161      *      options()[0][0] = "-foo"
162      *      options()[0][1] = "this"
163      *      options()[0][2] = "that"
164      *      options()[1][0] = "-bar"
165      *      options()[1][1] = "other"
166      * </pre>
167      *
168      * @return an array of arrays of String.
169      */
170     public String[][] options() {
171         return options.toArray(new String[options.length()][]);
172     }
173 
174     /**
175      * Packages specified on the command line.
176      */
177     public PackageDoc[] specifiedPackages() {
178         return (PackageDoc[])cmdLinePackages
179             .toArray(new PackageDocImpl[cmdLinePackages.length()]);
180     }
181 
182     /**
183      * Classes and interfaces specified on the command line.
184      */
185     public ClassDoc[] specifiedClasses() {
186         ListBuffer<ClassDocImpl> classesToDocument = new ListBuffer<ClassDocImpl>();
187         for (ClassDocImpl cd : cmdLineClasses) {
188             cd.addAllClasses(classesToDocument, true);
189         }
190         return (ClassDoc[])classesToDocument.toArray(new ClassDocImpl[classesToDocument.length()]);
191     }
192 
193     /**
194      * Return all classes and interfaces (including those inside
195      * packages) to be documented.
196      */
197     public ClassDoc[] classes() {
198         ListBuffer<ClassDocImpl> classesToDocument = new ListBuffer<ClassDocImpl>();
199         for (ClassDocImpl cd : cmdLineClasses) {
200             cd.addAllClasses(classesToDocument, true);
201         }
202         for (PackageDocImpl pd : cmdLinePackages) {
203             pd.addAllClassesTo(classesToDocument);
204         }
205         return classesToDocument.toArray(new ClassDocImpl[classesToDocument.length()]);
206     }
207 
208     /**
209      * Return a ClassDoc for the specified class/interface name
210      *
211      * @param qualifiedName qualified class name
212      *                        (i.e. includes package name).
213      *
214      * @return a ClassDocImpl holding the specified class, null if
215      * this class is not referenced.
216      */
217     public ClassDoc classNamed(String qualifiedName) {
218         return env.lookupClass(qualifiedName);
219     }
220 
221     /**
222      * Return a PackageDoc for the specified package name
223      *
224      * @param name package name
225      *
226      * @return a PackageDoc holding the specified package, null if
227      * this package is not referenced.
228      */
229     public PackageDoc packageNamed(String name) {
230         return env.lookupPackage(name);
231     }
232 
233     /**
234      * Return the name of this Doc item.
235      *
236      * @return the string <code>"*RootDocImpl*"</code>.
237      */
238     public String name() {
239         return "*RootDocImpl*";
240     }
241 
242     /**
243      * Return the name of this Doc item.
244      *
245      * @return the string <code>"*RootDocImpl*"</code>.
246      */
247     public String qualifiedName() {
248         return "*RootDocImpl*";
249     }
250 
251     /**
252      * Return true if this Doc is include in the active set.
253      * RootDocImpl isn't even a program entity so it is always false.
254      */
255     public boolean isIncluded() {
256         return false;
257     }
258 
259     /**
260      * Print error message, increment error count.
261      *
262      * @param msg message to print
263      */
264     public void printError(String msg) {
265         env.printError(msg);
266     }
267 
268     /**
269      * Print error message, increment error count.
270      *
271      * @param msg message to print
272      */
273     public void printError(SourcePosition pos, String msg) {
274         env.printError(pos, msg);
275     }
276 
277     /**
278      * Print warning message, increment warning count.
279      *
280      * @param msg message to print
281      */
282     public void printWarning(String msg) {
283         env.printWarning(msg);
284     }
285 
286     /**
287      * Print warning message, increment warning count.
288      *
289      * @param msg message to print
290      */
291     public void printWarning(SourcePosition pos, String msg) {
292         env.printWarning(pos, msg);
293     }
294 
295     /**
296      * Print a message.
297      *
298      * @param msg message to print
299      */
300     public void printNotice(String msg) {
301         env.printNotice(msg);
302     }
303 
304     /**
305      * Print a message.
306      *
307      * @param msg message to print
308      */
309     public void printNotice(SourcePosition pos, String msg) {
310         env.printNotice(pos, msg);
311     }
312 
313     /**
314      * Return the path of the overview file and null if it does not exist.
315      * @return the path of the overview file and null if it does not exist.
316      */
317     private JavaFileObject getOverviewPath() {
318         for (String[] opt : options) {
319             if (opt[0].equals("-overview")) {
320                 if (env.fileManager instanceof StandardJavaFileManager) {
321                     StandardJavaFileManager fm = (StandardJavaFileManager) env.fileManager;
322                     return fm.getJavaFileObjects(opt[1]).iterator().next();
323                 }
324             }
325         }
326         return null;
327     }
328 
329     /**
330      * Do lazy initialization of "documentation" string.
331      */
332     @Override
333     protected String documentation() {
334         if (documentation == null) {
335             JavaFileObject overviewPath = getOverviewPath();
336             if (overviewPath == null) {
337                 // no doc file to be had
338                 documentation = "";
339             } else {
340                 // read from file
341                 try {
342                     documentation = readHTMLDocumentation(
343                         overviewPath.openInputStream(),
344                         overviewPath);
345                 } catch (IOException exc) {
346                     documentation = "";
347                     env.error(null, "javadoc.File_Read_Error", overviewPath.getName());
348                 }
349             }
350         }
351         return documentation;
352     }
353 
354     /**
355      * Return the source position of the entity, or null if
356      * no position is available.
357      */
358     @Override
359     public SourcePosition position() {
360         JavaFileObject path;
361         return ((path = getOverviewPath()) == null) ?
362             null :
363             SourcePositionImpl.make(path, Position.NOPOS, null);
364     }
365 
366     /**
367      * Return the locale provided by the user or the default locale value.
368      */
369     public Locale getLocale() {
370         return env.doclocale.locale;
371     }
372 
373     /**
374      * Return the current file manager.
375      */
376     public JavaFileManager getFileManager() {
377         return env.fileManager;
378     }
379 
380     public void initDocLint(Collection<String> opts, Collection<String> customTagNames) {
381         env.initDoclint(opts, customTagNames);
382     }
383 
384     public boolean isFunctionalInterface(AnnotationDesc annotationDesc) {
385         return annotationDesc.annotationType().qualifiedName().equals(
386                 env.syms.functionalInterfaceType.toString()) && env.source.allowLambda();
387     }
388 
389     public boolean showTagMessages() {
390         return env.showTagMessages();
391     }
392 }