src/share/classes/com/sun/tools/javadoc/api/JavadocTaskImpl.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javadoc/api/JavadocTaskImpl.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,100 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package com.sun.tools.javadoc.api;
    1.29 +
    1.30 +import com.sun.tools.javac.util.ClientCodeException;
    1.31 +import java.util.Locale;
    1.32 +import java.util.concurrent.atomic.AtomicBoolean;
    1.33 +
    1.34 +import javax.tools.DocumentationTool.DocumentationTask;
    1.35 +import javax.tools.JavaFileObject;
    1.36 +
    1.37 +import com.sun.tools.javac.util.Context;
    1.38 +import com.sun.tools.javadoc.Start;
    1.39 +import java.util.Collections;
    1.40 +
    1.41 +/**
    1.42 + * Provides access to functionality specific to the JDK documentation tool,
    1.43 + * javadoc.
    1.44 + *
    1.45 + * <p><b>This is NOT part of any supported API.
    1.46 + * If you write code that depends on this, you do so at your own
    1.47 + * risk.  This code and its internal interfaces are subject to change
    1.48 + * or deletion without notice.</b></p>
    1.49 + */
    1.50 +public class JavadocTaskImpl implements DocumentationTask {
    1.51 +    private final AtomicBoolean used = new AtomicBoolean();
    1.52 +
    1.53 +    private final Context context;
    1.54 +    private Class<?> docletClass;
    1.55 +    private Iterable<String> options;
    1.56 +    private Iterable<? extends JavaFileObject> fileObjects;
    1.57 +    private Locale locale;
    1.58 +
    1.59 +    public JavadocTaskImpl(Context context, Class<?> docletClass,
    1.60 +            Iterable<String> options, Iterable<? extends JavaFileObject> fileObjects) {
    1.61 +        this.context = context;
    1.62 +        this.docletClass = docletClass;
    1.63 +
    1.64 +        this.options = (options == null) ? Collections.<String>emptySet()
    1.65 +                : nullCheck(options);
    1.66 +        this.fileObjects = (fileObjects == null) ? Collections.<JavaFileObject>emptySet()
    1.67 +                : nullCheck(fileObjects);
    1.68 +        setLocale(Locale.getDefault());
    1.69 +    }
    1.70 +
    1.71 +    public void setLocale(Locale locale) {
    1.72 +        if (used.get())
    1.73 +            throw new IllegalStateException();
    1.74 +        this.locale = locale;
    1.75 +    }
    1.76 +
    1.77 +    public Boolean call() {
    1.78 +        if (!used.getAndSet(true)) {
    1.79 +            initContext();
    1.80 +            Start jdoc = new Start(context);
    1.81 +            try {
    1.82 +                return jdoc.begin(docletClass, options, fileObjects);
    1.83 +            } catch (ClientCodeException e) {
    1.84 +                throw new RuntimeException(e.getCause());
    1.85 +            }
    1.86 +        } else {
    1.87 +            throw new IllegalStateException("multiple calls to method 'call'");
    1.88 +        }
    1.89 +    }
    1.90 +
    1.91 +    private void initContext() {
    1.92 +        //initialize compiler's default locale
    1.93 +        context.put(Locale.class, locale);
    1.94 +    }
    1.95 +
    1.96 +    private static <T> Iterable<T> nullCheck(Iterable<T> items) {
    1.97 +        for (T item: items) {
    1.98 +            if (item == null)
    1.99 +                throw new NullPointerException();
   1.100 +        }
   1.101 +        return items;
   1.102 +    }
   1.103 +}

mercurial