8220382: Cleanup doclet instantiation
authorpmuthuswamy
Wed, 17 Apr 2019 12:43:41 +0530
changeset 54561 6c4d8b7128d5
parent 54560 8b2f797e3edb
child 54562 1af5c0e68381
8220382: Cleanup doclet instantiation Reviewed-by: jjg
src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java	Wed Apr 17 08:15:09 2019 +0200
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java	Wed Apr 17 12:43:41 2019 +0530
@@ -374,7 +374,7 @@
 
         // locale, doclet and maybe taglet, needs to be determined first
         try {
-            docletClass = preprocess(fileManager, options);
+            doclet = preprocess(fileManager, options);
         } catch (ToolException te) {
             if (!te.result.isOK()) {
                 if (te.message != null) {
@@ -393,24 +393,6 @@
             dumpStack(t == null ? oe : t);
             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;
         try {
@@ -649,7 +631,7 @@
         return idx;
     }
 
-    private Class<?> preprocess(JavaFileManager jfm,
+    private Doclet preprocess(JavaFileManager jfm,
             List<String> argv) throws ToolException, OptionException {
         // doclet specifying arguments
         String userDocletPath = null;
@@ -706,71 +688,77 @@
             }
         }
 
-        // 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,
         // and load it.
-        if (userDocletName != null) {
-            ClassLoader cl = classLoader;
-            if (cl == null) {
-                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));
+        if(docletClass == null) {
+            if (userDocletName != null) {
+                ClassLoader cl = classLoader;
+                if (cl == null) {
+                    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);
                         }
                     }
-                    try {
-                        ((StandardJavaFileManager)fileManager).setLocation(DOCLET_PATH, paths);
-                    } catch (IOException ioe) {
+                    cl = fileManager.getClassLoader(DOCLET_PATH);
+                    if (cl == null) {
+                        // despite doclet specified on cmdline no classloader found!
                         if (apiMode) {
-                            throw new IllegalArgumentException("Could not set location for " +
-                                    userDocletPath, ioe);
+                            throw new IllegalArgumentException("Could not obtain classloader to load "
+
+                                    + userDocletPath);
                         }
-                        String text = messager.getText("main.doclet_could_not_set_location",
-                                userDocletPath);
-                        throw new ToolException(CMDERR, text, ioe);
+                        String text = messager.getText("main.doclet_no_classloader_found",
+                                userDocletName);
+                        throw new ToolException(CMDERR, text);
                     }
                 }
-                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);
-                }
-            }
-            try {
-                return cl.loadClass(userDocletName);
-            } catch (ClassNotFoundException cnfe) {
-                if (apiMode) {
-                    throw new IllegalArgumentException("Cannot find doclet class " + userDocletName,
-                            cnfe);
-                }
-                String text = messager.getText("main.doclet_class_not_found", userDocletName);
-                throw new ToolException(CMDERR, text, cnfe);
+                docletClass = loadDocletClass(userDocletName, cl);
+            } else if (docletName != null){
+                docletClass = loadDocletClass(docletName, getClass().getClassLoader());
+            } else {
+                docletClass = StdDoclet;
             }
         }
 
-        // Step 4: we have a doclet, try loading it
-        if (docletName != null) {
-            return loadDocletClass(docletName);
+        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);
+                }
+                String text = messager.getText("main.could_not_instantiate_class", docletClass.getName());
+                throw new ToolException(ERROR, text);
+            }
+        } else {
+            String text = messager.getText("main.not_a_doclet", docletClass.getName());
+            throw new ToolException(ERROR, text);
         }
-
-        // finally
-        return StdDoclet;
+        return doclet;
     }
 
-    private Class<?> loadDocletClass(String docletName) throws ToolException {
+    private Class<?> loadDocletClass(String docletName, ClassLoader classLoader) throws ToolException {
         try {
-            return Class.forName(docletName, true, getClass().getClassLoader());
+            return classLoader == null ? Class.forName(docletName) : classLoader.loadClass(docletName);
         } catch (ClassNotFoundException cnfe) {
             if (apiMode) {
                 throw new IllegalArgumentException("Cannot find doclet class " + docletName);