langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/DocletInvoker.java
changeset 28707 2ec91c67b30b
parent 25874 83c19f00452c
child 36526 3b41f1c69604
equal deleted inserted replaced
28706:a724585645ce 28707:2ec91c67b30b
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    24  */
    24  */
    25 
    25 
    26 package com.sun.tools.javadoc;
    26 package com.sun.tools.javadoc;
    27 
    27 
    28 import java.io.File;
    28 import java.io.File;
       
    29 import java.io.IOException;
    29 import java.lang.reflect.InvocationTargetException;
    30 import java.lang.reflect.InvocationTargetException;
    30 import java.lang.reflect.Method;
    31 import java.lang.reflect.Method;
    31 import java.lang.reflect.Modifier;
    32 import java.lang.reflect.Modifier;
       
    33 import java.net.MalformedURLException;
    32 import java.net.URL;
    34 import java.net.URL;
    33 import java.net.URLClassLoader;
    35 import java.net.URLClassLoader;
       
    36 import java.nio.file.Path;
       
    37 import java.nio.file.Paths;
       
    38 import java.util.ArrayList;
       
    39 import java.util.regex.Pattern;
    34 
    40 
    35 import javax.tools.DocumentationTool;
    41 import javax.tools.DocumentationTool;
    36 import javax.tools.JavaFileManager;
    42 import javax.tools.JavaFileManager;
    37 
    43 
    38 import com.sun.javadoc.*;
    44 import com.sun.javadoc.*;
    39 import com.sun.tools.javac.file.Locations;
       
    40 import com.sun.tools.javac.util.ClientCodeException;
    45 import com.sun.tools.javac.util.ClientCodeException;
    41 import com.sun.tools.javac.util.List;
    46 import com.sun.tools.javac.util.List;
       
    47 
    42 import static com.sun.javadoc.LanguageVersion.*;
    48 import static com.sun.javadoc.LanguageVersion.*;
    43 
    49 
    44 
    50 
    45 /**
    51 /**
    46  * Class creates, controls and invokes doclets.
    52  * Class creates, controls and invokes doclets.
   106 
   112 
   107             // do prepends to get correct ordering
   113             // do prepends to get correct ordering
   108             cpString = appendPath(System.getProperty("env.class.path"), cpString);
   114             cpString = appendPath(System.getProperty("env.class.path"), cpString);
   109             cpString = appendPath(System.getProperty("java.class.path"), cpString);
   115             cpString = appendPath(System.getProperty("java.class.path"), cpString);
   110             cpString = appendPath(docletPath, cpString);
   116             cpString = appendPath(docletPath, cpString);
   111             URL[] urls = Locations.pathToURLs(cpString);
   117             URL[] urls = pathToURLs(cpString);
   112             if (docletParentClassLoader == null)
   118             if (docletParentClassLoader == null)
   113                 appClassLoader = new URLClassLoader(urls, getDelegationClassLoader(docletClassName));
   119                 appClassLoader = new URLClassLoader(urls, getDelegationClassLoader(docletClassName));
   114             else
   120             else
   115                 appClassLoader = new URLClassLoader(urls, docletParentClassLoader);
   121                 appClassLoader = new URLClassLoader(urls, docletParentClassLoader);
   116         }
   122         }
   189             retVal = invoke(methodName, null, paramTypes, params);
   195             retVal = invoke(methodName, null, paramTypes, params);
   190         } catch (DocletInvokeException exc) {
   196         } catch (DocletInvokeException exc) {
   191             return false;
   197             return false;
   192         }
   198         }
   193         if (retVal instanceof Boolean) {
   199         if (retVal instanceof Boolean) {
   194             return ((Boolean)retVal).booleanValue();
   200             return ((Boolean)retVal);
   195         } else {
   201         } else {
   196             messager.error(Messager.NOPOS, "main.must_return_boolean",
   202             messager.error(Messager.NOPOS, "main.must_return_boolean",
   197                            docletClassName, methodName);
   203                            docletClassName, methodName);
   198             return false;
   204             return false;
   199         }
   205         }
   213             retVal = invoke(methodName, 0, paramTypes, params);
   219             retVal = invoke(methodName, 0, paramTypes, params);
   214         } catch (DocletInvokeException exc) {
   220         } catch (DocletInvokeException exc) {
   215             return -1;
   221             return -1;
   216         }
   222         }
   217         if (retVal instanceof Integer) {
   223         if (retVal instanceof Integer) {
   218             return ((Integer)retVal).intValue();
   224             return ((Integer)retVal);
   219         } else {
   225         } else {
   220             messager.error(Messager.NOPOS, "main.must_return_int",
   226             messager.error(Messager.NOPOS, "main.must_return_int",
   221                            docletClassName, methodName);
   227                            docletClassName, methodName);
   222             return -1;
   228             return -1;
   223         }
   229         }
   238             retVal = invoke(methodName, Boolean.TRUE, paramTypes, params);
   244             retVal = invoke(methodName, Boolean.TRUE, paramTypes, params);
   239         } catch (DocletInvokeException exc) {
   245         } catch (DocletInvokeException exc) {
   240             return false;
   246             return false;
   241         }
   247         }
   242         if (retVal instanceof Boolean) {
   248         if (retVal instanceof Boolean) {
   243             return ((Boolean)retVal).booleanValue();
   249             return ((Boolean)retVal);
   244         } else {
   250         } else {
   245             messager.error(Messager.NOPOS, "main.must_return_boolean",
   251             messager.error(Messager.NOPOS, "main.must_return_boolean",
   246                            docletClassName, methodName);
   252                            docletClassName, methodName);
   247             return false;
   253             return false;
   248         }
   254         }
   324                 if (err instanceof java.lang.OutOfMemoryError) {
   330                 if (err instanceof java.lang.OutOfMemoryError) {
   325                     messager.error(Messager.NOPOS, "main.out.of.memory");
   331                     messager.error(Messager.NOPOS, "main.out.of.memory");
   326                 } else {
   332                 } else {
   327                     messager.error(Messager.NOPOS, "main.exception_thrown",
   333                     messager.error(Messager.NOPOS, "main.exception_thrown",
   328                                docletClassName, methodName, exc.toString());
   334                                docletClassName, methodName, exc.toString());
   329                     exc.getTargetException().printStackTrace();
   335                     exc.getTargetException().printStackTrace(System.err);
   330                 }
   336                 }
   331                 throw new DocletInvokeException();
   337                 throw new DocletInvokeException();
   332             } finally {
   338             } finally {
   333                 Thread.currentThread().setContextClassLoader(savedCCL);
   339                 Thread.currentThread().setContextClassLoader(savedCCL);
   334             }
   340             }
   335     }
   341     }
       
   342 
       
   343     /**
       
   344      * Utility method for converting a search path string to an array of directory and JAR file
       
   345      * URLs.
       
   346      *
       
   347      * Note that this method is called by the DocletInvoker.
       
   348      *
       
   349      * @param path the search path string
       
   350      * @return the resulting array of directory and JAR file URLs
       
   351      */
       
   352     private static URL[] pathToURLs(String path) {
       
   353         java.util.List<URL> urls = new ArrayList<>();
       
   354         for (String s: path.split(Pattern.quote(File.pathSeparator))) {
       
   355             if (!s.isEmpty()) {
       
   356                 URL url = fileToURL(Paths.get(s));
       
   357                 if (url != null) {
       
   358                     urls.add(url);
       
   359                 }
       
   360             }
       
   361         }
       
   362         return urls.toArray(new URL[urls.size()]);
       
   363     }
       
   364 
       
   365     /**
       
   366      * Returns the directory or JAR file URL corresponding to the specified local file name.
       
   367      *
       
   368      * @param file the Path object
       
   369      * @return the resulting directory or JAR file URL, or null if unknown
       
   370      */
       
   371     private static URL fileToURL(Path file) {
       
   372         Path p;
       
   373         try {
       
   374             p = file.toRealPath();
       
   375         } catch (IOException e) {
       
   376             p = file.toAbsolutePath();
       
   377         }
       
   378         try {
       
   379             return p.normalize().toUri().toURL();
       
   380         } catch (MalformedURLException e) {
       
   381             return null;
       
   382         }
       
   383     }
   336 }
   384 }