Java Code Examples for javax.tools.JavaFileManager#getClassLoader()

The following examples show how to use javax.tools.JavaFileManager#getClassLoader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavacProcessingEnvironment.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private void initProcessorClassLoader() {
    JavaFileManager fileManager = context.get(JavaFileManager.class);
    try {
        // If processorpath is not explicitly set, use the classpath.
        processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
            ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
            : fileManager.getClassLoader(CLASS_PATH);

        if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
            JavaCompiler compiler = JavaCompiler.instance(context);
            compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
        }
    } catch (SecurityException e) {
        processorClassLoaderException = e;
    }
}
 
Example 2
Source File: JavacProcessingEnvironment.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private void initProcessorClassLoader() {
    JavaFileManager fileManager = context.get(JavaFileManager.class);
    try {
        // If processorpath is not explicitly set, use the classpath.
        processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
            ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
            : fileManager.getClassLoader(CLASS_PATH);

        if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
            JavaCompiler compiler = JavaCompiler.instance(context);
            compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
        }
    } catch (SecurityException e) {
        processorClassLoaderException = e;
    }
}
 
Example 3
Source File: JavacProcessingEnvironment.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void initProcessorClassLoader() {
    JavaFileManager fileManager = context.get(JavaFileManager.class);
    try {
        // If processorpath is not explicitly set, use the classpath.
        processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
            ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
            : fileManager.getClassLoader(CLASS_PATH);

        if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
            JavaCompiler compiler = JavaCompiler.instance(context);
            compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
        }
    } catch (SecurityException e) {
        processorClassLoaderException = e;
    }
}
 
Example 4
Source File: JavacProcessingEnvironment.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private void initProcessorClassLoader() {
    JavaFileManager fileManager = context.get(JavaFileManager.class);
    try {
        // If processorpath is not explicitly set, use the classpath.
        processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
            ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
            : fileManager.getClassLoader(CLASS_PATH);

        if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
            JavaCompiler compiler = JavaCompiler.instance(context);
            compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
        }
    } catch (SecurityException e) {
        processorClassLoaderException = e;
    }
}
 
Example 5
Source File: JavacProcessingEnvironment.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void initProcessorClassLoader() {
    JavaFileManager fileManager = context.get(JavaFileManager.class);
    try {
        // If processorpath is not explicitly set, use the classpath.
        processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
            ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
            : fileManager.getClassLoader(CLASS_PATH);

        if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
            JavaCompiler compiler = JavaCompiler.instance(context);
            compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
        }
    } catch (SecurityException e) {
        processorClassLoaderException = e;
    }
}
 
Example 6
Source File: JavacProcessingEnvironment.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void initProcessorClassLoader() {
    JavaFileManager fileManager = context.get(JavaFileManager.class);
    try {
        // If processorpath is not explicitly set, use the classpath.
        processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
            ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
            : fileManager.getClassLoader(CLASS_PATH);

        if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
            JavaCompiler compiler = JavaCompiler.instance(context);
            compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
        }
    } catch (SecurityException e) {
        processorClassLoaderException = e;
    }
}
 
Example 7
Source File: JavacProcessingEnvironment.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void initProcessorClassLoader() {
    JavaFileManager fileManager = context.get(JavaFileManager.class);
    try {
        // If processorpath is not explicitly set, use the classpath.
        processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
            ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
            : fileManager.getClassLoader(CLASS_PATH);

        if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
            JavaCompiler compiler = JavaCompiler.instance(context);
            compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
        }
    } catch (SecurityException e) {
        processorClassLoaderException = e;
    }
}
 
Example 8
Source File: TagletManager.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add a new <code>Taglet</code>.  Print a message to indicate whether or not
 * the Taglet was registered properly.
 * @param classname  the name of the class representing the custom tag.
 * @param fileManager the filemanager to load classes and resources.
 * @param tagletPath  the path to the class representing the custom tag.
 */
