6748546: javadoc API should be classloader-friendly
authorjjg
Mon, 06 Oct 2008 16:57:15 -0700
changeset 1365 ced0e3c4678f
parent 1364 5ee66e9b9a00
child 1366 62c548002ecb
child 1467 290837884931
6748546: javadoc API should be classloader-friendly Reviewed-by: bpatel
langtools/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java
langtools/src/share/classes/com/sun/tools/javadoc/Main.java
langtools/src/share/classes/com/sun/tools/javadoc/Start.java
--- a/langtools/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java	Sat Oct 04 22:59:05 2008 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java	Mon Oct 06 16:57:15 2008 -0700
@@ -32,7 +32,6 @@
 import com.sun.tools.javac.util.List;
 
 import java.net.*;
-import java.lang.OutOfMemoryError;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.lang.reflect.InvocationTargetException;
@@ -70,7 +69,8 @@
     }
 
     public DocletInvoker(Messager messager,
-                         String docletClassName, String docletPath) {
+                         String docletClassName, String docletPath,
+                         ClassLoader docletParentClassLoader) {
         this.messager = messager;
         this.docletClassName = docletClassName;
 
@@ -82,7 +82,10 @@
         cpString = appendPath(System.getProperty("java.class.path"), cpString);
         cpString = appendPath(docletPath, cpString);
         URL[] urls = pathToURLs(cpString);
-        appClassLoader = new URLClassLoader(urls);
+        if (docletParentClassLoader == null)
+            appClassLoader = new URLClassLoader(urls);
+        else
+            appClassLoader = new URLClassLoader(urls, docletParentClassLoader);
 
         // attempt to find doclet
         Class dc = null;
--- a/langtools/src/share/classes/com/sun/tools/javadoc/Main.java	Sat Oct 04 22:59:05 2008 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/Main.java	Mon Oct 06 16:57:15 2008 -0700
@@ -61,6 +61,21 @@
 
     /**
      * Programmatic interface.
+     * @param args   The command line parameters.
+     * @param docletParentClassLoader The parent class loader used when
+     *  creating the doclet classloader. If null, the class loader used
+     *  to instantiate doclets will be created without specifying a parent
+     *  class loader.
+     * @return The return code.
+     * @since 1.7
+     */
+    public static int execute(ClassLoader docletParentClassLoader, String... args) {
+        Start jdoc = new Start(docletParentClassLoader);
+        return jdoc.begin(args);
+    }
+
+    /**
+     * Programmatic interface.
      * @param programName  Name of the program (for error messages).
      * @param args   The command line parameters.
      * @return The return code.
@@ -73,6 +88,22 @@
     /**
      * Programmatic interface.
      * @param programName  Name of the program (for error messages).
+     * @param args   The command line parameters.
+     * @param docletParentClassLoader The parent class loader used when
+     *  creating the doclet classloader. If null, the class loader used
+     *  to instantiate doclets will be created without specifying a parent
+     *  class loader.
+     * @return The return code.
+     * @since 1.7
+     */
+    public static int execute(String programName, ClassLoader docletParentClassLoader, String... args) {
+        Start jdoc = new Start(programName, docletParentClassLoader);
+        return jdoc.begin(args);
+    }
+
+    /**
+     * Programmatic interface.
+     * @param programName  Name of the program (for error messages).
      * @param defaultDocletClassName  Fully qualified class name.
      * @param args   The command line parameters.
      * @return The return code.
@@ -87,6 +118,26 @@
     /**
      * Programmatic interface.
      * @param programName  Name of the program (for error messages).
+     * @param defaultDocletClassName  Fully qualified class name.
+     * @param docletParentClassLoader The parent class loader used when
+     *  creating the doclet classloader. If null, the class loader used
+     *  to instantiate doclets will be created without specifying a parent
+     *  class loader.
+     * @param args   The command line parameters.
+     * @return The return code.
+     * @since 1.7
+     */
+    public static int execute(String programName,
+                              String defaultDocletClassName,
+                              ClassLoader docletParentClassLoader,
+                              String... args) {
+        Start jdoc = new Start(programName, defaultDocletClassName, docletParentClassLoader);
+        return jdoc.begin(args);
+    }
+
+    /**
+     * Programmatic interface.
+     * @param programName  Name of the program (for error messages).
      * @param errWriter    PrintWriter to receive error messages.
      * @param warnWriter    PrintWriter to receive error messages.
      * @param noticeWriter    PrintWriter to receive error messages.
@@ -105,4 +156,33 @@
                                defaultDocletClassName);
         return jdoc.begin(args);
     }
+
+    /**
+     * Programmatic interface.
+     * @param programName  Name of the program (for error messages).
+     * @param errWriter    PrintWriter to receive error messages.
+     * @param warnWriter    PrintWriter to receive error messages.
+     * @param noticeWriter    PrintWriter to receive error messages.
+     * @param defaultDocletClassName  Fully qualified class name.
+     * @param docletParentClassLoader The parent class loader used when
+     *  creating the doclet classloader. If null, the class loader used
+     *  to instantiate doclets will be created without specifying a parent
+     *  class loader.
+     * @param args   The command line parameters.
+     * @return The return code.
+     * @since 1.7
+     */
+    public static int execute(String programName,
+                              PrintWriter errWriter,
+                              PrintWriter warnWriter,
+                              PrintWriter noticeWriter,
+                              String defaultDocletClassName,
+                              ClassLoader docletParentClassLoader,
+                              String... args) {
+        Start jdoc = new Start(programName,
+                               errWriter, warnWriter, noticeWriter,
+                               defaultDocletClassName,
+                               docletParentClassLoader);
+        return jdoc.begin(args);
+    }
 }
