src/share/classes/com/sun/tools/javadoc/DocletInvoker.java

Tue, 06 Nov 2012 14:32:49 -0800

author
jjg
date
Tue, 06 Nov 2012 14:32:49 -0800
changeset 1397
8abc56be3131
parent 1359
25e14ad23cef
child 1411
467f4f754368
permissions
-rw-r--r--

8000612: Discrepancy between resources provided in javadoc resource files and resources required by code
Reviewed-by: bpatel

duke@1 1 /*
jjg@1357 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javadoc;
duke@1 27
jjg@912 28 import java.io.File;
jjg@1357 29 import java.lang.reflect.InvocationTargetException;
duke@1 30 import java.lang.reflect.Method;
duke@1 31 import java.lang.reflect.Modifier;
jjg@912 32 import java.net.URL;
jjg@912 33 import java.net.URLClassLoader;
duke@1 34
jjg@1357 35 import com.sun.javadoc.*;
jjg@1357 36 import com.sun.tools.javac.util.List;
jjg@1357 37 import static com.sun.javadoc.LanguageVersion.*;
jjg@1357 38
duke@1 39
duke@1 40 /**
duke@1 41 * Class creates, controls and invokes doclets.
jjg@1359 42 *
jjg@1359 43 * <p><b>This is NOT part of any supported API.
jjg@1359 44 * If you write code that depends on this, you do so at your own risk.
jjg@1359 45 * This code and its internal interfaces are subject to change or
jjg@1359 46 * deletion without notice.</b>
jjg@1359 47 *
duke@1 48 * @author Neal Gafter (rewrite)
duke@1 49 */
duke@1 50 public class DocletInvoker {
duke@1 51
jjg@74 52 private final Class<?> docletClass;
duke@1 53
duke@1 54 private final String docletClassName;
duke@1 55
duke@1 56 private final ClassLoader appClassLoader;
duke@1 57
duke@1 58 private final Messager messager;
duke@1 59
duke@1 60 private static class DocletInvokeException extends Exception {
duke@1 61 private static final long serialVersionUID = 0;
duke@1 62 }
duke@1 63
duke@1 64 private String appendPath(String path1, String path2) {
duke@1 65 if (path1 == null || path1.length() == 0) {
duke@1 66 return path2 == null ? "." : path2;
duke@1 67 } else if (path2 == null || path2.length() == 0) {
duke@1 68 return path1;
duke@1 69 } else {
duke@1 70 return path1 + File.pathSeparator + path2;
duke@1 71 }
duke@1 72 }
duke@1 73
duke@1 74 public DocletInvoker(Messager messager,
jjg@129 75 String docletClassName, String docletPath,
jjg@129 76 ClassLoader docletParentClassLoader) {
duke@1 77 this.messager = messager;
duke@1 78 this.docletClassName = docletClassName;
duke@1 79
duke@1 80 // construct class loader
duke@1 81 String cpString = null; // make sure env.class.path defaults to dot
duke@1 82
duke@1 83 // do prepends to get correct ordering
duke@1 84 cpString = appendPath(System.getProperty("env.class.path"), cpString);
duke@1 85 cpString = appendPath(System.getProperty("java.class.path"), cpString);
duke@1 86 cpString = appendPath(docletPath, cpString);
jjg@1116 87 URL[] urls = com.sun.tools.javac.file.Locations.pathToURLs(cpString);
jjg@129 88 if (docletParentClassLoader == null)
jjg@209 89 appClassLoader = new URLClassLoader(urls, getDelegationClassLoader(docletClassName));
jjg@129 90 else
jjg@129 91 appClassLoader = new URLClassLoader(urls, docletParentClassLoader);
duke@1 92
duke@1 93 // attempt to find doclet
mcimadamore@184 94 Class<?> dc = null;
duke@1 95 try {
duke@1 96 dc = appClassLoader.loadClass(docletClassName);
duke@1 97 } catch (ClassNotFoundException exc) {
duke@1 98 messager.error(null, "main.doclet_class_not_found", docletClassName);
duke@1 99 messager.exit();
duke@1 100 }
duke@1 101 docletClass = dc;
duke@1 102 }
duke@1 103
jjg@209 104 /*
jjg@209 105 * Returns the delegation class loader to use when creating
jjg@209 106 * appClassLoader (used to load the doclet). The context class
jjg@209 107 * loader is the best choice, but legacy behavior was to use the
jjg@209 108 * default delegation class loader (aka system class loader).
jjg@209 109 *
jjg@209 110 * Here we favor using the context class loader. To ensure
jjg@209 111 * compatibility with existing apps, we revert to legacy
jjg@209 112 * behavior if either or both of the following conditions hold:
jjg@209 113 *
jjg@209 114 * 1) the doclet is loadable from the system class loader but not
jjg@209 115 * from the context class loader,
jjg@209 116 *
jjg@209 117 * 2) this.getClass() is loadable from the system class loader but not
jjg@209 118 * from the context class loader.
jjg@209 119 */
jjg@209 120 private ClassLoader getDelegationClassLoader(String docletClassName) {
jjg@209 121 ClassLoader ctxCL = Thread.currentThread().getContextClassLoader();
jjg@209 122 ClassLoader sysCL = ClassLoader.getSystemClassLoader();
jjg@209 123 if (sysCL == null)
jjg@209 124 return ctxCL;
jjg@209 125 if (ctxCL == null)
jjg@209 126 return sysCL;
jjg@209 127
jjg@209 128 // Condition 1.
jjg@209 129 try {
jjg@209 130 sysCL.loadClass(docletClassName);
jjg@209 131 try {
jjg@209 132 ctxCL.loadClass(docletClassName);
jjg@209 133 } catch (ClassNotFoundException e) {
jjg@209 134 return sysCL;
jjg@209 135 }
jjg@209 136 } catch (ClassNotFoundException e) {
jjg@209 137 }
jjg@209 138
jjg@209 139 // Condition 2.
jjg@209 140 try {
jjg@209 141 if (getClass() == sysCL.loadClass(getClass().getName())) {
jjg@209 142 try {
jjg@209 143 if (getClass() != ctxCL.loadClass(getClass().getName()))
jjg@209 144 return sysCL;
jjg@209 145 } catch (ClassNotFoundException e) {
jjg@209 146 return sysCL;
jjg@209 147 }
jjg@209 148 }
jjg@209 149 } catch (ClassNotFoundException e) {
jjg@209 150 }
jjg@209 151
jjg@209 152 return ctxCL;
jjg@209 153 }
jjg@209 154
duke@1 155 /**
duke@1 156 * Generate documentation here. Return true on success.
duke@1 157 */
duke@1 158 public boolean start(RootDoc root) {
duke@1 159 Object retVal;
duke@1 160 String methodName = "start";
jjg@584 161 Class<?>[] paramTypes = { RootDoc.class };
jjg@584 162 Object[] params = { root };
duke@1 163 try {
duke@1 164 retVal = invoke(methodName, null, paramTypes, params);
duke@1 165 } catch (DocletInvokeException exc) {
duke@1 166 return false;
duke@1 167 }
duke@1 168 if (retVal instanceof Boolean) {
duke@1 169 return ((Boolean)retVal).booleanValue();
duke@1 170 } else {
duke@1 171 messager.error(null, "main.must_return_boolean",
duke@1 172 docletClassName, methodName);
duke@1 173 return false;
duke@1 174 }
duke@1 175 }
duke@1 176
duke@1 177 /**
duke@1 178 * Check for doclet added options here. Zero return means
duke@1 179 * option not known. Positive value indicates number of
duke@1 180 * arguments to option. Negative value means error occurred.
duke@1 181 */
duke@1 182 public int optionLength(String option) {
duke@1 183 Object retVal;
duke@1 184 String methodName = "optionLength";
jjg@584 185 Class<?>[] paramTypes = { String.class };
jjg@584 186 Object[] params = { option };
duke@1 187 try {
duke@1 188 retVal = invoke(methodName, new Integer(0), paramTypes, params);
duke@1 189 } catch (DocletInvokeException exc) {
duke@1 190 return -1;
duke@1 191 }
duke@1 192 if (retVal instanceof Integer) {
duke@1 193 return ((Integer)retVal).intValue();
duke@1 194 } else {
duke@1 195 messager.error(null, "main.must_return_int",
duke@1 196 docletClassName, methodName);
duke@1 197 return -1;
duke@1 198 }
duke@1 199 }
duke@1 200
duke@1 201 /**
duke@1 202 * Let doclet check that all options are OK. Returning true means
duke@1 203 * options are OK. If method does not exist, assume true.
duke@1 204 */
duke@1 205 public boolean validOptions(List<String[]> optlist) {
duke@1 206 Object retVal;
duke@1 207 String options[][] = optlist.toArray(new String[optlist.length()][]);
duke@1 208 String methodName = "validOptions";
duke@1 209 DocErrorReporter reporter = messager;
jjg@584 210 Class<?>[] paramTypes = { String[][].class, DocErrorReporter.class };
jjg@584 211 Object[] params = { options, reporter };
duke@1 212 try {
duke@1 213 retVal = invoke(methodName, Boolean.TRUE, paramTypes, params);
duke@1 214 } catch (DocletInvokeException exc) {
duke@1 215 return false;
duke@1 216 }
duke@1 217 if (retVal instanceof Boolean) {
duke@1 218 return ((Boolean)retVal).booleanValue();
duke@1 219 } else {
duke@1 220 messager.error(null, "main.must_return_boolean",
duke@1 221 docletClassName, methodName);
duke@1 222 return false;
duke@1 223 }
duke@1 224 }
duke@1 225
duke@1 226 /**
duke@1 227 * Return the language version supported by this doclet.
duke@1 228 * If the method does not exist in the doclet, assume version 1.1.
duke@1 229 */
duke@1 230 public LanguageVersion languageVersion() {
duke@1 231 try {
duke@1 232 Object retVal;
duke@1 233 String methodName = "languageVersion";
mcimadamore@184 234 Class<?>[] paramTypes = new Class<?>[0];
duke@1 235 Object[] params = new Object[0];
duke@1 236 try {
duke@1 237 retVal = invoke(methodName, JAVA_1_1, paramTypes, params);
duke@1 238 } catch (DocletInvokeException exc) {
duke@1 239 return JAVA_1_1;
duke@1 240 }
duke@1 241 if (retVal instanceof LanguageVersion) {
duke@1 242 return (LanguageVersion)retVal;
duke@1 243 } else {
duke@1 244 messager.error(null, "main.must_return_languageversion",
duke@1 245 docletClassName, methodName);
duke@1 246 return JAVA_1_1;
duke@1 247 }
duke@1 248 } catch (NoClassDefFoundError ex) { // for boostrapping, no Enum class.
duke@1 249 return null;
duke@1 250 }
duke@1 251 }
duke@1 252
duke@1 253 /**
duke@1 254 * Utility method for calling doclet functionality
duke@1 255 */
duke@1 256 private Object invoke(String methodName, Object returnValueIfNonExistent,
mcimadamore@184 257 Class<?>[] paramTypes, Object[] params)
duke@1 258 throws DocletInvokeException {
duke@1 259 Method meth;
duke@1 260 try {
duke@1 261 meth = docletClass.getMethod(methodName, paramTypes);
duke@1 262 } catch (NoSuchMethodException exc) {
duke@1 263 if (returnValueIfNonExistent == null) {
duke@1 264 messager.error(null, "main.doclet_method_not_found",
duke@1 265 docletClassName, methodName);
duke@1 266 throw new DocletInvokeException();
duke@1 267 } else {
duke@1 268 return returnValueIfNonExistent;
duke@1 269 }
duke@1 270 } catch (SecurityException exc) {
duke@1 271 messager.error(null, "main.doclet_method_not_accessible",
duke@1 272 docletClassName, methodName);
duke@1 273 throw new DocletInvokeException();
duke@1 274 }
duke@1 275 if (!Modifier.isStatic(meth.getModifiers())) {
duke@1 276 messager.error(null, "main.doclet_method_must_be_static",
duke@1 277 docletClassName, methodName);
duke@1 278 throw new DocletInvokeException();
duke@1 279 }
jjg@209 280 ClassLoader savedCCL =
jjg@209 281 Thread.currentThread().getContextClassLoader();
duke@1 282 try {
duke@1 283 Thread.currentThread().setContextClassLoader(appClassLoader);
duke@1 284 return meth.invoke(null , params);
duke@1 285 } catch (IllegalArgumentException exc) {
duke@1 286 messager.error(null, "main.internal_error_exception_thrown",
duke@1 287 docletClassName, methodName, exc.toString());
duke@1 288 throw new DocletInvokeException();
duke@1 289 } catch (IllegalAccessException exc) {
duke@1 290 messager.error(null, "main.doclet_method_not_accessible",
duke@1 291 docletClassName, methodName);
duke@1 292 throw new DocletInvokeException();
duke@1 293 } catch (NullPointerException exc) {
duke@1 294 messager.error(null, "main.internal_error_exception_thrown",
duke@1 295 docletClassName, methodName, exc.toString());
duke@1 296 throw new DocletInvokeException();
duke@1 297 } catch (InvocationTargetException exc) {
duke@1 298 Throwable err = exc.getTargetException();
duke@1 299 if (err instanceof java.lang.OutOfMemoryError) {
duke@1 300 messager.error(null, "main.out.of.memory");
duke@1 301 } else {
duke@1 302 messager.error(null, "main.exception_thrown",
duke@1 303 docletClassName, methodName, exc.toString());
duke@1 304 exc.getTargetException().printStackTrace();
duke@1 305 }
duke@1 306 throw new DocletInvokeException();
jjg@209 307 } finally {
jjg@209 308 Thread.currentThread().setContextClassLoader(savedCCL);
duke@1 309 }
duke@1 310 }
duke@1 311 }

mercurial