1 /*
2  * Copyright (c) 2012, 2021, 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 jdk.javadoc.internal.api;
27 
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.io.OutputStreamWriter;
31 import java.io.PrintWriter;
32 import java.io.Writer;
33 import java.nio.charset.Charset;
34 import java.util.Collections;
35 import java.util.EnumSet;
36 import java.util.Locale;
37 import java.util.Objects;
38 import java.util.Set;
39 
40 import javax.lang.model.SourceVersion;
41 import javax.tools.DiagnosticListener;
42 import javax.tools.DocumentationTool;
43 import javax.tools.JavaFileManager;
44 import javax.tools.JavaFileObject;
45 import javax.tools.StandardJavaFileManager;
46 
47 import com.sun.tools.javac.api.ClientCodeWrapper;
48 import com.sun.tools.javac.file.JavacFileManager;
49 import com.sun.tools.javac.file.BaseFileManager;
50 import com.sun.tools.javac.util.ClientCodeException;
51 import com.sun.tools.javac.util.Context;
52 import com.sun.tools.javac.util.Log;
53 import jdk.javadoc.internal.tool.ToolOptions;
54 
55 /**
56  * Provides access to functionality specific to the JDK documentation tool,
57  * javadoc.
58  *
59  * <p><b>This is NOT part of any supported API.
60  * If you write code that depends on this, you do so at your own
61  * risk.  This code and its internal interfaces are subject to change
62  * or deletion without notice.</b></p>
63  */
64 public class JavadocTool implements DocumentationTool {
65 
66     @Override
name()67     public String name() {
68         return "javadoc";
69     }
70 
71     @Override
getTask( Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Class<?> docletClass, Iterable<String> options, Iterable<? extends JavaFileObject> compilationUnits)72     public DocumentationTask getTask(
73             Writer out,
74             JavaFileManager fileManager,
75             DiagnosticListener<? super JavaFileObject> diagnosticListener,
76             Class<?> docletClass,
77             Iterable<String> options,
78             Iterable<? extends JavaFileObject> compilationUnits)
79     {
80         Context context = new Context();
81         return getTask(out, fileManager, diagnosticListener,
82                 docletClass, options, compilationUnits, context);
83     }
84 
getTask( Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Class<?> docletClass, Iterable<String> options, Iterable<? extends JavaFileObject> compilationUnits, Context context)85     public DocumentationTask getTask(
86             Writer out,
87             JavaFileManager fileManager,
88             DiagnosticListener<? super JavaFileObject> diagnosticListener,
89             Class<?> docletClass,
90             Iterable<String> options,
91             Iterable<? extends JavaFileObject> compilationUnits,
92             Context context) {
93         try {
94             ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
95 
96             if (options != null) {
97                 for (String option : options)
98                     Objects.requireNonNull(option);
99             }
100 
101             if (compilationUnits != null) {
102                 compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
103                 for (JavaFileObject cu : compilationUnits) {
104                     if (cu.getKind() != JavaFileObject.Kind.SOURCE) {
105                         final String kindMsg = "All compilation units must be of SOURCE kind";
106                         throw new IllegalArgumentException(kindMsg);
107                     }
108                 }
109             }
110 
111             if (diagnosticListener != null)
112                 context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
113 
114             if (out == null)
115                 context.put(Log.errKey, new PrintWriter(System.err, true));
116             else if (out instanceof PrintWriter pout)
117                 context.put(Log.errKey, pout);
118             else
119                 context.put(Log.errKey, new PrintWriter(out, true));
120 
121             if (fileManager == null) {
122                 fileManager = getStandardFileManager(diagnosticListener, null, null);
123                 if (fileManager instanceof BaseFileManager bfm) {
124                     bfm.autoClose = true;
125                 }
126             }
127             fileManager = ccw.wrap(fileManager);
128             context.put(JavaFileManager.class, fileManager);
129 
130             return new JavadocTaskImpl(context, docletClass, options, compilationUnits);
131         } catch (ClientCodeException ex) {
132             throw new RuntimeException(ex.getCause());
133         }
134     }
135 
136     // TODO: used shared static method in JavacFileManager
137     @Override
getStandardFileManager( DiagnosticListener<? super JavaFileObject> diagnosticListener, Locale locale, Charset charset)138     public StandardJavaFileManager getStandardFileManager(
139             DiagnosticListener<? super JavaFileObject> diagnosticListener,
140             Locale locale,
141             Charset charset) {
142         Context context = new Context();
143         context.put(Locale.class, locale);
144         if (diagnosticListener != null)
145             context.put(DiagnosticListener.class, diagnosticListener);
146         PrintWriter pw = (charset == null)
147                 ? new PrintWriter(System.err, true)
148                 : new PrintWriter(new OutputStreamWriter(System.err, charset), true);
149         context.put(Log.errKey, pw);
150         return new JavacFileManager(context, true, charset);
151     }
152 
153     @Override
run(InputStream in, OutputStream out, OutputStream err, String... arguments)154     public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
155         PrintWriter err_pw = new PrintWriter(err == null ? System.err : err, true);
156         PrintWriter out_pw = new PrintWriter(out == null ? System.out : out);
157         try {
158             return jdk.javadoc.internal.tool.Main.execute(arguments, err_pw);
159         } finally {
160             err_pw.flush();
161             out_pw.flush();
162         }
163     }
164 
165     @Override
getSourceVersions()166     public Set<SourceVersion> getSourceVersions() {
167         return Collections.unmodifiableSet(
168                 EnumSet.range(SourceVersion.RELEASE_3, SourceVersion.latest()));
169     }
170 
171     @Override
isSupportedOption(String option)172     public int isSupportedOption(String option) {
173         if (option == null)
174             throw new NullPointerException();
175         return ToolOptions.isSupportedOption(option);
176     }
177 
178 }
179