public void addCustomTag(String classname, JavaFileManager fileManager, String tagletPath) {
    try {
        ClassLoader tagClassLoader;
        if (!fileManager.hasLocation(TAGLET_PATH)) {
            List<File> paths = new ArrayList<>();
            if (tagletPath != null) {
                for (String pathname : tagletPath.split(File.pathSeparator)) {
                    paths.add(new File(pathname));
                }
            }
            if (fileManager instanceof StandardJavaFileManager) {
                ((StandardJavaFileManager) fileManager).setLocation(TAGLET_PATH, paths);
            }
        }
        tagClassLoader = fileManager.getClassLoader(TAGLET_PATH);
        Class<? extends jdk.javadoc.doclet.Taglet> customTagClass =
                tagClassLoader.loadClass(classname).asSubclass(jdk.javadoc.doclet.Taglet.class);
        jdk.javadoc.doclet.Taglet instance = customTagClass.getConstructor().newInstance();
        instance.init(docEnv, doclet);
        Taglet newLegacy = new UserTaglet(instance);
        String tname = newLegacy.getName();
        Taglet t = customTags.get(tname);
        if (t != null) {
            customTags.remove(tname);
        }
        customTags.put(tname, newLegacy);
        messages.notice("doclet.Notice_taglet_registered", classname);
    } catch (Exception exc) {
        messages.error("doclet.Error_taglet_not_registered", exc.getClass().getName(), classname);
    }
}
 
Example 9
Source File: JavacProcessingEnvironment.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private void initProcessorIterator(Context context, Iterable<? extends Processor> processors) {
    Log   log   = Log.instance(context);
    Iterator<? extends Processor> processorIterator;

    if (options.isSet(XPRINT)) {
        try {
            Processor processor = PrintingProcessor.class.newInstance();
            processorIterator = List.of(processor).iterator();
        } catch (Throwable t) {
            AssertionError assertError =
                new AssertionError("Problem instantiating PrintingProcessor.");
            assertError.initCause(t);
            throw assertError;
        }
    } else if (processors != null) {
        processorIterator = processors.iterator();
    } else {
        String processorNames = options.get(PROCESSOR);
        JavaFileManager fileManager = context.get(JavaFileManager.class);
        try {
            // If processorpath is not explicitly set, use the classpath.
            processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
                ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
                : fileManager.getClassLoader(CLASS_PATH);

            /*
             * If the "-processor" option is used, search the appropriate
             * path for the named class.  Otherwise, use a service
             * provider mechanism to create the processor iterator.
             */
            if (processorNames != null) {
                processorIterator = new NameProcessIterator(processorNames, processorClassLoader, log);
            } else {
                processorIterator = new ServiceIterator(processorClassLoader, log);
            }
        } catch (SecurityException e) {
            /*
             * A security exception will occur if we can't create a classloader.
             * Ignore the exception if, with hindsight, we didn't need it anyway
             * (i.e. no processor was specified either explicitly, or implicitly,
             * in service configuration file.) Otherwise, we cannot continue.
             */
            processorIterator = handleServiceLoaderUnavailability("proc.cant.create.loader", e);
        }
    }
    discoveredProcs = new DiscoveredProcessors(processorIterator);
}
 
Example 10
Source File: TagletManager.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Add a new <code>Taglet</code>.  Print a message to indicate whether or not
 * the Taglet was registered properly.
 * @param classname  the name of the class representing the custom tag.
 * @param tagletPath  the path to the class representing the custom tag.
 */
public void addCustomTag(String classname, JavaFileManager fileManager, String tagletPath) {
    try {
        Class<?> customTagClass = null;
        // construct class loader
        String cpString = null;   // make sure env.class.path defaults to dot

        ClassLoader tagClassLoader;
        if (fileManager != null && fileManager.hasLocation(DocumentationTool.Location.TAGLET_PATH)) {
            tagClassLoader = fileManager.getClassLoader(DocumentationTool.Location.TAGLET_PATH);
        } else {
            // do prepends to get correct ordering
            cpString = appendPath(System.getProperty("env.class.path"), cpString);
            cpString = appendPath(System.getProperty("java.class.path"), cpString);
            cpString = appendPath(tagletPath, cpString);
            tagClassLoader = new URLClassLoader(pathToURLs(cpString));
        }

        customTagClass = tagClassLoader.loadClass(classname);
        Method meth = customTagClass.getMethod("register",
                                               new Class<?>[] {java.util.Map.class});
        Object[] list = customTags.values().toArray();
        Taglet lastTag = (list != null && list.length > 0)
            ? (Taglet) list[list.length-1] : null;
        meth.invoke(null, new Object[] {customTags});
        list = customTags.values().toArray();
        Object newLastTag = (list != null&& list.length > 0)
            ? list[list.length-1] : null;
        if (lastTag != newLastTag) {
            //New taglets must always be added to the end of the LinkedHashMap.
            //If the current and previous last taglet are not equal, that
            //means a new Taglet has been added.
            message.notice("doclet.Notice_taglet_registered", classname);
            if (newLastTag != null) {
                checkTaglet(newLastTag);
            }
        }
    } catch (Exception exc) {
        message.error("doclet.Error_taglet_not_registered", exc.getClass().getName(), classname);
    }

}
 
