Example usage for javax.tools DiagnosticCollector DiagnosticCollector

List of usage examples for javax.tools DiagnosticCollector DiagnosticCollector

Introduction

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

Prototype

DiagnosticCollector

Source Link

Usage

From source file:org.wso2.carbon.config.test.annotationprocessor.AnnotationProcessorTest.java

@BeforeClass
public void initClass() {
    //get the java compiler.
    compiler = ToolProvider.getSystemJavaCompiler();
    //configure the diagnostics collector.
    collector = new DiagnosticCollector<>();
    fileManager = compiler.getStandardFileManager(collector, Locale.US, Charset.forName("UTF-8"));
    packagePath = Paths/*from  ww w . jav  a2s .  c o  m*/
            .get("src", "test", "java", "org", "wso2", "carbon", "config", "test", "annotationprocessor")
            .toString();
    classesToCompile = new String[] { Paths.get(packagePath, "TestConfiguration.java").toString() };
}

From source file:org.wso2.carbon.kernel.annotationprocessor.AnnotationProcessorTest.java

@BeforeClass
public void initClass() {
    //get the java compiler.
    compiler = ToolProvider.getSystemJavaCompiler();
    //configure the diagnostics collector.
    collector = new DiagnosticCollector<>();
    fileManager = compiler.getStandardFileManager(collector, Locale.US, Charset.forName("UTF-8"));
    packagePath = Paths.get("src", "test", "java", "org", "wso2", "carbon", "kernel", "annotationprocessor")
            .toString();//from   w  w w  .ja v a2 s . c  o m
    classesToCompile = new String[] { Paths.get(packagePath, "Configurations.java").toString() };
}

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  w  w  .jav a 2s  .  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();
}