Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java
38899 views
/*1* Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.javadoc;2627import java.io.File;28import java.lang.reflect.InvocationTargetException;29import java.lang.reflect.Method;30import java.lang.reflect.Modifier;31import java.net.URL;32import java.net.URLClassLoader;3334import javax.tools.DocumentationTool;35import javax.tools.JavaFileManager;3637import com.sun.javadoc.*;38import com.sun.tools.javac.file.Locations;39import com.sun.tools.javac.util.ClientCodeException;40import com.sun.tools.javac.util.List;41import static com.sun.javadoc.LanguageVersion.*;424344/**45* Class creates, controls and invokes doclets.46*47* <p><b>This is NOT part of any supported API.48* If you write code that depends on this, you do so at your own risk.49* This code and its internal interfaces are subject to change or50* deletion without notice.</b>51*52* @author Neal Gafter (rewrite)53*/54public class DocletInvoker {5556private final Class<?> docletClass;5758private final String docletClassName;5960private final ClassLoader appClassLoader;6162private final Messager messager;6364/**65* In API mode, exceptions thrown while calling the doclet are66* propagated using ClientCodeException.67*/68private final boolean apiMode;6970private static class DocletInvokeException extends Exception {71private static final long serialVersionUID = 0;72}7374private String appendPath(String path1, String path2) {75if (path1 == null || path1.length() == 0) {76return path2 == null ? "." : path2;77} else if (path2 == null || path2.length() == 0) {78return path1;79} else {80return path1 + File.pathSeparator + path2;81}82}8384public DocletInvoker(Messager messager, Class<?> docletClass, boolean apiMode) {85this.messager = messager;86this.docletClass = docletClass;87docletClassName = docletClass.getName();88appClassLoader = null;89this.apiMode = apiMode;90}9192public DocletInvoker(Messager messager, JavaFileManager fileManager,93String docletClassName, String docletPath,94ClassLoader docletParentClassLoader,95boolean apiMode) {96this.messager = messager;97this.docletClassName = docletClassName;98this.apiMode = apiMode;99100if (fileManager != null && fileManager.hasLocation(DocumentationTool.Location.DOCLET_PATH)) {101appClassLoader = fileManager.getClassLoader(DocumentationTool.Location.DOCLET_PATH);102} else {103// construct class loader104String cpString = null; // make sure env.class.path defaults to dot105106// do prepends to get correct ordering107cpString = appendPath(System.getProperty("env.class.path"), cpString);108cpString = appendPath(System.getProperty("java.class.path"), cpString);109cpString = appendPath(docletPath, cpString);110URL[] urls = Locations.pathToURLs(cpString);111if (docletParentClassLoader == null)112appClassLoader = new URLClassLoader(urls, getDelegationClassLoader(docletClassName));113else114appClassLoader = new URLClassLoader(urls, docletParentClassLoader);115}116117// attempt to find doclet118Class<?> dc = null;119try {120dc = appClassLoader.loadClass(docletClassName);121} catch (ClassNotFoundException exc) {122messager.error(Messager.NOPOS, "main.doclet_class_not_found", docletClassName);123messager.exit();124}125docletClass = dc;126}127128/*129* Returns the delegation class loader to use when creating130* appClassLoader (used to load the doclet). The context class131* loader is the best choice, but legacy behavior was to use the132* default delegation class loader (aka system class loader).133*134* Here we favor using the context class loader. To ensure135* compatibility with existing apps, we revert to legacy136* behavior if either or both of the following conditions hold:137*138* 1) the doclet is loadable from the system class loader but not139* from the context class loader,140*141* 2) this.getClass() is loadable from the system class loader but not142* from the context class loader.143*/144private ClassLoader getDelegationClassLoader(String docletClassName) {145ClassLoader ctxCL = Thread.currentThread().getContextClassLoader();146ClassLoader sysCL = ClassLoader.getSystemClassLoader();147if (sysCL == null)148return ctxCL;149if (ctxCL == null)150return sysCL;151152// Condition 1.153try {154sysCL.loadClass(docletClassName);155try {156ctxCL.loadClass(docletClassName);157} catch (ClassNotFoundException e) {158return sysCL;159}160} catch (ClassNotFoundException e) {161}162163// Condition 2.164try {165if (getClass() == sysCL.loadClass(getClass().getName())) {166try {167if (getClass() != ctxCL.loadClass(getClass().getName()))168return sysCL;169} catch (ClassNotFoundException e) {170return sysCL;171}172}173} catch (ClassNotFoundException e) {174}175176return ctxCL;177}178179/**180* Generate documentation here. Return true on success.181*/182public boolean start(RootDoc root) {183Object retVal;184String methodName = "start";185Class<?>[] paramTypes = { RootDoc.class };186Object[] params = { root };187try {188retVal = invoke(methodName, null, paramTypes, params);189} catch (DocletInvokeException exc) {190return false;191}192if (retVal instanceof Boolean) {193return ((Boolean)retVal).booleanValue();194} else {195messager.error(Messager.NOPOS, "main.must_return_boolean",196docletClassName, methodName);197return false;198}199}200201/**202* Check for doclet added options here. Zero return means203* option not known. Positive value indicates number of204* arguments to option. Negative value means error occurred.205*/206public int optionLength(String option) {207Object retVal;208String methodName = "optionLength";209Class<?>[] paramTypes = { String.class };210Object[] params = { option };211try {212retVal = invoke(methodName, new Integer(0), paramTypes, params);213} catch (DocletInvokeException exc) {214return -1;215}216if (retVal instanceof Integer) {217return ((Integer)retVal).intValue();218} else {219messager.error(Messager.NOPOS, "main.must_return_int",220docletClassName, methodName);221return -1;222}223}224225/**226* Let doclet check that all options are OK. Returning true means227* options are OK. If method does not exist, assume true.228*/229public boolean validOptions(List<String[]> optlist) {230Object retVal;231String options[][] = optlist.toArray(new String[optlist.length()][]);232String methodName = "validOptions";233DocErrorReporter reporter = messager;234Class<?>[] paramTypes = { String[][].class, DocErrorReporter.class };235Object[] params = { options, reporter };236try {237retVal = invoke(methodName, Boolean.TRUE, paramTypes, params);238} catch (DocletInvokeException exc) {239return false;240}241if (retVal instanceof Boolean) {242return ((Boolean)retVal).booleanValue();243} else {244messager.error(Messager.NOPOS, "main.must_return_boolean",245docletClassName, methodName);246return false;247}248}249250/**251* Return the language version supported by this doclet.252* If the method does not exist in the doclet, assume version 1.1.253*/254public LanguageVersion languageVersion() {255try {256Object retVal;257String methodName = "languageVersion";258Class<?>[] paramTypes = new Class<?>[0];259Object[] params = new Object[0];260try {261retVal = invoke(methodName, JAVA_1_1, paramTypes, params);262} catch (DocletInvokeException exc) {263return JAVA_1_1;264}265if (retVal instanceof LanguageVersion) {266return (LanguageVersion)retVal;267} else {268messager.error(Messager.NOPOS, "main.must_return_languageversion",269docletClassName, methodName);270return JAVA_1_1;271}272} catch (NoClassDefFoundError ex) { // for boostrapping, no Enum class.273return null;274}275}276277/**278* Utility method for calling doclet functionality279*/280private Object invoke(String methodName, Object returnValueIfNonExistent,281Class<?>[] paramTypes, Object[] params)282throws DocletInvokeException {283Method meth;284try {285meth = docletClass.getMethod(methodName, paramTypes);286} catch (NoSuchMethodException exc) {287if (returnValueIfNonExistent == null) {288messager.error(Messager.NOPOS, "main.doclet_method_not_found",289docletClassName, methodName);290throw new DocletInvokeException();291} else {292return returnValueIfNonExistent;293}294} catch (SecurityException exc) {295messager.error(Messager.NOPOS, "main.doclet_method_not_accessible",296docletClassName, methodName);297throw new DocletInvokeException();298}299if (!Modifier.isStatic(meth.getModifiers())) {300messager.error(Messager.NOPOS, "main.doclet_method_must_be_static",301docletClassName, methodName);302throw new DocletInvokeException();303}304ClassLoader savedCCL =305Thread.currentThread().getContextClassLoader();306try {307if (appClassLoader != null) // will be null if doclet class provided via API308Thread.currentThread().setContextClassLoader(appClassLoader);309return meth.invoke(null , params);310} catch (IllegalArgumentException exc) {311messager.error(Messager.NOPOS, "main.internal_error_exception_thrown",312docletClassName, methodName, exc.toString());313throw new DocletInvokeException();314} catch (IllegalAccessException exc) {315messager.error(Messager.NOPOS, "main.doclet_method_not_accessible",316docletClassName, methodName);317throw new DocletInvokeException();318} catch (NullPointerException exc) {319messager.error(Messager.NOPOS, "main.internal_error_exception_thrown",320docletClassName, methodName, exc.toString());321throw new DocletInvokeException();322} catch (InvocationTargetException exc) {323Throwable err = exc.getTargetException();324if (apiMode)325throw new ClientCodeException(err);326if (err instanceof java.lang.OutOfMemoryError) {327messager.error(Messager.NOPOS, "main.out.of.memory");328} else {329messager.error(Messager.NOPOS, "main.exception_thrown",330docletClassName, methodName, exc.toString());331exc.getTargetException().printStackTrace();332}333throw new DocletInvokeException();334} finally {335Thread.currentThread().setContextClassLoader(savedCCL);336}337}338}339340341