Example 11
Source File: TagletManager.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Add a new <code>Taglet</code>.  Print a message to indicate whether or not
 * the Taglet was registered properly.
 * @param classname  the name of the class representing the custom tag.
 * @param tagletPath  the path to the class representing the custom tag.
 */
public void addCustomTag(String classname, JavaFileManager fileManager, String tagletPath) {
    try {
        Class<?> customTagClass = null;
        // construct class loader
        String cpString = null;   // make sure env.class.path defaults to dot

        ClassLoader tagClassLoader;
        if (fileManager != null && fileManager.hasLocation(DocumentationTool.Location.TAGLET_PATH)) {
            tagClassLoader = fileManager.getClassLoader(DocumentationTool.Location.TAGLET_PATH);
        } else {
            // do prepends to get correct ordering
            cpString = appendPath(System.getProperty("env.class.path"), cpString);
            cpString = appendPath(System.getProperty("java.class.path"), cpString);
            cpString = appendPath(tagletPath, cpString);
            tagClassLoader = new URLClassLoader(pathToURLs(cpString));
        }

        customTagClass = tagClassLoader.loadClass(classname);
        Method meth = customTagClass.getMethod("register",
                                               new Class<?>[] {java.util.Map.class});
        Object[] list = customTags.values().toArray();
        Taglet lastTag = (list != null && list.length > 0)
            ? (Taglet) list[list.length-1] : null;
        meth.invoke(null, new Object[] {customTags});
        list = customTags.values().toArray();
        Object newLastTag = (list != null&& list.length > 0)
            ? list[list.length-1] : null;
        if (lastTag != newLastTag) {
            //New taglets must always be added to the end of the LinkedHashMap.
            //If the current and previous last taglet are not equal, that
            //means a new Taglet has been added.
            message.notice("doclet.Notice_taglet_registered", classname);
            if (newLastTag != null) {
                checkTaglet(newLastTag);
            }
        }
    } catch (Exception exc) {
        message.error("doclet.Error_taglet_not_registered", exc.getClass().getName(), classname);
    }

}
 
Example 12
Source File: TagletManager.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Add a new <code>Taglet</code>.  Print a message to indicate whether or not
 * the Taglet was registered properly.
 * @param classname  the name of the class representing the custom tag.
 * @param tagletPath  the path to the class representing the custom tag.
 */
public void addCustomTag(String classname, JavaFileManager fileManager, String tagletPath) {
    try {
        Class<?> customTagClass = null;
        // construct class loader
        String cpString = null;   // make sure env.class.path defaults to dot

        ClassLoader tagClassLoader;
        if (fileManager != null && fileManager.hasLocation(DocumentationTool.Location.TAGLET_PATH)) {
            tagClassLoader = fileManager.getClassLoader(DocumentationTool.Location.TAGLET_PATH);
        } else {
            // do prepends to get correct ordering
            cpString = appendPath(System.getProperty("env.class.path"), cpString);
            cpString = appendPath(System.getProperty("java.class.path"), cpString);
            cpString = appendPath(tagletPath, cpString);
            tagClassLoader = new URLClassLoader(pathToURLs(cpString));
        }

        customTagClass = tagClassLoader.loadClass(classname);
        Method meth = customTagClass.getMethod("register",
                                               new Class<?>[] {java.util.Map.class});
        Object[] list = customTags.values().toArray();
        Taglet lastTag = (list != null && list.length > 0)
            ? (Taglet) list[list.length-1] : null;
        meth.invoke(null, new Object[] {customTags});
        list = customTags.values().toArray();
        Object newLastTag = (list != null&& list.length > 0)
            ? list[list.length-1] : null;
        if (lastTag != newLastTag) {
            //New taglets must always be added to the end of the LinkedHashMap.
            //If the current and previous last taglet are not equal, that
            //means a new Taglet has been added.
            message.notice("doclet.Notice_taglet_registered", classname);
            if (newLastTag != null) {
                checkTaglet(newLastTag);
            }
        }
    } catch (Exception exc) {
        message.error("doclet.Error_taglet_not_registered", exc.getClass().getName(), classname);
    }

}
 
