Example usage for javax.tools StandardLocation CLASS_PATH

List of usage examples for javax.tools StandardLocation CLASS_PATH

Introduction

In this page you can find the example usage for javax.tools StandardLocation CLASS_PATH.

Prototype

StandardLocation CLASS_PATH

To view the source code for javax.tools StandardLocation CLASS_PATH.

Click Source Link

Document

Location to search for user class files.

Usage

From source file:org.glowroot.common2.repo.util.Compilations.java

public static Class<?> compile(String source) throws Exception {
    JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();

    IsolatedClassLoader isolatedClassLoader = new IsolatedClassLoader();

    StandardJavaFileManager standardFileManager = javaCompiler.getStandardFileManager(diagnosticCollector,
            Locale.ENGLISH, UTF_8);
    standardFileManager.setLocation(StandardLocation.CLASS_PATH, getCompilationClassPath());
    JavaFileManager fileManager = new IsolatedJavaFileManager(standardFileManager, isolatedClassLoader);
    try {//w  w  w  . j a  va2s .  c o  m
        List<JavaFileObject> compilationUnits = Lists.newArrayList();

        String className = getPublicClassName(source);
        int index = className.lastIndexOf('.');
        String simpleName;
        if (index == -1) {
            simpleName = className;
        } else {
            simpleName = className.substring(index + 1);
        }
        compilationUnits.add(new SourceJavaFileObject(simpleName, source));

        JavaCompiler.CompilationTask task = javaCompiler.getTask(null, fileManager, diagnosticCollector, null,
                null, compilationUnits);
        task.call();

        List<Diagnostic<? extends JavaFileObject>> diagnostics = diagnosticCollector.getDiagnostics();
        if (!diagnostics.isEmpty()) {
            List<String> compilationErrors = Lists.newArrayList();
            for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
                compilationErrors.add(checkNotNull(diagnostic.toString()));
            }
            throw new CompilationException(compilationErrors);
        }
        if (className.isEmpty()) {
            throw new CompilationException(ImmutableList.of("Class must be public"));
        }
        return isolatedClassLoader.loadClass(className);
    } finally {
        fileManager.close();
    }
}

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

@Override
public final FileObject getFileForInput(final Location location, final String packageName,
        final String relativeName) throws IOException {
    String loggingName = "library [" + libraryNameForLogging + "] package [" + packageName + "], name ["
            + relativeName + "]";
    if (location == StandardLocation.CLASS_PATH) {
        logger.trace("searching library for file: " + loggingName);
        final FileObject file = getLibraryFile(packageName, relativeName);
        if (file != null) {
            logger.trace("found file in library: " + loggingName);
            return file;
        }// ww  w.j  av a 2  s  . co m
    } else {
        logger.trace("skipping library for non-classpath file: " + loggingName);
    }
    return super.getFileForInput(location, packageName, relativeName);
}

From source file:com.wavemaker.tools.compiler.ProjectCompiler.java

public String compile(final Project project) {
    try {/*  w  ww .  j  av a  2  s. c o m*/
        copyRuntimeServiceFiles(project.getWebAppRootFolder(), project.getClassOutputFolder());
        JavaCompiler compiler = new WaveMakerJavaCompiler();
        StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null);
        standardFileManager.setLocation(StandardLocation.CLASS_PATH, getStandardClassPath());
        ResourceJavaFileManager projectFileManager = new ResourceJavaFileManager(standardFileManager);
        projectFileManager.setLocation(StandardLocation.SOURCE_PATH, project.getSourceFolders());
        projectFileManager.setLocation(StandardLocation.CLASS_OUTPUT,
                Collections.singleton(project.getClassOutputFolder()));
        projectFileManager.setLocation(StandardLocation.CLASS_PATH, getClasspath(project));
        copyResources(project);
        Iterable<JavaFileObject> compilationUnits = projectFileManager.list(StandardLocation.SOURCE_PATH, "",
                Collections.singleton(Kind.SOURCE), true);
        StringWriter compilerOutput = new StringWriter();
        CompilationTask compilationTask = compiler.getTask(compilerOutput, projectFileManager, null,
                getCompilerOptions(project), null, compilationUnits);
        ServiceDefProcessor serviceDefProcessor = configure(new ServiceDefProcessor(), projectFileManager);
        ServiceConfigurationProcessor serviceConfigurationProcessor = configure(
                new ServiceConfigurationProcessor(), projectFileManager);
        compilationTask.setProcessors(Arrays.asList(serviceConfigurationProcessor, serviceDefProcessor));
        if (!compilationTask.call()) {
            throw new WMRuntimeException("Compile failed with output:\n\n" + compilerOutput.toString());
        }
        return compilerOutput.toString();
    } catch (IOException e) {
        throw new WMRuntimeException("Unable to compile " + project.getProjectName(), e);
    }
}

From source file:org.cloudfoundry.practical.demo.web.controller.CompilerController.java

private boolean compile(Folders folders, Writer out) throws IOException {
    JavaFileManager standardFileManager = this.compiler.getStandardFileManager(null, null, null);
    ResourceJavaFileManager fileManager = new ResourceJavaFileManager(standardFileManager);
    fileManager.setLocation(StandardLocation.SOURCE_PATH, folders.getSource());
    fileManager.setLocation(StandardLocation.CLASS_OUTPUT, folders.getOutput());
    fileManager.setLocation(StandardLocation.CLASS_PATH, folders.getLibJars());
    Iterable<? extends JavaFileObject> units = fileManager.list(StandardLocation.SOURCE_PATH, "",
            EnumSet.of(Kind.SOURCE), true);
    CompilationTask task = this.compiler.getTask(out, fileManager, null, COMPILER_OPTIONS, null, units);
    return task.call();
}

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

