JavadocTaskImpl.java revision 3233:b5d08bc0d224
11592Srgrimes/*
21592Srgrimes * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
31592Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
496196Sdes *
596196Sdes * This code is free software; you can redistribute it and/or modify it
61592Srgrimes * under the terms of the GNU General Public License version 2 only, as
796196Sdes * published by the Free Software Foundation.  Oracle designates this
896196Sdes * particular file as subject to the "Classpath" exception as provided
996196Sdes * by Oracle in the LICENSE file that accompanied this code.
1096196Sdes *
1196196Sdes * This code is distributed in the hope that it will be useful, but WITHOUT
121592Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
131592Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
141592Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
151592Srgrimes * accompanied this code).
161592Srgrimes *
171592Srgrimes * You should have received a copy of the GNU General Public License version
181592Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
191592Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20262435Sbrueffer *
211592Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
221592Srgrimes * or visit www.oracle.com if you need additional information or have any
231592Srgrimes * questions.
241592Srgrimes */
251592Srgrimes
261592Srgrimespackage jdk.javadoc.internal.api;
271592Srgrimes
281592Srgrimesimport java.util.Collections;
291592Srgrimesimport java.util.Locale;
301592Srgrimesimport java.util.concurrent.atomic.AtomicBoolean;
311592Srgrimes
321592Srgrimesimport javax.tools.DocumentationTool.DocumentationTask;
331592Srgrimesimport javax.tools.JavaFileObject;
341592Srgrimes
351592Srgrimesimport com.sun.tools.javac.util.ClientCodeException;
361592Srgrimesimport com.sun.tools.javac.util.Context;
37114624Sobrienimport com.sun.tools.javac.util.DefinedBy;
381592Srgrimesimport com.sun.tools.javac.util.DefinedBy.Api;
3929916Smarkmimport jdk.javadoc.internal.tool.Start;
401592Srgrimes
411592Srgrimes/**
421592Srgrimes * Provides access to functionality specific to the JDK documentation tool,
431592Srgrimes * javadoc.
441592Srgrimes *
4529916Smarkm * <p><b>This is NOT part of any supported API.
46114624Sobrien * If you write code that depends on this, you do so at your own
4731405Scharnier * risk.  This code and its internal interfaces are subject to change
48114624Sobrien * or deletion without notice.</b></p>
49114624Sobrien */
501592Srgrimespublic class JavadocTaskImpl implements DocumentationTask {
511592Srgrimes    private final AtomicBoolean used = new AtomicBoolean();
521592Srgrimes
531592Srgrimes    private final Context context;
541592Srgrimes    private Class<?> docletClass;
551592Srgrimes    private Iterable<String> options;
561592Srgrimes    private Iterable<? extends JavaFileObject> fileObjects;
571592Srgrimes    private Locale locale;
581592Srgrimes
591592Srgrimes    public JavadocTaskImpl(Context context, Class<?> docletClass,
601592Srgrimes            Iterable<String> options, Iterable<? extends JavaFileObject> fileObjects) {
6129916Smarkm        this.context = context;
621592Srgrimes        this.docletClass = docletClass;
631592Srgrimes
641592Srgrimes        this.options = (options == null) ? Collections.<String>emptySet()
651592Srgrimes                : nullCheck(options);
661592Srgrimes        this.fileObjects = (fileObjects == null) ? Collections.<JavaFileObject>emptySet()
671592Srgrimes                : nullCheck(fileObjects);
681592Srgrimes        setLocale(Locale.getDefault());
691592Srgrimes    }
701592Srgrimes
711592Srgrimes    @DefinedBy(Api.COMPILER)
7211486Sdg    public void setLocale(Locale locale) {
731592Srgrimes        if (used.get())
741592Srgrimes            throw new IllegalStateException();
751592Srgrimes        this.locale = locale;
7631405Scharnier    }
7731405Scharnier
7896196Sdes    @DefinedBy(Api.COMPILER)
791592Srgrimes    public Boolean call() {
801592Srgrimes        if (!used.getAndSet(true)) {
811592Srgrimes            initContext();
821592Srgrimes            Start jdoc = new Start(context);
831592Srgrimes            try {
8431405Scharnier                return jdoc.begin(docletClass, options, fileObjects);
851592Srgrimes            } catch (ClientCodeException e) {
8651433Smarkm                throw new RuntimeException(e.getCause());
871592Srgrimes            }
881592Srgrimes        } else {
891592Srgrimes            throw new IllegalStateException("multiple calls to method 'call'");
901592Srgrimes        }
91141588Sru    }
921592Srgrimes
931592Srgrimes    private void initContext() {
941592Srgrimes        //initialize compiler's default locale
951592Srgrimes        context.put(Locale.class, locale);
961592Srgrimes    }
971592Srgrimes
981592Srgrimes    private static <T> Iterable<T> nullCheck(Iterable<T> items) {
991592Srgrimes        for (T item: items) {
10011486Sdg            if (item == null)
1011592Srgrimes                throw new NullPointerException();
1021592Srgrimes        }
1031592Srgrimes        return items;
10456590Sshin    }
10556590Sshin}
10656590Sshin