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