JavadocTaskImpl.java revision 3999:ae88ea1b7649
150472Speter/* 237Srgrimes * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. 337Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 437Srgrimes * 527086Swosch * This code is free software; you can redistribute it and/or modify it 637Srgrimes * under the terms of the GNU General Public License version 2 only, as 737Srgrimes * published by the Free Software Foundation. Oracle designates this 837Srgrimes * particular file as subject to the "Classpath" exception as provided 937Srgrimes * by Oracle in the LICENSE file that accompanied this code. 1037Srgrimes * 1127086Swosch * This code is distributed in the hope that it will be useful, but WITHOUT 1299134Smaxim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1327086Swosch * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1437Srgrimes * version 2 for more details (a copy is included in the LICENSE file that 1599134Smaxim * accompanied this code). 1688684Sgshapiro * 1788684Sgshapiro * You should have received a copy of the GNU General Public License version 1827086Swosch * 2 along with this work; if not, write to the Free Software Foundation, 1940855Sphk * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2040855Sphk * 2137Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2237Srgrimes * or visit www.oracle.com if you need additional information or have any 2337Srgrimes * questions. 2437Srgrimes */ 2537Srgrimes 2637Srgrimespackage jdk.javadoc.internal.api; 2769191Sdougb 2837Srgrimesimport java.util.ArrayList; 2937Srgrimesimport java.util.Collections; 3069191Sdougbimport java.util.List; 3187664Sgshapiroimport java.util.Locale; 3227086Swoschimport java.util.Objects; 3327086Swoschimport java.util.concurrent.atomic.AtomicBoolean; 3437Srgrimes 3527086Swoschimport javax.tools.DocumentationTool.DocumentationTask; 3643781Sdesimport javax.tools.JavaFileObject; 3787664Sgshapiro 3899133Smaximimport com.sun.tools.javac.main.Option; 3937Srgrimesimport com.sun.tools.javac.util.ClientCodeException; 4037Srgrimesimport com.sun.tools.javac.util.Context; 4169191Sdougbimport com.sun.tools.javac.util.Options; 4227086Swoschimport jdk.javadoc.internal.tool.Start; 4337Srgrimes 4427086Swosch/** 4537Srgrimes * Provides access to functionality specific to the JDK documentation tool, 46153Srgrimes * javadoc. 47153Srgrimes * 48153Srgrimes * <p><b>This is NOT part of any supported API. 4912389Sache * If you write code that depends on this, you do so at your own 5027086Swosch * risk. This code and its internal interfaces are subject to change 5127086Swosch * or deletion without notice.</b></p> 5227086Swosch */ 5327086Swoschpublic class JavadocTaskImpl implements DocumentationTask { 5427086Swosch private final AtomicBoolean used = new AtomicBoolean(); 5527086Swosch 5627086Swosch private final Context context; 5728428Swosch private Class<?> docletClass; 5827086Swosch private Iterable<String> options; 5928428Swosch private Iterable<? extends JavaFileObject> fileObjects; 6027086Swosch private Locale locale; 6127086Swosch private List<String> addModules = new ArrayList<>(); 6228428Swosch 6328428Swosch public JavadocTaskImpl(Context context, Class<?> docletClass, 6427086Swosch Iterable<String> options, Iterable<? extends JavaFileObject> fileObjects) { 6527086Swosch this.context = context; 6627086Swosch this.docletClass = docletClass; 6727086Swosch 68110551Sgshapiro this.options = (options == null) ? Collections.emptySet() 69112573Sgshapiro : nullCheck(options); 70110551Sgshapiro this.fileObjects = (fileObjects == null) ? Collections.emptySet() 7112389Sache : nullCheck(fileObjects); 72110551Sgshapiro setLocale(Locale.getDefault()); 7327086Swosch } 7427086Swosch 75 public void setLocale(Locale locale) { 76 if (used.get()) 77 throw new IllegalStateException(); 78 this.locale = locale; 79 } 80 81 @Override 82 public void addModules(Iterable<String> moduleNames) { 83 nullCheck(moduleNames); 84 if (used.get()) 85 throw new IllegalStateException(); 86 for (String name : moduleNames) { 87 addModules.add(name); 88 } 89 } 90 91 public Boolean call() { 92 if (!used.getAndSet(true)) { 93 initContext(); 94 Start jdoc = new Start(context); 95 try { 96 return jdoc.begin(docletClass, options, fileObjects); 97 } catch (ClientCodeException e) { 98 throw new RuntimeException(e.getCause()); 99 } 100 } else { 101 throw new IllegalStateException("multiple calls to method 'call'"); 102 } 103 } 104 105 private void initContext() { 106 //initialize compiler's default locale 107 context.put(Locale.class, locale); 108 if (!addModules.isEmpty()) { 109 String names = String.join(",", addModules); 110 Options opts = Options.instance(context); 111 String prev = opts.get(Option.ADD_MODULES); 112 opts.put(Option.ADD_MODULES, (prev == null) ? names : prev + "," + names); 113 } 114 } 115 116 private static <T> Iterable<T> nullCheck(Iterable<T> items) { 117 for (T item: items) { 118 if (item == null) 119 throw new NullPointerException(); 120 } 121 return items; 122 } 123} 124