001/*
002 * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package org.jdrupes.mdoclet.internal.api;
027
028import java.util.ArrayList;
029import java.util.List;
030import java.util.Locale;
031import java.util.Set;
032import java.util.concurrent.atomic.AtomicBoolean;
033
034import javax.tools.DocumentationTool.DocumentationTask;
035
036import jdk.javadoc.internal.tool.Start;
037
038import javax.tools.JavaFileObject;
039
040import com.sun.tools.javac.main.Option;
041import com.sun.tools.javac.util.ClientCodeException;
042import com.sun.tools.javac.util.Context;
043import com.sun.tools.javac.util.Options;
044
045/**
046 * Provides access to functionality specific to the JDK documentation tool,
047 * javadoc.
048 */
049public class JavadocTaskImpl implements DocumentationTask {
050    private final AtomicBoolean used = new AtomicBoolean();
051
052    private final Context context;
053    private final Class<?> docletClass;
054    private final Iterable<String> options;
055    private final Iterable<? extends JavaFileObject> fileObjects;
056    private Locale locale;
057    private final List<String> addModules = new ArrayList<>();
058
059    public JavadocTaskImpl(Context context,
060            Class<?> docletClass,
061            Iterable<String> options,
062            Iterable<? extends JavaFileObject> fileObjects) {
063        this.context = context;
064        this.docletClass = docletClass;
065
066        this.options = (options == null) ? Set.of()
067            : nullCheck(options);
068        this.fileObjects = (fileObjects == null) ? Set.of()
069            : nullCheck(fileObjects);
070        setLocale(Locale.getDefault());
071    }
072
073    @Override
074    public void setLocale(Locale locale) {
075        if (used.get()) {
076            throw new IllegalStateException();
077        }
078        this.locale = locale;
079    }
080
081    @Override
082    public void addModules(Iterable<String> moduleNames) {
083        nullCheck(moduleNames);
084        if (used.get()) {
085            throw new IllegalStateException();
086        }
087        for (String name : moduleNames) {
088            addModules.add(name);
089        }
090    }
091
092    @Override
093    public Boolean call() {
094        if (used.getAndSet(true)) {
095            throw new IllegalStateException("multiple calls to method 'call'");
096        }
097        initContext();
098        Start jdoc = new Start(context);
099        try {
100            return jdoc.begin(docletClass, options, fileObjects);
101        } catch (ClientCodeException e) {
102            throw new RuntimeException(e.getCause());
103        }
104    }
105
106    private void initContext() {
107        // initialize compiler's default locale
108        context.put(Locale.class, locale);
109        if (!addModules.isEmpty()) {
110            String names = String.join(",", addModules);
111            Options opts = Options.instance(context);
112            String prev = opts.get(Option.ADD_MODULES);
113            opts.put(Option.ADD_MODULES,
114                (prev == null) ? names : prev + "," + names);
115        }
116    }
117
118    private static <T> Iterable<T> nullCheck(Iterable<T> items) {
119        for (T item : items) {
120            if (item == null)
121                throw new NullPointerException();
122        }
123        return items;
124    }
125}