< prev index next >

src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java

Print this page

        

@@ -372,11 +372,11 @@
             }
         }
 
         // locale, doclet and maybe taglet, needs to be determined first
         try {
-            doclet = preprocess(fileManager, options);
+            docletClass = preprocess(fileManager, options);
         } catch (ToolException te) {
             if (!te.result.isOK()) {
                 if (te.message != null) {
                     messager.printError(te.message);
                 }

@@ -391,10 +391,28 @@
             oe.m.run();
             Throwable t = oe.getCause();
             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 {
             result = parseAndExecute(options, fileObjects);
         } catch (com.sun.tools.javac.main.Option.InvalidValueException e) {

@@ -629,11 +647,11 @@
             throw new OptionException(ERROR, this::usage, text);
         }
         return idx;
     }
 
-    private Doclet preprocess(JavaFileManager jfm,
+    private Class<?> preprocess(JavaFileManager jfm,
             List<String> argv) throws ToolException, OptionException {
         // doclet specifying arguments
         String userDocletPath = null;
         String userDocletName = null;
 

@@ -686,81 +704,75 @@
                     userTagletPath.add(new File(pathname));
                 }
             }
         }
 
+        // 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(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);
+        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));
                         }
                     }
-                    cl = fileManager.getClassLoader(DOCLET_PATH);
-                    if (cl == null) {
-                        // despite doclet specified on cmdline no classloader found!
+                    try {
+                        ((StandardJavaFileManager)fileManager).setLocation(DOCLET_PATH, paths);
+                    } catch (IOException ioe) {
                         if (apiMode) {
-                            throw new IllegalArgumentException("Could not obtain classloader to load "
-
-                                    + userDocletPath);
+                            throw new IllegalArgumentException("Could not set location for " +
+                                    userDocletPath, ioe);
                         }
-                        String text = messager.getText("main.doclet_no_classloader_found",
-                                userDocletName);
-                        throw new ToolException(CMDERR, text);
+                        String text = messager.getText("main.doclet_could_not_set_location",
+                                userDocletPath);
+                        throw new ToolException(CMDERR, text, ioe);
                     }
                 }
-                docletClass = loadDocletClass(userDocletName, cl);
-            } else if (docletName != null){
-                docletClass = loadDocletClass(docletName, getClass().getClassLoader());
-            } else {
-                docletClass = StdDoclet;
+                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);
+                }
             }
-        }
-
-        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) {
+                return cl.loadClass(userDocletName);
+            } catch (ClassNotFoundException cnfe) {
                 if (apiMode) {
-                    throw new ClientCodeException(exc);
+                    throw new IllegalArgumentException("Cannot find doclet class " + userDocletName,
+                            cnfe);
                 }
-                String text = messager.getText("main.could_not_instantiate_class", docletClass.getName());
-                throw new ToolException(ERROR, text);
+                String text = messager.getText("main.doclet_class_not_found", userDocletName);
+                throw new ToolException(CMDERR, text, cnfe);
             }
-        } 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, ClassLoader classLoader) throws ToolException {
+    private Class<?> loadDocletClass(String docletName) throws ToolException {
         try {
-            return classLoader == null ? Class.forName(docletName) : classLoader.loadClass(docletName);
+            return Class.forName(docletName, true, getClass().getClassLoader());
         } catch (ClassNotFoundException cnfe) {
             if (apiMode) {
                 throw new IllegalArgumentException("Cannot find doclet class " + docletName);
             }
             String text = messager.getText("main.doclet_class_not_found", docletName);
< prev index next >