8220382: Cleanup doclet instantiation

Reviewed-by: jjg
This commit is contained in:
Priya Lakshmi Muthuswamy 2019-04-17 12:43:41 +05:30
parent 6493a50643
commit a4a6c1714d

View File

@ -374,7 +374,7 @@ public class Start extends ToolOption.Helper {
// locale, doclet and maybe taglet, needs to be determined first // locale, doclet and maybe taglet, needs to be determined first
try { try {
docletClass = preprocess(fileManager, options); doclet = preprocess(fileManager, options);
} catch (ToolException te) { } catch (ToolException te) {
if (!te.result.isOK()) { if (!te.result.isOK()) {
if (te.message != null) { if (te.message != null) {
@ -393,24 +393,6 @@ public class Start extends ToolOption.Helper {
dumpStack(t == null ? oe : t); dumpStack(t == null ? oe : t);
return oe.result; return oe.result;
} }
if (jdk.javadoc.doclet.Doclet.class.isAssignableFrom(docletClass)) {
// no need to dispatch to old, safe to init now
initMessager();
messager.setLocale(locale);
try {
Object o = docletClass.getConstructor().newInstance();
doclet = (Doclet) o;
} catch (ReflectiveOperationException exc) {
if (apiMode) {
throw new ClientCodeException(exc);
}
error("main.could_not_instantiate_class", docletClass.getName());
return ERROR;
}
} else {
error("main.not_a_doclet", docletClass.getName());
return ERROR;
}
Result result = OK; Result result = OK;
try { try {
@ -649,7 +631,7 @@ public class Start extends ToolOption.Helper {
return idx; return idx;
} }
private Class<?> preprocess(JavaFileManager jfm, private Doclet preprocess(JavaFileManager jfm,
List<String> argv) throws ToolException, OptionException { List<String> argv) throws ToolException, OptionException {
// doclet specifying arguments // doclet specifying arguments
String userDocletPath = null; String userDocletPath = null;
@ -706,71 +688,77 @@ public class Start extends ToolOption.Helper {
} }
} }
// Step 2: a doclet is provided, nothing more to do.
if (docletClass != null) {
return docletClass;
}
// Step 3: doclet name specified ? if so find a ClassLoader, // Step 3: doclet name specified ? if so find a ClassLoader,
// and load it. // and load it.
if (userDocletName != null) { if(docletClass == null) {
ClassLoader cl = classLoader; if (userDocletName != null) {
if (cl == null) { ClassLoader cl = classLoader;
if (!fileManager.hasLocation(DOCLET_PATH)) {
List<File> paths = new ArrayList<>();
if (userDocletPath != null) {
for (String pathname : userDocletPath.split(File.pathSeparator)) {
paths.add(new File(pathname));
}
}
try {
((StandardJavaFileManager)fileManager).setLocation(DOCLET_PATH, paths);
} catch (IOException ioe) {
if (apiMode) {
throw new IllegalArgumentException("Could not set location for " +
userDocletPath, ioe);
}
String text = messager.getText("main.doclet_could_not_set_location",
userDocletPath);
throw new ToolException(CMDERR, text, ioe);
}
}
cl = fileManager.getClassLoader(DOCLET_PATH);
if (cl == null) { if (cl == null) {
// despite doclet specified on cmdline no classloader found! if (!fileManager.hasLocation(DOCLET_PATH)) {
if (apiMode) { List<File> paths = new ArrayList<>();
throw new IllegalArgumentException("Could not obtain classloader to load " if (userDocletPath != null) {
+ userDocletPath); for (String pathname : userDocletPath.split(File.pathSeparator)) {
paths.add(new File(pathname));
}
}
try {
((StandardJavaFileManager)fileManager).setLocation(DOCLET_PATH, paths);
} catch (IOException ioe) {
if (apiMode) {
throw new IllegalArgumentException("Could not set location for " +
userDocletPath, ioe);
}
String text = messager.getText("main.doclet_could_not_set_location",
userDocletPath);
throw new ToolException(CMDERR, text, ioe);
}
}
cl = fileManager.getClassLoader(DOCLET_PATH);
if (cl == null) {
// despite doclet specified on cmdline no classloader found!
if (apiMode) {
throw new IllegalArgumentException("Could not obtain classloader to load "
+ userDocletPath);
}
String text = messager.getText("main.doclet_no_classloader_found",
userDocletName);
throw new ToolException(CMDERR, text);
} }
String text = messager.getText("main.doclet_no_classloader_found",
userDocletName);
throw new ToolException(CMDERR, text);
} }
docletClass = loadDocletClass(userDocletName, cl);
} else if (docletName != null){
docletClass = loadDocletClass(docletName, getClass().getClassLoader());
} else {
docletClass = StdDoclet;
} }
}
if (jdk.javadoc.doclet.Doclet.class.isAssignableFrom(docletClass)) {
// no need to dispatch to old, safe to init now
initMessager();
messager.setLocale(locale);
try { try {
return cl.loadClass(userDocletName); Object o = docletClass.getConstructor().newInstance();
} catch (ClassNotFoundException cnfe) { doclet = (Doclet) o;
} catch (ReflectiveOperationException exc) {
if (apiMode) { if (apiMode) {
throw new IllegalArgumentException("Cannot find doclet class " + userDocletName, throw new ClientCodeException(exc);
cnfe);
} }
String text = messager.getText("main.doclet_class_not_found", userDocletName); String text = messager.getText("main.could_not_instantiate_class", docletClass.getName());
throw new ToolException(CMDERR, text, cnfe); throw new ToolException(ERROR, text);
} }
} else {
String text = messager.getText("main.not_a_doclet", docletClass.getName());
throw new ToolException(ERROR, text);
} }
return doclet;
// Step 4: we have a doclet, try loading it
if (docletName != null) {
return loadDocletClass(docletName);
}
// finally
return StdDoclet;
} }
private Class<?> loadDocletClass(String docletName) throws ToolException { private Class<?> loadDocletClass(String docletName, ClassLoader classLoader) throws ToolException {
try { try {
return Class.forName(docletName, true, getClass().getClassLoader()); return classLoader == null ? Class.forName(docletName) : classLoader.loadClass(docletName);
} catch (ClassNotFoundException cnfe) { } catch (ClassNotFoundException cnfe) {
if (apiMode) { if (apiMode) {
throw new IllegalArgumentException("Cannot find doclet class " + docletName); throw new IllegalArgumentException("Cannot find doclet class " + docletName);