Package javax.tools

Examples of javax.tools.JavaCompiler


    List<File> javaFiles = new ArrayList<File>();
    for (OutputFile o : outputs) {
      javaFiles.add(o.writeToDestination(null, dstDir));
    }

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager =
      compiler.getStandardFileManager(null, null, null);
   
    CompilationTask cTask = compiler.getTask(null, fileManager, null, null,
        null,
        fileManager.getJavaFileObjects(
            javaFiles.toArray(new File[javaFiles.size()])));
    assertTrue(cTask.call());
  }
View Full Code Here


  private static final List<String> COMMON_ARGS = Arrays.asList("-source 1.6 -target 1.6".split(" "));

  public static List<File> compile(List<File> files, File outDir, boolean includeDebugInfo) throws IOException {

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);

    StaticFileManager staticFileManager = new StaticFileManager(fileManager, outDir);

    List<String> options = new ArrayList<String>();
    options.add(includeDebugInfo ? "-g" : "-g:none");
    options.addAll(COMMON_ARGS);
    CompilationTask task = compiler.getTask(null, staticFileManager, null, options, null, compilationUnits);
    Boolean result = task.call();
    fileManager.close();
    if (Boolean.TRUE.equals(result)) {
      return staticFileManager.outputFiles();
    }
View Full Code Here

  public boolean compile() throws Exception {
    String fullName = clsNode.getFullName();
    String code = clsNode.getCode().toString();

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

    List<JavaFileObject> jFiles = new ArrayList<JavaFileObject>(1);
    jFiles.add(new CharSequenceJavaFileObject(fullName, code));

    CompilationTask compilerTask = compiler.getTask(null, fileManager, null, null, null, jFiles);
    return Boolean.TRUE.equals(compilerTask.call());
  }
View Full Code Here

        switch ( compilerConfiguration.getCompilerReuseStrategy() )
        {
            case AlwaysNew:
                return ToolProvider.getSystemJavaCompiler();
            case ReuseCreated:
                JavaCompiler javaCompiler;
                synchronized ( JAVA_COMPILERS )
                {
                    if ( JAVA_COMPILERS.size() > 0 )
                    {
                        javaCompiler = JAVA_COMPILERS.get( 0 );
View Full Code Here

    static CompilerResult compileInProcess( String[] args, final CompilerConfiguration config,
                                                 String[] sourceFiles )
        throws CompilerException
    {
        JavaCompiler compiler = getJavaCompiler( config );
        try
        {
            if ( compiler == null )
            {
                CompilerMessage message =
                                new CompilerMessage( "No compiler is provided in this environment. "
                                                     + "Perhaps you are running on a JRE rather than a JDK?",
                                                     CompilerMessage.Kind.ERROR );
                return new CompilerResult( false, Collections.singletonList( message ) );
            }
            final String sourceEncoding = config.getSourceEncoding();
            final Charset sourceCharset = sourceEncoding == null ? null : Charset.forName( sourceEncoding );
            final DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<JavaFileObject>();
            final StandardJavaFileManager standardFileManager =
                compiler.getStandardFileManager( collector, null, sourceCharset );

            final Iterable<? extends JavaFileObject> fileObjects =
                standardFileManager.getJavaFileObjectsFromStrings( Arrays.asList( sourceFiles ) );
            final JavaCompiler.CompilationTask task =

                                         /*(Writer out,
                                         JavaFileManager fileManager,
                                         DiagnosticListener<? super JavaFileObject> diagnosticListener,
                                         Iterable<String> options,
                                         Iterable<String> classes,
                                         Iterable<? extends JavaFileObject> compilationUnits)*/


                compiler.getTask( null, standardFileManager, collector, Arrays.asList( args ), null, fileObjects );
            final Boolean result = task.call();
            final ArrayList<CompilerMessage> compilerMsgs = new ArrayList<CompilerMessage>();
            for ( Diagnostic<? extends JavaFileObject> diagnostic : collector.getDiagnostics() )
            {
                CompilerMessage.Kind kind;
View Full Code Here

      System.out.println("wrote file: " + sourceFile.getAbsolutePath());

      ByteArrayOutputStream errorOutputStream = new ByteArrayOutputStream();

      JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

      compiler.run(null, null, errorOutputStream, sourceFile.getAbsolutePath());

      for (byte b : errorOutputStream.toByteArray()) {
        System.out.print((char) b);
      }
View Full Code Here

        return files;
    }

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager sjfm = jc.getStandardFileManager(null, null, null);
        try {
            Set<File> generatedFiles = getJavaFiles(sourceFolder);
            Iterable<? extends JavaFileObject> fileObjects = sjfm.getJavaFileObjectsFromFiles(generatedFiles);
            List<String> opts = getCompilerOptions();
            jc.getTask(null, null, null, opts, null, fileObjects).call();
        } finally {
            try {
                sjfm.close();
            } catch (IOException e) {
                throw new MojoFailureException(e.getMessage(), e);
View Full Code Here

        compile(exporter);
    }

    private void compile(MetaDataExporter exporter) {
        JavaCompiler compiler = new SimpleCompiler();
        Set<String> classes = exporter.getClasses();
        int compilationResult = compiler.run(null, null, null, classes.toArray(new String[classes.size()]));
        if(compilationResult == 0) {
            System.out.println("Compilation is successful");
        } else {
            Assert.fail("Compilation Failed");
        }
View Full Code Here

        if (!out.mkdirs()) {
            Assert.fail("Creation of " + out.getPath() + " failed");
        }

        String classPath = SimpleCompiler.getClassPath((URLClassLoader) getClass().getClassLoader());
        JavaCompiler compiler = new EclipseCompiler();
        List<String> options = new ArrayList<String>();
        options.add("-s");
        options.add("target/out-eclipse");
        options.add("-proc:only");
        options.add("-processor");
        options.add(QuerydslAnnotationProcessor.class.getName());
        options.add("-Aquerydsl.entityAccessors=true");
        options.add("-cp");
        options.add(classPath);
        options.add("-source");
        options.add("1.6");
        options.add("-verbose");
        options.addAll(classes);

        int compilationResult = compiler.run(null, System.out, System.err, options.toArray(new String[options.size()]));
        if (compilationResult == 0) {
            System.out.println("Compilation is successful");
        } else {
            Assert.fail("Compilation Failed");
        }
View Full Code Here

  /**
   * @return Whether compilation was successful.
   */
  private boolean compileSourceCodeToByteCode(String className, String sourceCode,
      DiagnosticListener<JavaFileObject> diagnosticListener) {
    JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();

    // Set up the in-memory filesystem.
    InMemoryFileManager fileManager =
        new InMemoryFileManager(javaCompiler.getStandardFileManager(null, null, null));
    JavaFileObject javaFile = new InMemoryJavaFile(className, sourceCode);

    // Javac option: remove these when the javac zip impl is fixed
    // (http://b/issue?id=1822932)
    System.setProperty("useJavaUtilZip", "true"); // setting value to any non-null string
    List<String> options = new LinkedList<String>();
    // this is ignored by javac currently but useJavaUtilZip should be
    // a valid javac XD option, which is another bug
    options.add("-XDuseJavaUtilZip");

    // Now compile!
    JavaCompiler.CompilationTask compilationTask = javaCompiler.getTask(null, // Null: log any
                                                                              // unhandled errors to
                                                                              // stderr.
        fileManager, diagnosticListener, options, null, singleton(javaFile));
    return compilationTask.call();
  }
View Full Code Here

TOP

Related Classes of javax.tools.JavaCompiler

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.