JavadocTaskImpl.java revision 3828:d30434bde0a8
155682Smarkm/*
2233294Sstas * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
355682Smarkm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
455682Smarkm *
5233294Sstas * This code is free software; you can redistribute it and/or modify it
655682Smarkm * under the terms of the GNU General Public License version 2 only, as
755682Smarkm * published by the Free Software Foundation.  Oracle designates this
855682Smarkm * particular file as subject to the "Classpath" exception as provided
9233294Sstas * by Oracle in the LICENSE file that accompanied this code.
1055682Smarkm *
1155682Smarkm * This code is distributed in the hope that it will be useful, but WITHOUT
12233294Sstas * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1355682Smarkm * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1455682Smarkm * version 2 for more details (a copy is included in the LICENSE file that
1555682Smarkm * accompanied this code).
16233294Sstas *
1755682Smarkm * You should have received a copy of the GNU General Public License version
1855682Smarkm * 2 along with this work; if not, write to the Free Software Foundation,
1955682Smarkm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20233294Sstas *
2155682Smarkm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2255682Smarkm * or visit www.oracle.com if you need additional information or have any
2355682Smarkm * questions.
2455682Smarkm */
2555682Smarkm
2655682Smarkmpackage jdk.javadoc.internal.api;
2755682Smarkm
2855682Smarkmimport java.util.Collections;
2955682Smarkmimport java.util.Locale;
3055682Smarkmimport java.util.concurrent.atomic.AtomicBoolean;
3155682Smarkm
3255682Smarkmimport javax.tools.DocumentationTool.DocumentationTask;
3355682Smarkmimport javax.tools.JavaFileObject;
3455682Smarkm
3555682Smarkmimport com.sun.tools.javac.util.ClientCodeException;
3655682Smarkmimport com.sun.tools.javac.util.Context;
3772445Sassarimport jdk.javadoc.internal.tool.Start;
3855682Smarkm
3972445Sassar/**
4072445Sassar * Provides access to functionality specific to the JDK documentation tool,
4172445Sassar * javadoc.
4272445Sassar *
4372445Sassar * <p><b>This is NOT part of any supported API.
4472445Sassar * If you write code that depends on this, you do so at your own
4572445Sassar * risk.  This code and its internal interfaces are subject to change
4672445Sassar * or deletion without notice.</b></p>
4772445Sassar */
4872445Sassarpublic class JavadocTaskImpl implements DocumentationTask {
4972445Sassar    private final AtomicBoolean used = new AtomicBoolean();
5072445Sassar
5172445Sassar    private final Context context;
5272445Sassar    private Class<?> docletClass;
5372445Sassar    private Iterable<String> options;
5472445Sassar    private Iterable<? extends JavaFileObject> fileObjects;
5572445Sassar    private Locale locale;
5678527Sassar
5778527Sassar    public JavadocTaskImpl(Context context, Class<?> docletClass,
5872445Sassar            Iterable<String> options, Iterable<? extends JavaFileObject> fileObjects) {
5972445Sassar        this.context = context;
6072445Sassar        this.docletClass = docletClass;
61233294Sstas
6272445Sassar        this.options = (options == null) ? Collections.emptySet()
6372445Sassar                : nullCheck(options);
6472445Sassar        this.fileObjects = (fileObjects == null) ? Collections.emptySet()
6572445Sassar                : nullCheck(fileObjects);
6672445Sassar        setLocale(Locale.getDefault());
6772445Sassar    }
6872445Sassar
6972445Sassar    public void setLocale(Locale locale) {
7072445Sassar        if (used.get())
7172445Sassar            throw new IllegalStateException();
7272445Sassar        this.locale = locale;
7372445Sassar    }
7472445Sassar
75233294Sstas    public Boolean call() {
7672445Sassar        if (!used.getAndSet(true)) {
7772445Sassar            initContext();
7872445Sassar            Start jdoc = new Start(context);
7972445Sassar            try {
8072445Sassar                return jdoc.begin(docletClass, options, fileObjects);
8172445Sassar            } catch (ClientCodeException e) {
8272445Sassar                throw new RuntimeException(e.getCause());
8372445Sassar            }
8472445Sassar        } else {
8572445Sassar            throw new IllegalStateException("multiple calls to method 'call'");
8672445Sassar        }
8772445Sassar    }
8872445Sassar
8972445Sassar    private void initContext() {
9072445Sassar        //initialize compiler's default locale
9172445Sassar        context.put(Locale.class, locale);
9272445Sassar    }
9372445Sassar
9472445Sassar    private static <T> Iterable<T> nullCheck(Iterable<T> items) {
9572445Sassar        for (T item: items) {
9672445Sassar            if (item == null)
9772445Sassar                throw new NullPointerException();
9872445Sassar        }
9972445Sassar        return items;
10072445Sassar    }
10172445Sassar}
10272445Sassar