Package javax.tools

Examples of javax.tools.JavaFileManager


     *
     * @param key The resource key to use to log an error message
     * @param e   If non-null, pass this exception to Abort
     */
    private Iterator<Processor> handleServiceLoaderUnavailability(String key, Exception e) {
        JavaFileManager fileManager = context.get(JavaFileManager.class);

        if (fileManager instanceof JavacFileManager) {
            StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager;
            Iterable<? extends File> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
                ? standardFileManager.getLocation(ANNOTATION_PROCESSOR_PATH)
                : standardFileManager.getLocation(CLASS_PATH);

            if (needClassLoader(options.get(PROCESSOR), workingPath) )
                handleException(key, e);
View Full Code Here


        return new ClassLoaderImpl(loader);
      }
    });

    this.diagnostics = new DiagnosticCollector<JavaFileObject>();
    final JavaFileManager fileManager = this.compiler.getStandardFileManager(this.diagnostics, null, null);
    // create our FileManager which chains to the default file manager
    // and our ClassLoader
    this.javaFileManager = new FileManagerImpl(fileManager, this.classLoader);
    this.options = new ArrayList<String>();
    if (options != null) { // make a save copy of input options
View Full Code Here

            DEBUG.P("processorIterator="+processorIterator);
        } 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.
                //在javax.tools.StandardLocation定义了ANNOTATION_PROCESSOR_PATH
                //没有指定“-processorpath <路径>”选项时,从
                ClassLoader processorCL = 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.
                 */
                DEBUG.P("processorNames="+processorNames);
                if (processorNames != null) {
                    processorIterator = new NameProcessIterator(processorNames, processorCL, log);
                } else {
                    //为什么要@SuppressWarnings("unchecked")呢?
                    //因为Service.providers返回的是一个没有范型化的LazyIterator类实例
                    //而it是一个范型化的Iterator<Processor>
                    @SuppressWarnings("unchecked")
                        Iterator<Processor> it =
                            Service.providers(Processor.class, processorCL);
              //Service不包含在javac的源码中,在rt.jar文件中
                    processorIterator = it;
                }
            } 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.
                 */

        if (fileManager instanceof JavacFileManager) {
            StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager;
            Iterable<? extends File> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
          ? standardFileManager.getLocation(ANNOTATION_PROCESSOR_PATH)
          : standardFileManager.getLocation(CLASS_PATH);
   
            if (needClassLoader(processorNames, workingPath) ) {
          log.error("proc.cant.create.loader", e.getLocalizedMessage());
View Full Code Here

        packageInfoFiles = List.nil();

                    compiler.close();
                    currentContext = contextForNextRound(currentContext, true);

                    JavaFileManager fileManager = currentContext.get(JavaFileManager.class);

                    List<JavaFileObject> fileObjects = List.nil();
                    for (JavaFileObject jfo : filer.getGeneratedSourceFileObjects() ) {
                        fileObjects = fileObjects.prepend(jfo);
                    }
View Full Code Here

        TaskListener tl = context.get(TaskListener.class);
        if (tl != null)
            next.put(TaskListener.class, tl);

        JavaFileManager jfm = context.get(JavaFileManager.class);
        assert jfm != null;
        next.put(JavaFileManager.class, jfm);
        if (jfm instanceof JavacFileManager) {
            ((JavacFileManager)jfm).setContext(next);
        }
View Full Code Here

        out.append("      .getDeclaredMethod(\"call\", String.class, Object[].class).invoke(null, fn, args);");
        out.append("  }");
        out.append("}");
       
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        JavaFileManager jfm = new MemoryFileManager(compiler);
        JavaFileObject file = new JavaObjectFromString(className, out.toString());
        compiler.getTask(null, jfm, null, null, null, Arrays.asList(file)).call();       
      }
     
        return Class.forName(className, true, classLoader).getDeclaredMethod("call", Object[].class);
View Full Code Here

            }
        } 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.
    ClassLoader processorCL = 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, processorCL, log);
                } else {
                    @SuppressWarnings("unchecked")
                        Iterator<Processor> it =
                            Service.providers(Processor.class, processorCL);
                    processorIterator = it;
                }
            } 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.
                 */

    if (fileManager instanceof JavacFileManager) {
        StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager;
        Iterable<? extends File> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
      ? standardFileManager.getLocation(ANNOTATION_PROCESSOR_PATH)
      : standardFileManager.getLocation(CLASS_PATH);

        if (needClassLoader(processorNames, workingPath) ) {
      log.error("proc.cant.create.loader", e.getLocalizedMessage());
View Full Code Here

        packageInfoFiles = List.nil();

                    compiler.close();
                    currentContext = contextForNextRound(currentContext, true);

                    JavaFileManager fileManager = currentContext.get(JavaFileManager.class);

                    List<JavaFileObject> fileObjects = List.nil();
                    for (JavaFileObject jfo : filer.getGeneratedSourceFileObjects() ) {
                        fileObjects = fileObjects.prepend(jfo);
                    }
View Full Code Here

        TaskListener tl = context.get(TaskListener.class);
        if (tl != null)
            next.put(TaskListener.class, tl);

        JavaFileManager jfm = context.get(JavaFileManager.class);
        assert jfm != null;
        next.put(JavaFileManager.class, jfm);
        if (jfm instanceof JavacFileManager) {
            ((JavacFileManager)jfm).setContext(next);
        }
View Full Code Here

      clientOnly = true;
      argList.remove(0);
    }

    // Control how the compile process writes data to disk
    JavaFileManager fileManager =
        new JarOrDirectoryOutputFileManager(new File(argList.remove(0)), compiler
            .getStandardFileManager(null, null, null));

    // Create a validator and require it to process the specified types
    RfValidator processor = new RfValidator();
    if (clientOnly) {
      processor.setClientOnly(true);
    } else {
      processor.setMustResolveAllMappings(true);
    }
    processor.setRootOverride(argList);

    // Create the compilation task
    CompilationTask task =
        compiler.getTask(null, fileManager, null, null, null, Arrays
            .asList(new FakeJavaFileObject()));
    task.setProcessors(Arrays.asList(processor));
    if (!task.call()) {
      return false;
    }
    // Save data only on successful compilation
    fileManager.close();
    return true;
  }
View Full Code Here

TOP

Related Classes of javax.tools.JavaFileManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.