Example 13
Source File: TagletManager.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Add a new <code>Taglet</code>.  Print a message to indicate whether or not
 * the Taglet was registered properly.
 * @param classname  the name of the class representing the custom tag.
 * @param tagletPath  the path to the class representing the custom tag.
 */
public void addCustomTag(String classname, JavaFileManager fileManager, String tagletPath) {
    try {
        Class<?> customTagClass = null;
        // construct class loader
        String cpString = null;   // make sure env.class.path defaults to dot

        ClassLoader tagClassLoader;
        if (fileManager != null && fileManager.hasLocation(DocumentationTool.Location.TAGLET_PATH)) {
            tagClassLoader = fileManager.getClassLoader(DocumentationTool.Location.TAGLET_PATH);
        } else {
            // do prepends to get correct ordering
            cpString = appendPath(System.getProperty("env.class.path"), cpString);
            cpString = appendPath(System.getProperty("java.class.path"), cpString);
            cpString = appendPath(tagletPath, cpString);
            tagClassLoader = new URLClassLoader(pathToURLs(cpString));
        }

        if (exportInternalAPI) {
            exportInternalAPI(tagClassLoader);
        }

        customTagClass = tagClassLoader.loadClass(classname);

        Method meth = customTagClass.getMethod("register",
                                               Map.class);
        Object[] list = customTags.values().toArray();
        Taglet lastTag = (list != null && list.length > 0)
            ? (Taglet) list[list.length-1] : null;
        meth.invoke(null, customTags);
        list = customTags.values().toArray();
        Object newLastTag = (list != null&& list.length > 0)
            ? list[list.length-1] : null;
        if (lastTag != newLastTag) {
            //New taglets must always be added to the end of the LinkedHashMap.
            //If the current and previous last taglet are not equal, that
            //means a new Taglet has been added.
            message.notice("doclet.Notice_taglet_registered", classname);
            if (newLastTag != null) {
                checkTaglet(newLastTag);
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        message.error("doclet.Error_taglet_not_registered", exc.getClass().getName(), classname);
    }

}
 
Example 14
Source File: TagletManager.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Add a new <code>Taglet</code>.  Print a message to indicate whether or not
 * the Taglet was registered properly.
 * @param classname  the name of the class representing the custom tag.
 * @param tagletPath  the path to the class representing the custom tag.
 */
public void addCustomTag(String classname, JavaFileManager fileManager, String tagletPath) {
    try {
        Class<?> customTagClass = null;
        // construct class loader
        String cpString = null;   // make sure env.class.path defaults to dot

        ClassLoader tagClassLoader;
        if (fileManager != null && fileManager.hasLocation(DocumentationTool.Location.TAGLET_PATH)) {
            tagClassLoader = fileManager.getClassLoader(DocumentationTool.Location.TAGLET_PATH);
        } else {
            // do prepends to get correct ordering
            cpString = appendPath(System.getProperty("env.class.path"), cpString);
            cpString = appendPath(System.getProperty("java.class.path"), cpString);
            cpString = appendPath(tagletPath, cpString);
            tagClassLoader = new URLClassLoader(pathToURLs(cpString));
        }

        customTagClass = tagClassLoader.loadClass(classname);
        Method meth = customTagClass.getMethod("register",
                                               new Class<?>[] {java.util.Map.class});
        Object[] list = customTags.values().toArray();
        Taglet lastTag = (list != null && list.length > 0)
            ? (Taglet) list[list.length-1] : null;
        meth.invoke(null, new Object[] {customTags});
        list = customTags.values().toArray();
        Object newLastTag = (list != null&& list.length > 0)
            ? list[list.length-1] : null;
        if (lastTag != newLastTag) {
            //New taglets must always be added to the end of the LinkedHashMap.
            //If the current and previous last taglet are not equal, that
            //means a new Taglet has been added.
            message.notice("doclet.Notice_taglet_registered", classname);
            if (newLastTag != null) {
                checkTaglet(newLastTag);
            }
        }
    } catch (Exception exc) {
        message.error("doclet.Error_taglet_not_registered", exc.getClass().getName(), classname);
    }

}
 
Example 15
Source File: TagletManager.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Add a new <code>Taglet</code>.  Print a message to indicate whether or not
 * the Taglet was registered properly.
 * @param classname  the name of the class representing the custom tag.
 * @param tagletPath  the path to the class representing the custom tag.
 */
public void addCustomTag(String classname, JavaFileManager fileManager, String tagletPath) {
    try {
        Class<?> customTagClass = null;
        // construct class loader
        String cpString = null;   // make sure env.class.path defaults to dot

        ClassLoader tagClassLoader;
        if (fileManager != null && fileManager.hasLocation(DocumentationTool.Location.TAGLET_PATH)) {
            tagClassLoader = fileManager.getClassLoader(DocumentationTool.Location.TAGLET_PATH);
        } else {
            // do prepends to get correct ordering
            cpString = appendPath(System.getProperty("env.class.path"), cpString);
            cpString = appendPath(System.getProperty("java.class.path"), cpString);
            cpString = appendPath(tagletPath, cpString);
            tagClassLoader = new URLClassLoader(pathToURLs(cpString));
        }

        customTagClass = tagClassLoader.loadClass(classname);
        Method meth = customTagClass.getMethod("register",
                                               new Class<?>[] {java.util.Map.class});
        Object[] list = customTags.values().toArray();
        Taglet lastTag = (list != null && list.length > 0)
            ? (Taglet) list[list.length-1] : null;
        meth.invoke(null, new Object[] {customTags});
        list = customTags.values().toArray();
        Object newLastTag = (list != null&& list.length > 0)
            ? list[list.length-1] : null;
        if (lastTag != newLastTag) {
            //New taglets must always be added to the end of the LinkedHashMap.
            //If the current and previous last taglet are not equal, that
            //means a new Taglet has been added.
            message.notice("doclet.Notice_taglet_registered", classname);
            if (newLastTag != null) {
                checkTaglet(newLastTag);
            }
        }
    } catch (Exception exc) {
        message.error("doclet.Error_taglet_not_registered", exc.getClass().getName(), classname);
    }

}
 
Example 16
Source File: JavacProcessingEnvironment.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
private void initProcessorIterator(Context context, Iterable<? extends Processor> processors) {
    Log   log   = Log.instance(context);
    Iterator<? extends Processor> processorIterator;

    if (options.isSet(XPRINT)) {
        try {
            Processor processor = PrintingProcessor.class.newInstance();
            processorIterator = List.of(processor).iterator();
        } catch (Throwable t) {
            AssertionError assertError =
                new AssertionError("Problem instantiating PrintingProcessor.");
            assertError.initCause(t);
            throw assertError;
        }
    } else if (processors != null) {
        processorIterator = processors.iterator();
    } else {
        String processorNames = options.get(PROCESSOR);
        JavaFileManager fileManager = context.get(JavaFileManager.class);
        try {
            // If processorpath is not explicitly set, use the classpath.
            processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
                ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
                : fileManager.getClassLoader(CLASS_PATH);

            /*
             * If the "-processor" option is used, search the appropriate
             * path for the named class.  Otherwise, use a service
             * provider mechanism to create the processor iterator.
             */
            if (processorNames != null) {
                processorIterator = new NameProcessIterator(processorNames, processorClassLoader, log);
            } else {
                processorIterator = new ServiceIterator(processorClassLoader, log);
            }
        } catch (SecurityException e) {
            /*
             * A security exception will occur if we can't create a classloader.
             * Ignore the exception if, with hindsight, we didn't need it anyway
             * (i.e. no processor was specified either explicitly, or implicitly,
             * in service configuration file.) Otherwise, we cannot continue.
             */
            processorIterator = handleServiceLoaderUnavailability("proc.cant.create.loader", e);
        }
    }
    discoveredProcs = new DiscoveredProcessors(processorIterator);
}
 
Example 17
Source File: TagletManager.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Add a new <code>Taglet</code>.  Print a message to indicate whether or not
 * the Taglet was registered properly.
 * @param classname  the name of the class representing the custom tag.
 * @param tagletPath  the path to the class representing the custom tag.
 */
public void addCustomTag(String classname, JavaFileManager fileManager, String tagletPath) {
    try {
        Class<?> customTagClass = null;
        // construct class loader
        String cpString = null;   // make sure env.class.path defaults to dot

        ClassLoader tagClassLoader;
        if (fileManager != null && fileManager.hasLocation(DocumentationTool.Location.TAGLET_PATH)) {
            tagClassLoader = fileManager.getClassLoader(DocumentationTool.Location.TAGLET_PATH);
        } else {
            // do prepends to get correct ordering
            cpString = appendPath(System.getProperty("env.class.path"), cpString);
            cpString = appendPath(System.getProperty("java.class.path"), cpString);
            cpString = appendPath(tagletPath, cpString);
            tagClassLoader = new URLClassLoader(pathToURLs(cpString));
        }

        customTagClass = tagClassLoader.loadClass(classname);
        Method meth = customTagClass.getMethod("register",
                                               new Class<?>[] {java.util.Map.class});
        Object[] list = customTags.values().toArray();
        Taglet lastTag = (list != null && list.length > 0)
            ? (Taglet) list[list.length-1] : null;
        meth.invoke(null, new Object[] {customTags});
        list = customTags.values().toArray();
        Object newLastTag = (list != null&& list.length > 0)
            ? list[list.length-1] : null;
        if (lastTag != newLastTag) {
            //New taglets must always be added to the end of the LinkedHashMap.
            //If the current and previous last taglet are not equal, that
            //means a new Taglet has been added.
            message.notice("doclet.Notice_taglet_registered", classname);
            if (newLastTag != null) {
                checkTaglet(newLastTag);
            }
        }
    } catch (Exception exc) {
        message.error("doclet.Error_taglet_not_registered", exc.getClass().getName(), classname);
    }

}
 
Example 18
Source File: TagletManager.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Add a new <code>Taglet</code>.  Print a message to indicate whether or not
 * the Taglet was registered properly.
 * @param classname  the name of the class representing the custom tag.
 * @param tagletPath  the path to the class representing the custom tag.
 */
public void addCustomTag(String classname, JavaFileManager fileManager, String tagletPath) {
    try {
        Class<?> customTagClass = null;
        // construct class loader
        String cpString = null;   // make sure env.class.path defaults to dot

        ClassLoader tagClassLoader;
        if (fileManager != null && fileManager.hasLocation(DocumentationTool.Location.TAGLET_PATH)) {
            tagClassLoader = fileManager.getClassLoader(DocumentationTool.Location.TAGLET_PATH);
        } else {
            // do prepends to get correct ordering
            cpString = appendPath(System.getProperty("env.class.path"), cpString);
            cpString = appendPath(System.getProperty("java.class.path"), cpString);
            cpString = appendPath(tagletPath, cpString);
            tagClassLoader = new URLClassLoader(pathToURLs(cpString));
        }

        customTagClass = tagClassLoader.loadClass(classname);
        Method meth = customTagClass.getMethod("register",
                                               new Class<?>[] {java.util.Map.class});
        Object[] list = customTags.values().toArray();
        Taglet lastTag = (list != null && list.length > 0)
            ? (Taglet) list[list.length-1] : null;
        meth.invoke(null, new Object[] {customTags});
        list = customTags.values().toArray();
        Object newLastTag = (list != null&& list.length > 0)
            ? list[list.length-1] : null;
        if (lastTag != newLastTag) {
            //New taglets must always be added to the end of the LinkedHashMap.
            //If the current and previous last taglet are not equal, that
            //means a new Taglet has been added.
            message.notice("doclet.Notice_taglet_registered", classname);
            if (newLastTag != null) {
                checkTaglet(newLastTag);
            }
        }
    } catch (Exception exc) {
        message.error("doclet.Error_taglet_not_registered", exc.getClass().getName(), classname);
    }

}