langtools/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java
author jjg
Tue, 25 Oct 2011 10:48:05 -0700
changeset 10818 e95eb04c68cc
parent 8633 41311e88ffb7
child 14258 8d2148961366
permissions -rw-r--r--
7104039: refactor/cleanup javac Paths class Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
8633
41311e88ffb7 6964914: javadoc does not output number of warnings using user written doclet
jjg
parents: 7681
diff changeset
     2
 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. 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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4937
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4937
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4937
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4937
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4937
diff changeset
    23
 * questions.
10
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
8633
41311e88ffb7 6964914: javadoc does not output number of warnings using user written doclet
jjg
parents: 7681
diff changeset
    34
import java.io.File;
10
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;
8633
41311e88ffb7 6964914: javadoc does not output number of warnings using user written doclet
jjg
parents: 7681
diff changeset
    38
import java.net.URL;
41311e88ffb7 6964914: javadoc does not output number of warnings using user written doclet
jjg
parents: 7681
diff changeset
    39
import java.net.URLClassLoader;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * Class creates, controls and invokes doclets.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * @author Neal Gafter (rewrite)
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
public class DocletInvoker {
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    48
    private final Class<?> docletClass;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private final String docletClassName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    private final ClassLoader appClassLoader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    private final Messager messager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    private static class DocletInvokeException extends Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private String appendPath(String path1, String path2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        if (path1 == null || path1.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
            return path2 == null ? "." : path2;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        } else if (path2 == null || path2.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
            return path1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
            return path1  + File.pathSeparator + path2;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    public DocletInvoker(Messager messager,
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1264
diff changeset
    71
                         String docletClassName, String docletPath,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1264
diff changeset
    72
                         ClassLoader docletParentClassLoader) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        this.messager = messager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        this.docletClassName = docletClassName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        // construct class loader
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        String cpString = null;   // make sure env.class.path defaults to dot
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        // do prepends to get correct ordering
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        cpString = appendPath(System.getProperty("env.class.path"), cpString);
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        cpString = appendPath(System.getProperty("java.class.path"), cpString);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        cpString = appendPath(docletPath, cpString);
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 8633
diff changeset
    83
        URL[] urls = com.sun.tools.javac.file.Locations.pathToURLs(cpString);
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1264
diff changeset
    84
        if (docletParentClassLoader == null)
1990
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
    85
            appClassLoader = new URLClassLoader(urls, getDelegationClassLoader(docletClassName));
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1264
diff changeset
    86
        else
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1264
diff changeset
    87
            appClassLoader = new URLClassLoader(urls, docletParentClassLoader);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        // attempt to find doclet
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1365
diff changeset
    90
        Class<?> dc = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            dc = appClassLoader.loadClass(docletClassName);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        } catch (ClassNotFoundException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            messager.error(null, "main.doclet_class_not_found", docletClassName);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            messager.exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        docletClass = dc;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
1990
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   100
    /*
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   101
     * Returns the delegation class loader to use when creating
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   102
     * appClassLoader (used to load the doclet).  The context class
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   103
     * loader is the best choice, but legacy behavior was to use the
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   104
     * default delegation class loader (aka system class loader).
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   105
     *
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   106
     * Here we favor using the context class loader.  To ensure
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   107
     * compatibility with existing apps, we revert to legacy
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   108
     * behavior if either or both of the following conditions hold:
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   109
     *
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   110
     * 1) the doclet is loadable from the system class loader but not
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   111
     *    from the context class loader,
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   112
     *
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   113
     * 2) this.getClass() is loadable from the system class loader but not
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   114
     *    from the context class loader.
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   115
     */
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   116
    private ClassLoader getDelegationClassLoader(String docletClassName) {
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   117
        ClassLoader ctxCL = Thread.currentThread().getContextClassLoader();
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   118
        ClassLoader sysCL = ClassLoader.getSystemClassLoader();
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   119
        if (sysCL == null)
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   120
            return ctxCL;
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   121
        if (ctxCL == null)
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   122
            return sysCL;
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   123
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   124
        // Condition 1.
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   125
        try {
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   126
            sysCL.loadClass(docletClassName);
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   127
            try {
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   128
                ctxCL.loadClass(docletClassName);
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   129
            } catch (ClassNotFoundException e) {
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   130
                return sysCL;
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   131
            }
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   132
        } catch (ClassNotFoundException e) {
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   133
        }
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   134
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   135
        // Condition 2.
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   136
        try {
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   137
            if (getClass() == sysCL.loadClass(getClass().getName())) {
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   138
                try {
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   139
                    if (getClass() != ctxCL.loadClass(getClass().getName()))
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   140
                        return sysCL;
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   141
                } catch (ClassNotFoundException e) {
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   142
                    return sysCL;
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   143
                }
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   144
            }
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   145
        } catch (ClassNotFoundException e) {
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   146
        }
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   147
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   148
        return ctxCL;
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   149
    }
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   150
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
     * Generate documentation here.  Return true on success.
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    public boolean start(RootDoc root) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        Object retVal;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        String methodName = "start";
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   157
        Class<?>[] paramTypes = { RootDoc.class };
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   158
        Object[] params = { root };
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            retVal = invoke(methodName, null, paramTypes, params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        } catch (DocletInvokeException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        if (retVal instanceof Boolean) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            return ((Boolean)retVal).booleanValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            messager.error(null, "main.must_return_boolean",
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                           docletClassName, methodName);
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
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
     * Check for doclet added options here. Zero return means
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     * option not known.  Positive value indicates number of
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     * arguments to option.  Negative value means error occurred.
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    public int optionLength(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        Object retVal;
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        String methodName = "optionLength";
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   181
        Class<?>[] paramTypes = { String.class };
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   182
        Object[] params = { option };
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            retVal = invoke(methodName, new Integer(0), paramTypes, params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        } catch (DocletInvokeException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        if (retVal instanceof Integer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
            return ((Integer)retVal).intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            messager.error(null, "main.must_return_int",
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                           docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     * Let doclet check that all options are OK. Returning true means
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     * options are OK.  If method does not exist, assume true.
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    public boolean validOptions(List<String[]> optlist) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        Object retVal;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        String options[][] = optlist.toArray(new String[optlist.length()][]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        String methodName = "validOptions";
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        DocErrorReporter reporter = messager;
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   206
        Class<?>[] paramTypes = { String[][].class, DocErrorReporter.class };
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   207
        Object[] params = { options, reporter };
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
            retVal = invoke(methodName, Boolean.TRUE, paramTypes, params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        } catch (DocletInvokeException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        if (retVal instanceof Boolean) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            return ((Boolean)retVal).booleanValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            messager.error(null, "main.must_return_boolean",
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                           docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
     * Return the language version supported by this doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
     * If the method does not exist in the doclet, assume version 1.1.
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    public LanguageVersion languageVersion() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            Object retVal;
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            String methodName = "languageVersion";
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1365
diff changeset
   230
            Class<?>[] paramTypes = new Class<?>[0];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            Object[] params = new Object[0];
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                retVal = invoke(methodName, JAVA_1_1, paramTypes, params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            } catch (DocletInvokeException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                return JAVA_1_1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            if (retVal instanceof LanguageVersion) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                return (LanguageVersion)retVal;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                messager.error(null, "main.must_return_languageversion",
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                               docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                return JAVA_1_1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        } catch (NoClassDefFoundError ex) { // for boostrapping, no Enum class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     * Utility method for calling doclet functionality
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
    private Object invoke(String methodName, Object returnValueIfNonExistent,
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1365
diff changeset
   253
                          Class<?>[] paramTypes, Object[] params)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        throws DocletInvokeException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            Method meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                meth = docletClass.getMethod(methodName, paramTypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            } catch (NoSuchMethodException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                if (returnValueIfNonExistent == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                    messager.error(null, "main.doclet_method_not_found",
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                                   docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                    throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                    return returnValueIfNonExistent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            } catch (SecurityException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                messager.error(null, "main.doclet_method_not_accessible",
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                               docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            if (!Modifier.isStatic(meth.getModifiers())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                messager.error(null, "main.doclet_method_must_be_static",
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                               docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            }
1990
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   276
            ClassLoader savedCCL =
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   277
                Thread.currentThread().getContextClassLoader();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                Thread.currentThread().setContextClassLoader(appClassLoader);
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                return meth.invoke(null , params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            } catch (IllegalArgumentException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                messager.error(null, "main.internal_error_exception_thrown",
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                               docletClassName, methodName, exc.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            } catch (IllegalAccessException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                messager.error(null, "main.doclet_method_not_accessible",
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                               docletClassName, methodName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            } catch (NullPointerException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                messager.error(null, "main.internal_error_exception_thrown",
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                               docletClassName, methodName, exc.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                throw new DocletInvokeException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
            } catch (InvocationTargetException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                Throwable err = exc.getTargetException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                if (err instanceof java.lang.OutOfMemoryError) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                    messager.error(null, "main.out.of.memory");
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                messager.error(null, "main.exception_thrown",
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                               docletClassName, methodName, exc.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                    exc.getTargetException().printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                throw new DocletInvokeException();
1990
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   303
            } finally {
5d90be5d60bb 6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents: 1789
diff changeset
   304
                Thread.currentThread().setContextClassLoader(savedCCL);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
}