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.cdmckay.coffeep.Program.java

private static JavaFileObject getFileObject(String className) throws IOException {
    final Context context = new Context();
    final JavaFileManager fileManager = new JavacFileManager(context, true, null);

    JavaFileObject fileObject;/*from   w  w w.j ava 2  s.  c om*/

    fileObject = fileManager.getJavaFileForInput(StandardLocation.PLATFORM_CLASS_PATH, className,
            JavaFileObject.Kind.CLASS);
    if (fileObject != null)
        return fileObject;

    fileObject = fileManager.getJavaFileForInput(StandardLocation.CLASS_PATH, className,
            JavaFileObject.Kind.CLASS);
    if (fileObject != null)
        return fileObject;

    final StandardJavaFileManager standardFileManager = (StandardJavaFileManager) fileManager;
    return standardFileManager.getJavaFileObjects(className).iterator().next();
}

From source file:org.openmrs.logic.CompilingClassLoader.java

private String compile(String javaFile) throws IOException {
    String errorMessage = null;/*from  w  w  w. j  a v a  2  s . co  m*/
    String ruleClassDir = Context.getAdministrationService()
            .getGlobalProperty(LogicConstants.RULE_DEFAULT_CLASS_FOLDER);
    String ruleJavaDir = Context.getAdministrationService()
            .getGlobalProperty(LogicConstants.RULE_DEFAULT_SOURCE_FOLDER);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    log.info("JavaCompiler is null: " + compiler == null);
    if (compiler != null) {
        // java compiler only available on JDK. This part of "IF" will not get executed when we run JUnit test
        File outputFolder = OpenmrsUtil.getDirectoryInApplicationDataDirectory(ruleClassDir);
        String[] options = {};
        DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector,
                Context.getLocale(), Charset.defaultCharset());
        fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(outputFolder));
        // create compiling classpath
        String stringProperties = System.getProperty("java.class.path");
        if (log.isDebugEnabled())
            log.debug("Compiler classpath: " + stringProperties);
        String[] properties = StringUtils.split(stringProperties, File.pathSeparator);
        List<File> classpathFiles = new ArrayList<File>();
        for (String property : properties) {
            File f = new File(property);
            if (f.exists())
                classpathFiles.add(f);
        }
        classpathFiles.addAll(getCompilerClasspath());
        fileManager.setLocation(StandardLocation.CLASS_PATH, classpathFiles);
        // create the compilation task
        CompilationTask compilationTask = compiler.getTask(null, fileManager, diagnosticCollector,
                Arrays.asList(options), null, fileManager.getJavaFileObjects(javaFile));
        boolean success = compilationTask.call();
        fileManager.close();
        if (success) {
            return null;
        } else {
            errorMessage = "";
            for (Diagnostic<?> diagnostic : diagnosticCollector.getDiagnostics()) {
                errorMessage += diagnostic.getLineNumber() + ": " + diagnostic.getMessage(Context.getLocale())
                        + "\n";
            }
            return errorMessage;
        }
    } else {
        File outputFolder = OpenmrsUtil.getDirectoryInApplicationDataDirectory(ruleClassDir);
        String[] commands = { "javac", "-classpath", System.getProperty("java.class.path"), "-d",
                outputFolder.getAbsolutePath(), javaFile };
        // Start up the compiler
        File workingDirectory = OpenmrsUtil.getDirectoryInApplicationDataDirectory(ruleJavaDir);
        boolean success = LogicUtil.executeCommand(commands, workingDirectory);
        return success ? null : "Compilation error. (You must be using the JDK to see details.)";
    }
}