Example usage for javax.tools JavaFileObject getName

List of usage examples for javax.tools JavaFileObject getName

Introduction

In this page you can find the example usage for javax.tools JavaFileObject getName.

Prototype

String getName();

Source Link

Document

Returns a user-friendly name for this file object.

Usage

From source file:adalid.util.meta.MetaJavaCompiler.java

private static void scanGeneratedFiles(Iterable<? extends JavaFileObject> files) {
    //      logger.info("files" + EQ + files);
    logger.info("files");
    for (JavaFileObject file : files) {
        //          logger.info("file" + CL + file);
        //          logger.info("kind" + CL + file.getKind());
        logger.info("file" + CL + file + CM + file.getKind() + CM + file.getName());
    }// w w w.  java2 s.  c o  m
}

From source file:name.martingeisse.webide.features.java.compiler.classpath.PlatformClasspathShieldFileManager.java

@Override
public String inferBinaryName(final Location location, final JavaFileObject file) {
    String loggingName = "location [" + location + "], file [" + file + "] / [" + file.getName() + "]";
    if (location == StandardLocation.PLATFORM_CLASS_PATH) {
        logger.trace("standard file manager inferring binary name for " + loggingName);
        final String result = super.inferBinaryName(location, file);
        logger.trace("binary name: " + result);
        return result;
    } else {/*from   w  w w  .  j  av  a2 s .c  o  m*/
        logger.trace("inferring binary name skipped for standard file manager for " + loggingName);
        return null;
    }
}

From source file:org.cloudfoundry.tools.io.compiler.ResourceJavaFileManager.java

@Override
public String inferBinaryName(Location location, JavaFileObject file) {
    String name = getNameWithoutExtension(file.getName());
    ResourceJavaFileObject javaFileObject = getJavaFile(location, name, file.getKind(), true);
    if (javaFileObject != null) {
        String binaryName = javaFileObject.getFile().toString();
        while (binaryName.startsWith("/")) {
            binaryName = binaryName.substring(1);
        }//  w ww.j  a v a  2  s.c  om
        binaryName = getNameWithoutExtension(binaryName);
        binaryName = binaryName.replace("/", ".");
        return binaryName;
    }
    return super.inferBinaryName(location, file);
}

From source file:net.cliseau.composer.javacor.MissingToolException.java

/**
 * Compiles the unit startup file from Java source to Java bytecode.
 *
 * This compiles the startup file. During this process, multiple class files
 * may be generated even though only a single input file to compile is
 * specified. The reason for this is that classes other than the main class
 * may be defined in the single file and are written to distinct class
 * files. All created files are collected and returned by the method.
 *
 * @param startupFile The file to be compiled.
 * @param startupDependencies Names of classpath entries to use for compilation.
 * @return List of names of created (class) files during compilation.
 * @exception UnitGenerationException Thrown when finding a Java compiler failed.
 *///from  w w  w.java 2  s  . com
private LinkedList<String> compile(final File startupFile, final Collection<String> startupDependencies)
        throws MissingToolException, InvalidConfigurationException {
    // code inspired by the examples at
    //   http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html
    final LinkedList<String> createdFiles = new LinkedList<String>();

    // set the file manager, which records the written files
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        throw new MissingToolException("Could not find system Java compiler.");
    }
    StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, null, null);
    JavaFileManager fileManager = new ForwardingJavaFileManager<StandardJavaFileManager>(stdFileManager) {
        /**
         * Collect the list of all output (class) files.
         *
         * Besides its side-effect on the createdFiles list of the containing
         * method, this method is functionally equivalent to its superclass
         * version.
         */
        public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className,
                JavaFileObject.Kind kind, FileObject sibling) throws IOException {
            JavaFileObject fileForOutput = super.getJavaFileForOutput(location, className, kind, sibling);
            createdFiles.addLast(fileForOutput.getName());
            return fileForOutput;
        }
    };

    // set the files to compile
    Iterable<? extends JavaFileObject> compilationUnits = stdFileManager
            .getJavaFileObjectsFromFiles(Arrays.asList(startupFile));

    // do the actual compilation
    ArrayList<String> compileParams = new ArrayList<String>(2);

    boolean verbose = org.apache.log4j.Level.DEBUG.isGreaterOrEqual(config.getInstantiationLogLevel());
    if (verbose)
        compileParams.add("-verbose");

    compileParams
            .addAll(Arrays.asList("-classpath", StringUtils.join(startupDependencies, File.pathSeparator)));
    if (!compiler.getTask(null, fileManager, null, compileParams, null, compilationUnits).call()) {
        // could not compile all files without error
        //TODO: throw an exception ... see where to get the required information from
    }
    return createdFiles;
}

From source file:org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.java

public void persistCompiledClasses(CompilationUnit compilationUnit) {
    JavaFileObjectRegistry registry = compilationUnit.getRegistry();
    File classOutputDirectory = compilationUnit.getOutputClassDirectory();
    if (!classOutputDirectory.exists()) {
        if (!classOutputDirectory.mkdirs())
            throw new IllegalStateException(
                    "Failed to create directory " + classOutputDirectory.getAbsolutePath());
    }//w  ww.  ja  v a2  s.c  o  m
    for (JavaFileObject javaFileObject : registry.get(JavaFileObject.Kind.CLASS)) {
        String internalName = javaFileObject.getName().substring(1);
        File compiledClassFile = new File(classOutputDirectory, internalName);
        if (!compiledClassFile.getParentFile().exists()) {
            if (!compiledClassFile.getParentFile().mkdirs()) {
                throw new IllegalStateException(
                        "Failed to create directories " + compiledClassFile.getParent());
            }
        }
        try {
            FileUtils.writeByteArrayToFile(compiledClassFile,
                    JavaCodeFileObject.class.cast(javaFileObject).getByteCode());
        } catch (IOException e) {
            throw new IllegalStateException("Failed to write to file " + compiledClassFile, e);
        }
    }
}

From source file:org.cloudfoundry.tools.compiler.CloudFoundryJavaCompilerTest.java

@Test
public void shouldCompileVirtualFile() throws Exception {
    JavaFileObject sourceFile = mock(JavaFileObject.class);
    given(sourceFile.getKind()).willReturn(Kind.SOURCE);
    given(sourceFile.getName()).willReturn("Example.java");
    given(sourceFile.getCharContent(anyBoolean())).willReturn(getExampleJavaContent());
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    JavaFileObject classFile = mock(JavaFileObject.class);
    given(classFile.openOutputStream()).willReturn(outputStream);

    JavaFileManager fileManager = mock(JavaFileManager.class);
    given(fileManager.getJavaFileForOutput(Matchers.any(Location.class), anyString(), eq(Kind.CLASS),
            eq(sourceFile))).willReturn(classFile);
    Iterable<? extends JavaFileObject> compilationUnits = Collections.singleton(sourceFile);
    CompilationTask task = this.javaCompiler.getTask(null, fileManager, null, standardCompilerOptions(), null,
            compilationUnits);/*from w w  w .j a v a  2s  . com*/
    assertThat(task.call(), is(Boolean.TRUE));
    assertThat(outputStream.toByteArray().length, is(greaterThan(0)));
}

From source file:org.jsweet.transpiler.util.Util.java

/**
 * Gets the file from a Java file object.
 *//*from w ww.ja v a  2  s .  c  om*/
public static File toFile(JavaFileObject javaFileObject) {
    return new File(javaFileObject.getName());
}