Package javax.tools

Examples of javax.tools.StandardJavaFileManager

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

    }

    @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


    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

    public JdkCompiler(){
        options = new ArrayList<String>();
        options.add("-target");
        options.add("1.6");
        StandardJavaFileManager manager = compiler.getStandardFileManager(diagnosticCollector, null, null);
        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader instanceof URLClassLoader
                && (! loader.getClass().getName().equals("sun.misc.Launcher$AppClassLoader"))) {
            try {
                URLClassLoader urlClassLoader = (URLClassLoader) loader;
                List<File> files = new ArrayList<File>();
                for (URL url : urlClassLoader.getURLs()) {
                    files.add(new File(url.getFile()));
                }
                manager.setLocation(StandardLocation.CLASS_PATH, files);
            } catch (IOException e) {
                throw new IllegalStateException(e.getMessage(), e);
            }
        }
        classLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoaderImpl>() {
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

    } catch (IllegalArgumentException e) {
      throw e;
    }

    if (eclipseCompiler2.fileManager instanceof StandardJavaFileManager) {
      StandardJavaFileManager javaFileManager = (StandardJavaFileManager) eclipseCompiler2.fileManager;

      Iterable<? extends File> location = javaFileManager.getLocation(StandardLocation.CLASS_OUTPUT);
      if (location != null) {
        eclipseCompiler2.setDestinationPath(location.iterator().next().getAbsolutePath());
      }
    }
View Full Code Here

  private static void createTestJar(OutputStream outStream, String dummyClassName)
      throws URISyntaxException, IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    JavaFileObject srcFileObject = new SimpleJavaFileObjectImpl(
        URI.create("string:///" + dummyClassName + Kind.SOURCE.extension), Kind.SOURCE);
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    compiler.getTask(null, fileManager, null, null, null, Collections.singletonList(srcFileObject))
        .call();

    JavaFileObject javaFileObject = fileManager.getJavaFileForOutput(StandardLocation.CLASS_OUTPUT,
        dummyClassName, Kind.CLASS, null);

    File classFile = new File(dummyClassName + Kind.CLASS.extension);

    JarOutputStream jarOutputStream = new JarOutputStream(outStream);
View Full Code Here

    }
    logger.debug("Compiling " + fileName);
    //javax.tool compiler api is cool as hell
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnosticListener = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticListener, Locale.getDefault(),
        Charset.forName("utf-8"));
    JavaFileObject java = new StringJavaObject(fileName, javaCode);
    List<JavaFileObject> compilationUnits = Arrays.asList(java);

    //otherwise classes are written to current directory
    String[] options = new String[] { "-d", "target/classes" };
    List<String> compileOptions = Arrays.asList(options);

    CompilationTask task = compiler.getTask(null, fileManager, diagnosticListener, compileOptions, null,
        compilationUnits);
    Boolean result = task.call();
    /*
    List<Diagnostic<? extends JavaFileObject>> diagnostics = diagnosticListener.getDiagnostics();
    for (Diagnostic<? extends JavaFileObject> diagnosticItem : diagnostics) {
      System.out.format("Error in %s", diagnosticItem);
    }
    */
    fileManager.close();

    if (!result) {
      throw new IllegalArgumentException("Compilation failed " + diagnosticListener.getDiagnostics());
    }

View Full Code Here

    private static List<File> compile(File[] javaFiles) {
        LOG.info("Compiling: " + Arrays.asList(javaFiles));

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        Iterable<? extends JavaFileObject> compilationUnits1 =
                fileManager.getJavaFileObjects(javaFiles);
        JavaCompiler.CompilationTask task =
                compiler.getTask(null, fileManager, null, null, null, compilationUnits1);
        task.call();

        List<File> classFiles = Lists.newArrayList();
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

       *
       * The same file manager can be reopened for another compiler task.
       * Thus we reduce the overhead of scanning through file system and jar
       * files each time
       */
      StandardJavaFileManager stdFileManager = compiler.getStandardFileManager( null, Locale.getDefault(), null );

      /*
       * Prepare a list of compilation units (java source code file objects)
       * to input to compilation task
       */
      Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList( javaFileObjects );

      /* Prepare any compilation options to be used during compilation */
      // In this example, we are asking the compiler to place the output
      // files under bin folder.
      String[] compileOptions = new String[] { "-d", "bin" };
      Iterable<String> compilationOptionss = Arrays.asList( compileOptions );

      /* Create a diagnostic controller, which holds the compilation problems */
      DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

      /*
       * Create a compilation task from compiler by passing in the required
       * input objects prepared above
       */
      CompilationTask compilerTask = compiler.getTask( null, stdFileManager, diagnostics, compilationOptionss, null, compilationUnits );

      // Perform the compilation by calling the call method on compilerTask
      // object.
      boolean status = compilerTask.call();

      Object instance = null;
      if( !status ) {// If compilation error occurs
        /* Iterate through each compilation problem and print it */
        for(Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
          System.out.format( "Error on line %d in %s", diagnostic.getLineNumber(), diagnostic );
        }
      } else {
        // make our object
        try {
          Class<?> c = Class.forName( root_package + "." + name );
          instance = c.newInstance();
          // set its fields
          System.out.println("all");
          for(Field f : c.getDeclaredFields() ) {
            System.out.println("Filed: " + f);
          }
          for(Entry<String, Object> entry : data.entrySet()) {
            System.out.println("Field: " + c.getDeclaredField( entry.getKey() ) );
            Field f =c.getDeclaredField( entry.getKey() );
            f.set( instance, entry.getValue() );
          }

        } catch (ClassNotFoundException e) {
          e.printStackTrace();
        } catch (SecurityException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (NoSuchFieldException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (InstantiationException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IllegalAccessException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

      try {
        stdFileManager.close();// Close the file manager
      } catch (IOException e) {
        e.printStackTrace();
      }

      return instance;
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.