Example usage for javax.tools DiagnosticCollector getDiagnostics

List of usage examples for javax.tools DiagnosticCollector getDiagnostics

Introduction

In this page you can find the example usage for javax.tools DiagnosticCollector getDiagnostics.

Prototype

public List<Diagnostic<? extends S>> getDiagnostics() 

Source Link

Document

Returns a list view of diagnostics collected by this object.

Usage

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

private String compile(String javaFile) throws IOException {
    String errorMessage = null;//from   w w  w  . j  a va2s.  c  om
    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.)";
    }
}

From source file:zhwb.study.compiler.CachedCompiler.java

public Map<String, byte[]> compileFromJava(String className, String javaCode) {
    Iterable<? extends JavaFileObject> compilationUnits;
    if (sourceDir != null) {
        String filename = className.replaceAll("\\.", '\\' + File.separator) + ".java";
        File file = new File(sourceDir, filename);
        writeText(file, javaCode);//w  ww .  ja  va2 s  .com
        compilationUnits = CompilerUtils.s_standardJavaFileManager.getJavaFileObjects(file);

    } else {
        javaFileObjects.add(new JavaSourceFromString(className, javaCode));
        compilationUnits = javaFileObjects;
    }
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

    // reuse the same file manager to allow caching of jar files
    Boolean status = CompilerUtils.s_compiler
            .getTask(null, CompilerUtils.s_fileManager, diagnostics, null, null, compilationUnits).call();
    if (!status) {//If compilation error occurs
        /*Iterate through each compilation problem and print it*/
        StringBuilder sb = new StringBuilder();
        sb.append(StringUtils.LF);
        for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
            sb.append(String.format("Error on line %d in %s", diagnostic.getLineNumber(), diagnostic))
                    .append(StringUtils.LF);
        }
        throw new IllegalStateException(String.format("compile error, %s", sb.toString()));
    }
    return CompilerUtils.s_fileManager.getAllBuffers();
}