langtools/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java
author mcimadamore
Tue, 13 Jan 2009 13:27:14 +0000
changeset 1789 7ac8c0815000
parent 1365 ced0e3c4678f
child 1990 5d90be5d60bb
permissions -rw-r--r--
6765045: Remove rawtypes warnings from langtools Summary: Removed all occurrences of rawtypes warnings from langtools Reviewed-by: jjg, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
1264
076a3cde30d5 6754988: Update copyright year
xdono
parents: 1262
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javadoc;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import static com.sun.javadoc.LanguageVersion.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.net.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.Method;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.Modifier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.InvocationTargetException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import java.util.StringTokenizer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * Class creates, controls and invokes doclets.
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * @author Neal Gafter (rewrite)
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
public class DocletInvoker {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    49
    private final Class<?> docletClass;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    private final String docletClassName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    private final ClassLoader appClassLoader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    private final Messager messager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    private static class DocletInvokeException extends Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    private String appendPath(String path1, String path2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        if (path1 == null || path1.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
            return path2 == null ? "." : path2;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        } else if (path2 == null || path2.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
            return path1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
            return path1  + File.pathSeparator + path2;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    public DocletInvoker(Messager messager,
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1264
diff changeset
    72
                         String docletClassName, String docletPath,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1264
diff changeset
    73
                         ClassLoader docletParentClassLoader) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        this.messager = messager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        this.docletClassName = docletClassName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        // construct class loader
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        String cpString = null;   // make sure env.class.path defaults to dot
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        // do prepends to get correct ordering
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        cpString = appendPath(System.getProperty("env.class.path"), cpString);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        cpString = appendPath(System.getProperty("java.class.path"), cpString);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        cpString = appendPath(docletPath, cpString);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        URL[] urls = pathToURLs(cpString);
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1264
diff changeset
    85
        if (docletParentClassLoader == null)
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1264
diff changeset
    86
            appClassLoader = new URLClassLoader(urls);
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1264
diff changeset
    87
        else
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1264
diff changeset
    88
            appClassLoader = new URLClassLoader(urls, docletParentClassLoader);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        // attempt to find doclet
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1365
diff changeset
    91
        Class<?> dc = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
            dc = appClassLoader.loadClass(docletClassName);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        } catch (ClassNotFoundException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            messager.error(null, "main.doclet_class_not_found", docletClassName);
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
            messager.exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        docletClass = dc;
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * Generate documentation here.  Return true on success.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    public boolean start(RootDoc root) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        Object retVal;
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        String methodName = "start";
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1365
diff changeset
   107
        Class<?>[] paramTypes = new Class<?>[1];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        Object[] params = new Object[1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        paramTypes[0] = RootDoc.class;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        params[0] = root;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            retVal = invoke(methodName, null, paramTypes, params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        } catch (DocletInvokeException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        if (retVal instanceof Boolean) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            return ((Boolean)retVal).booleanValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            messager.error(null, "main.must_return_boolean",
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                           docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     * Check for doclet added options here. Zero return means
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     * option not known.  Positive value indicates number of
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     * arguments to option.  Negative value means error occurred.
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    public int optionLength(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        Object retVal;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        String methodName = "optionLength";
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1365
diff changeset
   133
        Class<?>[] paramTypes = new Class<?>[1];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        Object[] params = new Object[1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        paramTypes[0] = option.getClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        params[0] = option;
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            retVal = invoke(methodName, new Integer(0), paramTypes, params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        } catch (DocletInvokeException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        if (retVal instanceof Integer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            return ((Integer)retVal).intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            messager.error(null, "main.must_return_int",
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                           docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
     * Let doclet check that all options are OK. Returning true means
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     * options are OK.  If method does not exist, assume true.
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    public boolean validOptions(List<String[]> optlist) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        Object retVal;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        String options[][] = optlist.toArray(new String[optlist.length()][]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        String methodName = "validOptions";
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        DocErrorReporter reporter = messager;
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1365
diff changeset
   160
        Class<?>[] paramTypes = new Class<?>[2];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        Object[] params = new Object[2];
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        paramTypes[0] = options.getClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        paramTypes[1] = DocErrorReporter.class;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        params[0] = options;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        params[1] = reporter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            retVal = invoke(methodName, Boolean.TRUE, paramTypes, params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        } catch (DocletInvokeException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        if (retVal instanceof Boolean) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            return ((Boolean)retVal).booleanValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            messager.error(null, "main.must_return_boolean",
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                           docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
     * Return the language version supported by this doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     * If the method does not exist in the doclet, assume version 1.1.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    public LanguageVersion languageVersion() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            Object retVal;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            String methodName = "languageVersion";
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1365
diff changeset
   188
            Class<?>[] paramTypes = new Class<?>[0];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
            Object[] params = new Object[0];
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                retVal = invoke(methodName, JAVA_1_1, paramTypes, params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            } catch (DocletInvokeException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                return JAVA_1_1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            if (retVal instanceof LanguageVersion) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                return (LanguageVersion)retVal;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                messager.error(null, "main.must_return_languageversion",
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                               docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                return JAVA_1_1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        } catch (NoClassDefFoundError ex) { // for boostrapping, no Enum class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
     * Utility method for calling doclet functionality
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    private Object invoke(String methodName, Object returnValueIfNonExistent,
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1365
diff changeset
   211
                          Class<?>[] paramTypes, Object[] params)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        throws DocletInvokeException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            Method meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                meth = docletClass.getMethod(methodName, paramTypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            } catch (NoSuchMethodException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                if (returnValueIfNonExistent == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                    messager.error(null, "main.doclet_method_not_found",
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                                   docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                    throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                    return returnValueIfNonExistent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            } catch (SecurityException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
                messager.error(null, "main.doclet_method_not_accessible",
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
                               docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            if (!Modifier.isStatic(meth.getModifiers())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                messager.error(null, "main.doclet_method_must_be_static",
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                               docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                Thread.currentThread().setContextClassLoader(appClassLoader);
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                return meth.invoke(null , params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            } catch (IllegalArgumentException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                messager.error(null, "main.internal_error_exception_thrown",
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                               docletClassName, methodName, exc.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            } catch (IllegalAccessException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                messager.error(null, "main.doclet_method_not_accessible",
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                               docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            } catch (NullPointerException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                messager.error(null, "main.internal_error_exception_thrown",
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                               docletClassName, methodName, exc.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
                throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            } catch (InvocationTargetException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                Throwable err = exc.getTargetException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                if (err instanceof java.lang.OutOfMemoryError) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                    messager.error(null, "main.out.of.memory");
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                messager.error(null, "main.exception_thrown",
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                               docletClassName, methodName, exc.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                    exc.getTargetException().printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     * Utility method for converting a search path string to an array
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     * of directory and JAR file URLs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     * @param path the search path string
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     * @return the resulting array of directory and JAR file URLs
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
    static URL[] pathToURLs(String path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        URL[] urls = new URL[st.countTokens()];
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        int count = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        while (st.hasMoreTokens()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            URL url = fileToURL(new File(st.nextToken()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            if (url != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                urls[count++] = url;
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        if (urls.length != count) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
            URL[] tmp = new URL[count];
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            System.arraycopy(urls, 0, tmp, 0, count);
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            urls = tmp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        return urls;
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
     * Returns the directory or JAR file URL corresponding to the specified
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     * local file name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
     * @param file the File object
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
     * @return the resulting directory or JAR file URL, or null if unknown
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
    static URL fileToURL(File file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        String name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            name = file.getCanonicalPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            name = file.getAbsolutePath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        name = name.replace(File.separatorChar, '/');
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        if (!name.startsWith("/")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            name = "/" + name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        // If the file does not exist, then assume that it's a directory
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        if (!file.isFile()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            name = name + "/";
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            return new URL("file", "", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        } catch (MalformedURLException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            throw new IllegalArgumentException("file");
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
}