@Override
public final JavaFileObject getJavaFileForInput(final Location location, final String className,
        final Kind kind) throws IOException {
    String loggingName = "library [" + libraryNameForLogging + "] class [" + className + "], kind [" + kind
            + "]";
    if (location == StandardLocation.CLASS_PATH && kind == Kind.CLASS) {
        logger.trace("searching library for class " + loggingName);
        final JavaFileObject file = getLibraryClassFile(className);
        if (file != null) {
            logger.trace("found class in library: " + loggingName);
            return file;
        }/*from   ww w  .j  a va2s  .  c o m*/
    } else {
        logger.trace("skipping library for non-class or non-classpath class: " + loggingName);
    }
    return super.getJavaFileForInput(location, className, kind);
}

From source file:com.wavemaker.tools.compiler.ProjectCompiler.java

public String compile(Folder webAppRootFolder, Iterable<Folder> sources, Folder destination,
        Iterable<Resource> classpath) {
    try {/*  www.  ja v a2 s. c  om*/
        if (webAppRootFolder != null) {
            copyRuntimeServiceFiles(webAppRootFolder, destination);
        }
        JavaCompiler compiler = new WaveMakerJavaCompiler();
        StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null);
        standardFileManager.setLocation(StandardLocation.CLASS_PATH, getStandardClassPath());
        ResourceJavaFileManager projectFileManager = new ResourceJavaFileManager(standardFileManager);
        projectFileManager.setLocation(StandardLocation.SOURCE_PATH, sources);
        ArrayList<Folder> destinations = new ArrayList<Folder>();
        destinations.add(destination);
        projectFileManager.setLocation(StandardLocation.CLASS_OUTPUT, destinations);
        projectFileManager.setLocation(StandardLocation.CLASS_PATH, classpath);
        Iterable<JavaFileObject> compilationUnits = projectFileManager.list(StandardLocation.SOURCE_PATH, "",
                Collections.singleton(Kind.SOURCE), true);
        StringWriter compilerOutput = new StringWriter();
        CompilationTask compilationTask = compiler.getTask(compilerOutput, projectFileManager, null,
                getCompilerOptions(), null, compilationUnits);
        if (!compilationTask.call()) {
            throw new WMRuntimeException("Compile failed with output:\n\n" + compilerOutput.toString());
        }
        return compilerOutput.toString();
    } catch (IOException e) {
        throw new WMRuntimeException("Compile error: " + e);
    }
}

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

@Override
public final boolean hasLocation(final Location location) {
    boolean result = (location == StandardLocation.CLASS_PATH) || super.hasLocation(location);
    logger.trace("library [" + libraryNameForLogging + "] has location [" + location + "]: " + result);
    return result;
}

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

@Override
public final String inferBinaryName(Location location, JavaFileObject file) {
    String loggingName = "location [" + location + "], file [" + file + "]";
    if (location == StandardLocation.CLASS_PATH) {
        logger.trace("trying to infer binary name in library for " + loggingName);
        String result = inferLibraryBinaryName(file);
        logger.trace("result: " + result);
        if (result != null) {
            return result;
        }//from ww  w .j  ava  2 s.c  om
    }
    return super.inferBinaryName(location, file);
}

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

@Override
public final Iterable<JavaFileObject> list(final Location location, final String packageName,
        final Set<Kind> kinds, final boolean recurse) throws IOException {
    logger.trace("listing library [" + libraryNameForLogging + "] files for location [" + location
            + "], package [" + packageName + "], kinds [" + StringUtils.join(kinds, ", ") + "], recurse ["
            + recurse + "]");
    final Iterable<JavaFileObject> superIterable = super.list(location, packageName, kinds, recurse);
    if (location == StandardLocation.CLASS_PATH && kinds.contains(Kind.CLASS)) {
        final Iterable<JavaFileObject> libraryIterable = listLibraryClassFiles(packageName, recurse);
        logger.trace(/*from   ww w  . ja v  a2  s. c o m*/
                "contributed files from this library: " + StringUtils.join(libraryIterable.iterator(), ", "));
        return new Iterable<JavaFileObject>() {
            @Override
            public Iterator<JavaFileObject> iterator() {
                return GenericTypeUtil
                        .unsafeCast(new IteratorChain(superIterable.iterator(), libraryIterable.iterator()));
            }
        };
    } else {
        logger.trace("no contribution from this library because of location/kinds");
        return superIterable;
    }
}

From source file:edu.harvard.i2b2.fhir.FhirUtil.java

public static List<Class> getAllFhirResourceClasses(String packageName) throws IOException {

    // logger.trace("Running getAllFhirResourceClasses for:" +
    // packageName);
    List<Class> commands = new ArrayList<Class>();

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

    Location location = StandardLocation.CLASS_PATH;

    Set<JavaFileObject.Kind> kinds = new HashSet<JavaFileObject.Kind>();
    kinds.add(JavaFileObject.Kind.CLASS);
    boolean recurse = false;

    Iterable<JavaFileObject> list = fileManager.list(location, packageName, kinds, recurse);

    for (JavaFileObject javaFileObject : list) {

        // commands.add(javaFileObject.getClass());
    }/*  www.j  av a2 s.  c o m*/

    Class c = null;

    for (String x : Arrays.asList(RESOURCE_LIST_REGEX.replace("(", "").replace(")", "").split("\\|")))

    {
        x = "org.hl7.fhir." + x;
        // logger.trace("X:"+x);
        c = Utils.getClassFromClassPath(x);
        if (c != null)
            commands.add(c);
    }
    // logger.trace(commands.toString());
    return commands;
}