Package javax.tools

Examples of javax.tools.JavaFileObject


public class JavaMemoryCompiler {

  private final Logger log = LoggerFactory.getLogger(JavaMemoryCompiler.class);

  public byte[] compile(String fullyQualifiedClassName, String javaSource) {
    JavaFileObject javaObject = new JavaMemoryObject(fullyQualifiedClassName, javaSource);
    return compile(javaObject);
  }
View Full Code Here


    this.javaSource = String.format(sourceSkeleton, className, javaSource);
  }

  public byte[] compile() {
    JavaMemoryCompiler compiler = new JavaMemoryCompiler();
    JavaFileObject javaObject = new JavaMemoryObject(className, javaSource);
    List<Class<? extends Object>> classPath = new ArrayList<Class<? extends Object>>();
    classPath.add(Executable.class);

    return compiler.compile(javaObject, classPath);
  }
View Full Code Here

    }

    @Override
    public Class findClass(String name) throws ClassNotFoundException {
      try {
        JavaFileObject file = jfm.getJavaFileForInput(StandardLocation.CLASS_OUTPUT,
                                                      name, Kind.CLASS);
        if (file == null) {
          throw new FileNotFoundException();
        }
        byte[] bytes = ByteStreams.toByteArray(file.openInputStream());
        return defineClass(name, bytes, 0, bytes.length);
      } catch (IOException e) {
        throw new ClassNotFoundException();
      }
    }
View Full Code Here

    try {
      uri = new URI("drill-class-compiler");
    } catch (URISyntaxException use) {
      throw new RuntimeException(use);
    }
    JavaFileObject javaFileObject = new SimpleJavaFileObject(uri, Kind.SOURCE) {

      @Override
      public boolean isNameCompatible(String simpleName, Kind kind) {
        return true;
      }
View Full Code Here

  @Override
  public byte[] getClassByteCode(final String className, final String sourcecode) throws CompileException, IOException,
      ClassNotFoundException {

    // Create one Java source file in memory, which will be compiled later.
    JavaFileObject compilationUnit = getCompilationUnit(sourcecode);

    //logger.debug("Compiling the following source code\n{}", sourcecode);
    // Run the compiler.
    try {
      CompilationTask task = compiler.getTask(null, fileManager, listener, getOptions(), null, Collections.singleton(compilationUnit));
      long n0 = System.nanoTime();
      if(!task.call()){
        throw new CompileException("Compilation failed", null);
      }
      long n1 = (System.nanoTime() - n0)/1000/1000;
     
    } catch (RuntimeException rte) {
     
      // Unwrap the compilation exception and throw it.
      Throwable cause = rte.getCause();
      if (cause != null) {
        cause = cause.getCause();
        if (cause instanceof CompileException) throw (CompileException) cause;
        if (cause instanceof IOException) throw (IOException) cause;
      }
      throw rte;
    }

    JavaFileObject classFileObject = fileManager.getJavaFileForInput(StandardLocation.CLASS_OUTPUT, className, Kind.CLASS);

    if (classFileObject == null) {
      throw new ClassNotFoundException(className + ": Class file not created by compilation");
    }
View Full Code Here

        new DiagnosticListener<JavaFileObject>() {

      @Override
      public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
        StringBuilder message = new StringBuilder();
        @Nullable JavaFileObject src = diagnostic.getSource();
        if (src != null) {
          // TODO: Map the line and character back to the source inputs.
          message.append(src.getName()).append(':').append(diagnostic.getLineNumber()).append('+')
              .append(diagnostic.getColumnNumber()).append(": ");
        }
        message.append(diagnostic.getMessage(Locale.getDefault()));
        switch (diagnostic.getKind()) {
          case ERROR:
View Full Code Here

     * Creates a file where source code of the given metaClass will be written.
     *
     */
    private PrintWriter createSourceFile(String originalClass, String metaClass, TypeElement e)
        throws IOException {
        JavaFileObject javaFile = processingEnv.getFiler().createSourceFile(metaClass, e);
        logger.info(_loc.get("mmg-process", javaFile.toUri().normalize()));
        return new PrintWriter(javaFile.openWriter());
    }
View Full Code Here

     * Creates a file where source code of the given metaClass will be written.
     *
     */
    private PrintWriter createSourceFile(String originalClass, String metaClass, TypeElement e)
        throws IOException {
        JavaFileObject javaFile = processingEnv.getFiler().createSourceFile(metaClass, e);
        logger.info(_loc.get("mmg-process", javaFile.toUri().normalize()));
        return new PrintWriter(javaFile.openWriter());
    }
View Full Code Here

    /**
     * Returns the {@link AnnotationSourceInfo} object associated with
     * the underlying {@link Diagnostic}, if any.
     */
    protected AnnotationSourceInfo getSourceInfo() {
      JavaFileObject source = diagnostic.getSource();
      if (!(source instanceof SyntheticJavaFile)) {
        return null;
      }
      SyntheticJavaFile synthSource = (SyntheticJavaFile) source;
      Object info = synthSource.getSourceInfo(diagnostic.getLineNumber());
View Full Code Here

        msg += "syntax error";
      } else {
        msg += diagnostic.getMessage(locale);
      }

      JavaFileObject source = diagnostic.getSource();
      if (source != null && sourceInfo != null) {
        try {
          CharSequence chars = source.getCharContent(true);
          return msg + "\n" + formatErrorSnippet(chars, sourceInfo);
        } catch (IOException e) {
          /* No source code available. */
        }
      }
View Full Code Here

TOP

Related Classes of javax.tools.JavaFileObject

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.