Path: blob/master/src/jdk.javadoc/share/classes/jdk/javadoc/internal/api/JavadocTool.java
41171 views
/*1* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.javadoc.internal.api;2627import java.io.InputStream;28import java.io.OutputStream;29import java.io.OutputStreamWriter;30import java.io.PrintWriter;31import java.io.Writer;32import java.nio.charset.Charset;33import java.util.Collections;34import java.util.EnumSet;35import java.util.Locale;36import java.util.Objects;37import java.util.Set;3839import javax.lang.model.SourceVersion;40import javax.tools.DiagnosticListener;41import javax.tools.DocumentationTool;42import javax.tools.JavaFileManager;43import javax.tools.JavaFileObject;44import javax.tools.StandardJavaFileManager;4546import com.sun.tools.javac.api.ClientCodeWrapper;47import com.sun.tools.javac.file.JavacFileManager;48import com.sun.tools.javac.file.BaseFileManager;49import com.sun.tools.javac.util.ClientCodeException;50import com.sun.tools.javac.util.Context;51import com.sun.tools.javac.util.Log;52import jdk.javadoc.internal.tool.ToolOptions;5354/**55* Provides access to functionality specific to the JDK documentation tool,56* javadoc.57*58* <p><b>This is NOT part of any supported API.59* If you write code that depends on this, you do so at your own60* risk. This code and its internal interfaces are subject to change61* or deletion without notice.</b></p>62*/63public class JavadocTool implements DocumentationTool {6465@Override66public String name() {67return "javadoc";68}6970@Override71public DocumentationTask getTask(72Writer out,73JavaFileManager fileManager,74DiagnosticListener<? super JavaFileObject> diagnosticListener,75Class<?> docletClass,76Iterable<String> options,77Iterable<? extends JavaFileObject> compilationUnits)78{79Context context = new Context();80return getTask(out, fileManager, diagnosticListener,81docletClass, options, compilationUnits, context);82}8384public DocumentationTask getTask(85Writer out,86JavaFileManager fileManager,87DiagnosticListener<? super JavaFileObject> diagnosticListener,88Class<?> docletClass,89Iterable<String> options,90Iterable<? extends JavaFileObject> compilationUnits,91Context context) {92try {93ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);9495if (options != null) {96for (String option : options)97Objects.requireNonNull(option);98}99100if (compilationUnits != null) {101compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check102for (JavaFileObject cu : compilationUnits) {103if (cu.getKind() != JavaFileObject.Kind.SOURCE) {104final String kindMsg = "All compilation units must be of SOURCE kind";105throw new IllegalArgumentException(kindMsg);106}107}108}109110if (diagnosticListener != null)111context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));112113if (out == null)114context.put(Log.errKey, new PrintWriter(System.err, true));115else if (out instanceof PrintWriter pout)116context.put(Log.errKey, pout);117else118context.put(Log.errKey, new PrintWriter(out, true));119120if (fileManager == null) {121fileManager = getStandardFileManager(diagnosticListener, null, null);122if (fileManager instanceof BaseFileManager bfm) {123bfm.autoClose = true;124}125}126fileManager = ccw.wrap(fileManager);127context.put(JavaFileManager.class, fileManager);128129return new JavadocTaskImpl(context, docletClass, options, compilationUnits);130} catch (ClientCodeException ex) {131throw new RuntimeException(ex.getCause());132}133}134135// TODO: used shared static method in JavacFileManager136@Override137public StandardJavaFileManager getStandardFileManager(138DiagnosticListener<? super JavaFileObject> diagnosticListener,139Locale locale,140Charset charset) {141Context context = new Context();142context.put(Locale.class, locale);143if (diagnosticListener != null)144context.put(DiagnosticListener.class, diagnosticListener);145PrintWriter pw = (charset == null)146? new PrintWriter(System.err, true)147: new PrintWriter(new OutputStreamWriter(System.err, charset), true);148context.put(Log.errKey, pw);149return new JavacFileManager(context, true, charset);150}151152@Override153public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {154PrintWriter err_pw = new PrintWriter(err == null ? System.err : err, true);155PrintWriter out_pw = new PrintWriter(out == null ? System.out : out);156try {157return jdk.javadoc.internal.tool.Main.execute(arguments, err_pw);158} finally {159err_pw.flush();160out_pw.flush();161}162}163164@Override165public Set<SourceVersion> getSourceVersions() {166return Collections.unmodifiableSet(167EnumSet.range(SourceVersion.RELEASE_3, SourceVersion.latest()));168}169170@Override171public int isSupportedOption(String option) {172if (option == null)173throw new NullPointerException();174return ToolOptions.isSupportedOption(option);175}176177}178179180