Path: blob/master/src/jdk.javadoc/share/classes/jdk/javadoc/internal/api/JavadocTaskImpl.java
41171 views
/*1* Copyright (c) 2012, 2020, 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.util.ArrayList;28import java.util.Collections;29import java.util.List;30import java.util.Locale;31import java.util.concurrent.atomic.AtomicBoolean;3233import javax.tools.DocumentationTool.DocumentationTask;34import javax.tools.JavaFileObject;3536import com.sun.tools.javac.main.Option;37import com.sun.tools.javac.util.ClientCodeException;38import com.sun.tools.javac.util.Context;39import com.sun.tools.javac.util.Options;40import jdk.javadoc.internal.tool.Start;4142/**43* Provides access to functionality specific to the JDK documentation tool,44* javadoc.45*46* <p><b>This is NOT part of any supported API.47* If you write code that depends on this, you do so at your own48* risk. This code and its internal interfaces are subject to change49* or deletion without notice.</b></p>50*/51public class JavadocTaskImpl implements DocumentationTask {52private final AtomicBoolean used = new AtomicBoolean();5354private final Context context;55private final Class<?> docletClass;56private final Iterable<String> options;57private final Iterable<? extends JavaFileObject> fileObjects;58private Locale locale;59private final List<String> addModules = new ArrayList<>();6061public JavadocTaskImpl(Context context,62Class<?> docletClass,63Iterable<String> options,64Iterable<? extends JavaFileObject> fileObjects)65{66this.context = context;67this.docletClass = docletClass;6869this.options = (options == null) ? Collections.emptySet()70: nullCheck(options);71this.fileObjects = (fileObjects == null) ? Collections.emptySet()72: nullCheck(fileObjects);73setLocale(Locale.getDefault());74}7576@Override77public void setLocale(Locale locale) {78if (used.get()) {79throw new IllegalStateException();80}81this.locale = locale;82}8384@Override85public void addModules(Iterable<String> moduleNames) {86nullCheck(moduleNames);87if (used.get()) {88throw new IllegalStateException();89}90for (String name : moduleNames) {91addModules.add(name);92}93}9495@Override96public Boolean call() {97if (used.getAndSet(true)) {98throw new IllegalStateException("multiple calls to method 'call'");99}100initContext();101Start jdoc = new Start(context);102try {103return jdoc.begin(docletClass, options, fileObjects);104} catch (ClientCodeException e) {105throw new RuntimeException(e.getCause());106}107}108109private void initContext() {110//initialize compiler's default locale111context.put(Locale.class, locale);112if (!addModules.isEmpty()) {113String names = String.join(",", addModules);114Options opts = Options.instance(context);115String prev = opts.get(Option.ADD_MODULES);116opts.put(Option.ADD_MODULES, (prev == null) ? names : prev + "," + names);117}118}119120private static <T> Iterable<T> nullCheck(Iterable<T> items) {121for (T item : items) {122if (item == null)123throw new NullPointerException();124}125return items;126}127}128129130