--- a/langtools/src/share/classes/com/sun/tools/javadoc/Start.java	Sat Oct 04 22:59:05 2008 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/Start.java	Mon Oct 06 16:57:15 2008 -0700
@@ -54,10 +54,8 @@
     /** Context for this invocation. */
     private final Context context;
 
-    /**
-     * Name of the program
-     */
     private final String defaultDocletClassName;
+    private final ClassLoader docletParentClassLoader;
 
     private static final String javadocName = "javadoc";
 
@@ -91,21 +89,45 @@
           PrintWriter warnWriter,
           PrintWriter noticeWriter,
           String defaultDocletClassName) {
+        this(programName, errWriter, warnWriter, noticeWriter, defaultDocletClassName, null);
+    }
+
+    Start(String programName,
+          PrintWriter errWriter,
+          PrintWriter warnWriter,
+          PrintWriter noticeWriter,
+          String defaultDocletClassName,
+          ClassLoader docletParentClassLoader) {
         context = new Context();
         messager = new Messager(context, programName, errWriter, warnWriter, noticeWriter);
         this.defaultDocletClassName = defaultDocletClassName;
+        this.docletParentClassLoader = docletParentClassLoader;
     }
 
     Start(String programName, String defaultDocletClassName) {
+        this(programName, defaultDocletClassName, null);
+    }
+
+    Start(String programName, String defaultDocletClassName,
+          ClassLoader docletParentClassLoader) {
         context = new Context();
         messager = new Messager(context, programName);
         this.defaultDocletClassName = defaultDocletClassName;
+        this.docletParentClassLoader = docletParentClassLoader;
+    }
+
+    Start(String programName, ClassLoader docletParentClassLoader) {
+        this(programName, standardDocletClassName, docletParentClassLoader);
     }
 
     Start(String programName) {
         this(programName, standardDocletClassName);
     }
 
+    Start(ClassLoader docletParentClassLoader) {
+        this(javadocName, docletParentClassLoader);
+    }
+
     Start() {
         this(javadocName);
     }
@@ -390,7 +412,8 @@
 
         // attempt to find doclet
         docletInvoker = new DocletInvoker(messager,
-                                          docletClassName, docletPath);
+                                          docletClassName, docletPath,
+                                          docletParentClassLoader);
     }
 
     private void setFilter(long filterBits) {