Package javax.tools

Examples of javax.tools.StandardJavaFileManager

Whereas these are not (reason in parentheses): @author Peter von der Ahé @since 1.6

      JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
      if (compiler == null) {
        throw new STJSRuntimeException("A Java compiler is not available for this project. "
            + "You may have configured your environment to run with a JRE instead of a JDK");
      }
      StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
      fileManager.setLocation(StandardLocation.CLASS_PATH, dependencies);

      Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(sourceFiles);
      compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();

      fileManager.close();
    }
    catch (Exception e) {
      throw Throwables.propagate(e);
    }
View Full Code Here


      ArrayList endorsedDirClasspaths,
      String customEncoding) {

    ArrayList<FileSystem.Classpath> fileSystemClasspaths = new ArrayList<FileSystem.Classpath>();
    EclipseFileManager javaFileManager = null;
    StandardJavaFileManager standardJavaFileManager = null;
    if (this.fileManager instanceof EclipseFileManager) {
      javaFileManager = (EclipseFileManager) this.fileManager;
    }
    if (this.fileManager instanceof StandardJavaFileManager) {
      standardJavaFileManager = (StandardJavaFileManager) this.fileManager;
    }

    if (javaFileManager != null) {
      if ((javaFileManager.flags & EclipseFileManager.HAS_ENDORSED_DIRS) == 0
          && (javaFileManager.flags & EclipseFileManager.HAS_BOOTCLASSPATH) != 0) {
        fileSystemClasspaths.addAll(this.handleEndorseddirs(null));
      }
    }
    Iterable<? extends File> location = null;
    if (standardJavaFileManager != null) {
      location = standardJavaFileManager.getLocation(StandardLocation.PLATFORM_CLASS_PATH);
    }
    if (location != null) {
      for (File file : location) {
        Classpath classpath = FileSystem.getClasspath(
          file.getAbsolutePath(),
          null,
          null);
        if (classpath != null) {
          fileSystemClasspaths.add(classpath);
        }
      }
    }
    if (javaFileManager != null) {
      if ((javaFileManager.flags & EclipseFileManager.HAS_EXT_DIRS) == 0
          && (javaFileManager.flags & EclipseFileManager.HAS_BOOTCLASSPATH) != 0) {
        fileSystemClasspaths.addAll(this.handleExtdirs(null));
      }
    }
    if (standardJavaFileManager != null) {
      location = standardJavaFileManager.getLocation(StandardLocation.SOURCE_PATH);
    } else {
      location = null;
    }
    if (location != null) {
      for (File file : location) {
        Classpath classpath = FileSystem.getClasspath(
            file.getAbsolutePath(),
            null,
            null);
        if (classpath != null) {
          fileSystemClasspaths.add(classpath);
        }
      }
    }
    if (standardJavaFileManager != null) {
      location = standardJavaFileManager.getLocation(StandardLocation.CLASS_PATH);
    } else {
      location = null;
    }
    if (location != null) {
      for (File file : location) {
View Full Code Here

            errDispatcher.jspError("java.err.nojdk");
        }

        DiagnosticCollector<JavaFileObject> diagnostics =
            new DiagnosticCollector<JavaFileObject>();
        StandardJavaFileManager stdFileManager =
                    javac.getStandardFileManager(diagnostics, null, null);

        String name = className.substring(className.lastIndexOf('.')+1);

        JavaFileObject[] sourceFiles = {
            new SimpleJavaFileObject(
                    URI.create("string:///" + name.replace('.','/') +
                               Kind.SOURCE.extension),
                    Kind.SOURCE) {
                public CharSequence getCharContent(boolean ignore) {
                    return source;
                }
            }
        };

        try {
            stdFileManager.setLocation(StandardLocation.CLASS_PATH, this.cpath);
        } catch (IOException e) {
        }

        JavaFileManager javaFileManager = getJavaFileManager(stdFileManager);
        javax.tools.JavaCompiler.CompilationTask ct =
View Full Code Here

            // Using Java 6 compiler API to compile the generated .java files
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            DiagnosticCollector<JavaFileObject> diagnostics =
                   new DiagnosticCollector<JavaFileObject>();
            StandardJavaFileManager manager =
                    compiler.getStandardFileManager(diagnostics, null, null);
            Iterable compilationUnits = manager.getJavaFileObjectsFromFiles(files);

            long start = System.currentTimeMillis();
            long end = start;

            compilationResult = compiler.getTask(
                    null, manager, diagnostics, options, null, compilationUnits).call();

            end = System.currentTimeMillis();
            _logger.fine("JAVA compile time (" + files.size()
                    + " files) = " + (end - start));

            // Save compilation erros in msgBuffer to be used in case of failure
            for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
                //Ignore NOTE about generated non safe code
                if (diagnostic.getKind().equals(Diagnostic.Kind.NOTE)) {
                    if (_logger.isLoggable(Logger.FINE)) {
                        msgBuffer.append("\n").append(diagnostic.getMessage(null));
                    }
                    continue;
                }
                msgBuffer.append("\n").append(diagnostic.getMessage(null));
            }

            manager.close();

        } catch(Exception jce) {
            _logger.fine("cmpc.cmp_complilation_exception", jce);
            String msg = I18NHelper.getMessage(messages,
                    "cmpc.cmp_complilation_exception",
View Full Code Here

public class Compiler {

    public void compile(File directory, String classpath) {

        JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, null, null);

        Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(findAllSourceFiles(directory));

        ArrayList<String> options = new ArrayList<String>();
        options.add("-classpath");
        options.add(classpath);
        options.add("-encoding");
View Full Code Here

        try {
            List<File> files = new ArrayList<File>();
            gatherFiles(baseDirectory, files);

            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

            List<String> options = new ArrayList<String>();
            options.add("-cp");
            StringBuilder sb = new StringBuilder();
            sb.append(ASClassLoaderUtil.getModuleClassPath(habitat, "", null));
            options.add(sb.toString());
           
            Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);
            if (!compiler.getTask(null, fileManager, null, options, null, compilationUnits).call()) {
                RestLogging.restLogger.log(Level.INFO, RestLogging.COMPILATION_FAILED);
            }
           
            fileManager.close();
        } catch (IOException ex) {
            RestLogging.restLogger.log(Level.SEVERE, null, ex);
        }
    }
View Full Code Here

            legacyProprietary.remove(name);
            documented.add(name);
        }

        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
        Location jarLocation = StandardLocation.locationFor(jarName);
        File jarFile = new File(jarName);
        fm.setLocation(jarLocation, List.of(jarFile));
        fm.setLocation(StandardLocation.CLASS_PATH, List.<File>nil());
        fm.setLocation(StandardLocation.SOURCE_PATH, List.<File>nil());
        {
            ArrayList<File> bootClassPath = new ArrayList<File>();
            bootClassPath.add(jarFile);
            for (File path : fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH)) {
                if (!new File(path.getName()).equals(new File("rt.jar")))
                    bootClassPath.add(path);
            }
            System.err.println("Using boot class path = " + bootClassPath);
            fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
        }
        // System.out.println(fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH));
        File destDir = new File(destName);
        if (!destDir.exists())
            if (!destDir.mkdirs())
                throw new RuntimeException("Could not create " + destDir);
        fm.setLocation(StandardLocation.CLASS_OUTPUT, List.of(destDir));
        Set<String> hiddenPackages = new HashSet<String>();
        Set<String> crisp = new HashSet<String>();
        List<String> options = List.of("-XDdev");
        // options = options.prepend("-doe");
        // options = options.prepend("-verbose");
        JavacTaskImpl task = (JavacTaskImpl)
            tool.getTask(null, fm, null, options, null, null);
        com.sun.tools.javac.main.JavaCompiler compiler =
            com.sun.tools.javac.main.JavaCompiler.instance(task.getContext());
        ClassReader reader = ClassReader.instance(task.getContext());
        ClassWriter writer = ClassWriter.instance(task.getContext());
        Symtab syms = Symtab.instance(task.getContext());
        Attribute.Compound proprietary =
            new Attribute.Compound(syms.proprietaryType,
                                   List.<Pair<Symbol.MethodSymbol,Attribute>>nil());

        Type.moreInfo = true;
        Pool pool = new Pool();
        for (JavaFileObject file : fm.list(jarLocation, "", EnumSet.of(CLASS), true)) {
            String className = fm.inferBinaryName(jarLocation, file);
            int index = className.lastIndexOf('.');
            String pckName = index == -1 ? "" : className.substring(0, index);
            boolean addLegacyAnnotation = false;
            if (documented.contains(pckName)) {
                if (!legacy.contains(pckName))
View Full Code Here

      DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
      JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
      if (javaCompiler == null) {
        throw new RuntimeException("Unable to detect java compiler, make sure you're using a JDK not a JRE!");
      }
      StandardJavaFileManager standardFileManager = javaCompiler.getStandardFileManager(null, null, null);

      standardFileManager.setLocation(StandardLocation.SOURCE_PATH, Collections.singleton(javaSourceContext.getSourceRoot()));
      fileManager = new MemoryFileManager(loader, standardFileManager);

      // TODO - this needs to be fixed so it can compile classes from the classpath otherwise can't include
      // other .java resources from other modules

      JavaFileObject javaFile = standardFileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, resolveMainClassName(), Kind.SOURCE);
      JavaCompiler.CompilationTask task = javaCompiler.getTask(null, fileManager, diagnostics, COMPILER_OPTIONS, null, Collections.singleton(javaFile));
      boolean valid = task.call();
      if (valid) {
        for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
          log.info(d);
View Full Code Here

        DEBUG.P("documented="+documented);
        DEBUG.P("legacyProprietary.size()="+legacyProprietary.size());

        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        //DEBUG.P("tool="+tool);
        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
        Location jarLocation = StandardLocation.locationFor(jarName);
        File jarFile = new File(jarName);
        fm.setLocation(jarLocation, List.of(jarFile));
        fm.setLocation(StandardLocation.CLASS_PATH, List.<File>nil());
        fm.setLocation(StandardLocation.SOURCE_PATH, List.<File>nil());
        {
            ArrayList<File> bootClassPath = new ArrayList<File>();
            bootClassPath.add(jarFile);
            for (File path : fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH)) {
                if (!new File(path.getName()).equals(new File("rt.jar")))
                    bootClassPath.add(path);
            }
            System.err.println("Using boot class path = " + bootClassPath);
            fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
        }
        // System.out.println(fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH));
        File destDir = new File(destName);
        if (!destDir.exists())
            if (!destDir.mkdirs())
                throw new RuntimeException("Could not create " + destDir);
        fm.setLocation(StandardLocation.CLASS_OUTPUT, List.of(destDir));
        Set<String> hiddenPackages = new HashSet<String>();
        Set<String> crisp = new HashSet<String>();
        List<String> options = List.of("-XDdev");
        // options = options.prepend("-doe");
        // options = options.prepend("-verbose");
        JavacTaskImpl task = (JavacTaskImpl)
            tool.getTask(null, fm, null, options, null, null);
        com.sun.tools.javac.main.JavaCompiler compiler =
            com.sun.tools.javac.main.JavaCompiler.instance(task.getContext());
        ClassReader reader = ClassReader.instance(task.getContext());
        ClassWriter writer = ClassWriter.instance(task.getContext());
        Symtab syms = Symtab.instance(task.getContext());
        Attribute.Compound proprietary =
            new Attribute.Compound(syms.proprietaryType,
                                   List.<Pair<Symbol.MethodSymbol,Attribute>>nil());

        Type.moreInfo = true;
        Pool pool = new Pool();
       
        DEBUG.P("jarLocation="+jarLocation.getName());
       
        for (JavaFileObject file : fm.list(jarLocation, "", EnumSet.of(CLASS), true)) {
            String className = fm.inferBinaryName(jarLocation, file);
           
            DEBUG.P("className="+className);
           
            int index = className.lastIndexOf('.');
            String pckName = index == -1 ? "" : className.substring(0, index);
View Full Code Here

        if (verbose && verbosePath) {
          //javac加-verbose时输出[search path for source files:.....]
          //[search path for class files:...........................]
            if (fileManager instanceof StandardJavaFileManager) {
                StandardJavaFileManager fm = (StandardJavaFileManager)fileManager;
                //加了-sourcepath选项时,打印-sourcepath所指示的路径
                //路径由com.sun.tools.javac.util.Paths.computeSourcePath()求出
                if (haveSourcePath && wantSourceFiles) {
                    List<File> path = List.nil();
                    for (File file : fm.getLocation(SOURCE_PATH)) {
                      DEBUG.P("file="+file);
                        path = path.prepend(file);
                    }
                    printVerbose("sourcepath", path.reverse().toString());
                //没加-sourcepath选项时,默认打印类路径上的信息
                //路径由com.sun.tools.javac.util.Paths.computeUserClassPath()求出
                } else if (wantSourceFiles) {
                    List<File> path = List.nil();
                    for (File file : fm.getLocation(CLASS_PATH)) {
                        path = path.prepend(file);
                    }
                    printVerbose("sourcepath", path.reverse().toString());
                }
                if (wantClassFiles) {
                    List<File> path = List.nil();
                    //一般是jre\lib和jre\lib\ext目录下的.jar文件
                    //路径由com.sun.tools.javac.util.Paths.computeBootClassPath()求出
                    for (File file : fm.getLocation(PLATFORM_CLASS_PATH)) {
                        path = path.prepend(file);
                    }
                   
                    //路径由com.sun.tools.javac.util.Paths.computeUserClassPath()求出
                    for (File file : fm.getLocation(CLASS_PATH)) {
                        path = path.prepend(file);
                    }
                    //将上面两种类路径连在一起输出
                    printVerbose("classpath",  path.reverse().toString());
                }
View Full Code Here

TOP

Related Classes of javax.tools.StandardJavaFileManager

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.