Java Code Examples for javax.tools.Diagnostic#getEndPosition()
The following examples show how to use
javax.tools.Diagnostic#getEndPosition() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DiagnosticPresenter.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
@Override public void click(Diagnostic diagnostic) { Log.d(TAG, "click() called with: diagnostic = [" + diagnostic + "]"); mMainActivity.closeDrawer(GravityCompat.START); Object source = diagnostic.getSource(); if (source instanceof JavaFileObject && diagnostic.getKind() == Diagnostic.Kind.ERROR) { String path = ((JavaFileObject) source).getName(); int i = mPagePresenter.gotoPage(path); if (i == -1) { mPagePresenter.addPage(path, true); } EditPageContract.SourceView editor = mPagePresenter.getCurrentPage(); if (editor == null) { Log.d(TAG, "click: editor null"); return; } int startPosition = (int) diagnostic.getStartPosition(); int endPosition = (int) diagnostic.getEndPosition(); editor.highlightError(startPosition, endPosition); editor.setCursorPosition(endPosition); } else { // TODO: 19/07/2017 implement other } }
Example 2
Source File: TreeUtilities.java From netbeans with Apache License 2.0 | 6 votes |
/** * Check the tree has compile error in given errors. * * @param tree compilation tree * @param errors Array of error code * @return true if tree has compile error present in list of errors. * @since 2.37 */ public boolean hasError(@NonNull Tree tree, String... errors) { long startPos = info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), tree); long endPos = info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), tree); List<Diagnostic> diagnosticsList = info.getDiagnostics(); for (Diagnostic d : diagnosticsList) { if ((d.getKind() == Diagnostic.Kind.ERROR) && ((d.getStartPosition() >= startPos) && (d.getEndPosition() <= endPos))) { if (errors == null || errors.length == 0) { return true; } else { for (String error : errors) { if (error.equals(d.getCode())) { return true; } } } } } return false; }
Example 3
Source File: EndPositions.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) throws IOException { class MyFileObject extends SimpleJavaFileObject { MyFileObject() { super(URI.create("myfo:///Test.java"), SOURCE); } @Override public String getCharContent(boolean ignoreEncodingErrors) { // 0 1 2 3 // 012345678901234567890123456789012345 return "class Test { String s = 1234; }"; } } JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject()); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); List<String> options = Arrays.asList("-processor", EndPositions.class.getCanonicalName()); JavacTask task = (JavacTask)javac.getTask(null, null, diagnostics, options, null, compilationUnits); boolean valid = task.call(); if (valid) throw new AssertionError("Expected one error, but found none."); List<Diagnostic<? extends JavaFileObject>> errors = diagnostics.getDiagnostics(); if (errors.size() != 1) throw new AssertionError("Expected one error only, but found " + errors.size() + "; errors: " + errors); Diagnostic<?> error = errors.get(0); if (error.getStartPosition() >= error.getEndPosition()) throw new AssertionError("Expected start to be less than end position: start [" + error.getStartPosition() + "], end [" + error.getEndPosition() +"]" + "; diagnostics code: " + error.getCode()); System.out.println("All is good!"); }
Example 4
Source File: EndPositions.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) throws IOException { class MyFileObject extends SimpleJavaFileObject { MyFileObject() { super(URI.create("myfo:///Test.java"), SOURCE); } @Override public String getCharContent(boolean ignoreEncodingErrors) { // 0 1 2 3 // 012345678901234567890123456789012345 return "class Test { String s = 1234; }"; } } JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject()); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); List<String> options = Arrays.asList("-processor", EndPositions.class.getCanonicalName()); JavacTask task = (JavacTask)javac.getTask(null, null, diagnostics, options, null, compilationUnits); boolean valid = task.call(); if (valid) throw new AssertionError("Expected one error, but found none."); List<Diagnostic<? extends JavaFileObject>> errors = diagnostics.getDiagnostics(); if (errors.size() != 1) throw new AssertionError("Expected one error only, but found " + errors.size() + "; errors: " + errors); Diagnostic<?> error = errors.get(0); if (error.getStartPosition() >= error.getEndPosition()) throw new AssertionError("Expected start to be less than end position: start [" + error.getStartPosition() + "], end [" + error.getEndPosition() +"]" + "; diagnostics code: " + error.getCode()); System.out.println("All is good!"); }
Example 5
Source File: TreeEndPosTest.java From hottub with GNU General Public License v2.0 | 5 votes |
static void compile(JavaSource src) throws IOException { ByteArrayOutputStream ba = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(ba); File tempDir = new File("."); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector dc = new DiagnosticCollector(); JavaFileManager javaFileManager = getJavaFileManager(compiler, dc); List<String> options = new ArrayList<>(); options.add("-cp"); options.add(tempDir.getPath()); options.add("-d"); options.add(tempDir.getPath()); options.add("-XDshouldStopPolicy=GENERATE"); List<JavaFileObject> sources = new ArrayList<>(); sources.add(src); JavaCompiler.CompilationTask task = compiler.getTask(writer, javaFileManager, dc, options, null, sources); task.call(); for (Diagnostic diagnostic : (List<Diagnostic>) dc.getDiagnostics()) { long actualStart = diagnostic.getStartPosition(); long actualEnd = diagnostic.getEndPosition(); System.out.println("Source: " + src.source); System.out.println("Diagnostic: " + diagnostic); System.out.print("Start position: Expected: " + src.startPos); System.out.println(", Actual: " + actualStart); System.out.print("End position: Expected: " + src.endPos); System.out.println(", Actual: " + actualEnd); if (src.startPos != actualStart || src.endPos != actualEnd) { throw new RuntimeException("error: trees don't match"); } } }
Example 6
Source File: TreeEndPosTest.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
static void compile(JavaSource src) throws IOException { ByteArrayOutputStream ba = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(ba); File tempDir = new File("."); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector dc = new DiagnosticCollector(); JavaFileManager javaFileManager = getJavaFileManager(compiler, dc); List<String> options = new ArrayList<>(); options.add("-cp"); options.add(tempDir.getPath()); options.add("-d"); options.add(tempDir.getPath()); options.add("-XDshouldStopPolicy=GENERATE"); List<JavaFileObject> sources = new ArrayList<>(); sources.add(src); JavaCompiler.CompilationTask task = compiler.getTask(writer, javaFileManager, dc, options, null, sources); task.call(); for (Diagnostic diagnostic : (List<Diagnostic>) dc.getDiagnostics()) { long actualStart = diagnostic.getStartPosition(); long actualEnd = diagnostic.getEndPosition(); System.out.println("Source: " + src.source); System.out.println("Diagnostic: " + diagnostic); System.out.print("Start position: Expected: " + src.startPos); System.out.println(", Actual: " + actualStart); System.out.print("End position: Expected: " + src.endPos); System.out.println(", Actual: " + actualEnd); if (src.startPos != actualStart || src.endPos != actualEnd) { throw new RuntimeException("error: trees don't match"); } } }
Example 7
Source File: TreeEndPosTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
static void compile(JavaSource src) throws IOException { ByteArrayOutputStream ba = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(ba); File tempDir = new File("."); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector dc = new DiagnosticCollector(); try (JavaFileManager javaFileManager = getJavaFileManager(compiler, dc)) { List<String> options = new ArrayList<>(); options.add("-cp"); options.add(tempDir.getPath()); options.add("-d"); options.add(tempDir.getPath()); options.add("--should-stop:at=GENERATE"); List<JavaFileObject> sources = new ArrayList<>(); sources.add(src); JavaCompiler.CompilationTask task = compiler.getTask(writer, javaFileManager, dc, options, null, sources); task.call(); for (Diagnostic diagnostic : (List<Diagnostic>) dc.getDiagnostics()) { long actualStart = diagnostic.getStartPosition(); long actualEnd = diagnostic.getEndPosition(); System.out.println("Source: " + src.source); System.out.println("Diagnostic: " + diagnostic); System.out.print("Start position: Expected: " + src.startPos); System.out.println(", Actual: " + actualStart); System.out.print("End position: Expected: " + src.endPos); System.out.println(", Actual: " + actualEnd); if (src.startPos != actualStart || src.endPos != actualEnd) { throw new RuntimeException("error: trees don't match"); } } } }
Example 8
Source File: EndPositions.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) throws IOException { class MyFileObject extends SimpleJavaFileObject { MyFileObject() { super(URI.create("myfo:///Test.java"), SOURCE); } @Override public String getCharContent(boolean ignoreEncodingErrors) { // 0 1 2 3 // 012345678901234567890123456789012345 return "class Test { String s = 1234; }"; } } JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject()); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); List<String> options = Arrays.asList("-processor", EndPositions.class.getCanonicalName()); JavacTask task = (JavacTask)javac.getTask(null, null, diagnostics, options, null, compilationUnits); boolean valid = task.call(); if (valid) throw new AssertionError("Expected one error, but found none."); List<Diagnostic<? extends JavaFileObject>> errors = diagnostics.getDiagnostics(); if (errors.size() != 1) throw new AssertionError("Expected one error only, but found " + errors.size() + "; errors: " + errors); Diagnostic<?> error = errors.get(0); if (error.getStartPosition() >= error.getEndPosition()) throw new AssertionError("Expected start to be less than end position: start [" + error.getStartPosition() + "], end [" + error.getEndPosition() +"]" + "; diagnostics code: " + error.getCode()); System.out.println("All is good!"); }
Example 9
Source File: TaskFactory.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override public Diag diag(final Diagnostic<? extends JavaFileObject> d) { return new Diag() { @Override public boolean isError() { return d.getKind() == Diagnostic.Kind.ERROR; } @Override public long getPosition() { return d.getPosition(); } @Override public long getStartPosition() { return d.getStartPosition(); } @Override public long getEndPosition() { return d.getEndPosition(); } @Override public String getCode() { return d.getCode(); } @Override public String getMessage(Locale locale) { return expunge(d.getMessage(locale)); } }; }
Example 10
Source File: TreeEndPosTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
static void compile(JavaSource src) throws IOException { ByteArrayOutputStream ba = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(ba); File tempDir = new File("."); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector dc = new DiagnosticCollector(); JavaFileManager javaFileManager = getJavaFileManager(compiler, dc); List<String> options = new ArrayList<>(); options.add("-cp"); options.add(tempDir.getPath()); options.add("-d"); options.add(tempDir.getPath()); options.add("-XDshouldStopPolicy=GENERATE"); List<JavaFileObject> sources = new ArrayList<>(); sources.add(src); JavaCompiler.CompilationTask task = compiler.getTask(writer, javaFileManager, dc, options, null, sources); task.call(); for (Diagnostic diagnostic : (List<Diagnostic>) dc.getDiagnostics()) { long actualStart = diagnostic.getStartPosition(); long actualEnd = diagnostic.getEndPosition(); System.out.println("Source: " + src.source); System.out.println("Diagnostic: " + diagnostic); System.out.print("Start position: Expected: " + src.startPos); System.out.println(", Actual: " + actualStart); System.out.print("End position: Expected: " + src.endPos); System.out.println(", Actual: " + actualEnd); if (src.startPos != actualStart || src.endPos != actualEnd) { throw new RuntimeException("error: trees don't match"); } } }
Example 11
Source File: EndPositions.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) throws IOException { class MyFileObject extends SimpleJavaFileObject { MyFileObject() { super(URI.create("myfo:///Test.java"), SOURCE); } @Override public String getCharContent(boolean ignoreEncodingErrors) { // 0 1 2 3 // 012345678901234567890123456789012345 return "class Test { String s = 1234; }"; } } JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject()); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); List<String> options = Arrays.asList("-processor", EndPositions.class.getCanonicalName()); JavacTask task = (JavacTask)javac.getTask(null, null, diagnostics, options, null, compilationUnits); boolean valid = task.call(); if (valid) throw new AssertionError("Expected one error, but found none."); List<Diagnostic<? extends JavaFileObject>> errors = diagnostics.getDiagnostics(); if (errors.size() != 1) throw new AssertionError("Expected one error only, but found " + errors.size() + "; errors: " + errors); Diagnostic<?> error = errors.get(0); if (error.getStartPosition() >= error.getEndPosition()) throw new AssertionError("Expected start to be less than end position: start [" + error.getStartPosition() + "], end [" + error.getEndPosition() +"]" + "; diagnostics code: " + error.getCode()); System.out.println("All is good!"); }
Example 12
Source File: PartialReparseTest.java From netbeans with Apache License 2.0 | 5 votes |
public DiagnosticDescription(Diagnostic diag) { this.code = diag.getCode(); this.message = diag.getMessage(Locale.ROOT); this.column = diag.getColumnNumber(); this.endPos = diag.getEndPosition(); this.kind = diag.getKind(); this.line = diag.getLineNumber(); this.pos = diag.getPosition(); this.source = diag.getSource(); this.startPos = diag.getStartPosition(); }
Example 13
Source File: TreeEndPosTest.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
static void compile(JavaSource src) throws IOException { ByteArrayOutputStream ba = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(ba); File tempDir = new File("."); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector dc = new DiagnosticCollector(); JavaFileManager javaFileManager = getJavaFileManager(compiler, dc); List<String> options = new ArrayList<>(); options.add("-cp"); options.add(tempDir.getPath()); options.add("-d"); options.add(tempDir.getPath()); options.add("-XDshouldStopPolicy=GENERATE"); List<JavaFileObject> sources = new ArrayList<>(); sources.add(src); JavaCompiler.CompilationTask task = compiler.getTask(writer, javaFileManager, dc, options, null, sources); task.call(); for (Diagnostic diagnostic : (List<Diagnostic>) dc.getDiagnostics()) { long actualStart = diagnostic.getStartPosition(); long actualEnd = diagnostic.getEndPosition(); System.out.println("Source: " + src.source); System.out.println("Diagnostic: " + diagnostic); System.out.print("Start position: Expected: " + src.startPos); System.out.println(", Actual: " + actualStart); System.out.print("End position: Expected: " + src.endPos); System.out.println(", Actual: " + actualEnd); if (src.startPos != actualStart || src.endPos != actualEnd) { throw new RuntimeException("error: trees don't match"); } } }
Example 14
Source File: TreeEndPosTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
static void compile(JavaSource src) throws IOException { ByteArrayOutputStream ba = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(ba); File tempDir = new File("."); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector dc = new DiagnosticCollector(); JavaFileManager javaFileManager = getJavaFileManager(compiler, dc); List<String> options = new ArrayList<>(); options.add("-cp"); options.add(tempDir.getPath()); options.add("-d"); options.add(tempDir.getPath()); options.add("-XDshouldStopPolicy=GENERATE"); List<JavaFileObject> sources = new ArrayList<>(); sources.add(src); JavaCompiler.CompilationTask task = compiler.getTask(writer, javaFileManager, dc, options, null, sources); task.call(); for (Diagnostic diagnostic : (List<Diagnostic>) dc.getDiagnostics()) { long actualStart = diagnostic.getStartPosition(); long actualEnd = diagnostic.getEndPosition(); System.out.println("Source: " + src.source); System.out.println("Diagnostic: " + diagnostic); System.out.print("Start position: Expected: " + src.startPos); System.out.println(", Actual: " + actualStart); System.out.print("End position: Expected: " + src.endPos); System.out.println(", Actual: " + actualEnd); if (src.startPos != actualStart || src.endPos != actualEnd) { throw new RuntimeException("error: trees don't match"); } } }
Example 15
Source File: EndPositions.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) throws IOException { class MyFileObject extends SimpleJavaFileObject { MyFileObject() { super(URI.create("myfo:///Test.java"), SOURCE); } @Override public String getCharContent(boolean ignoreEncodingErrors) { // 0 1 2 3 // 012345678901234567890123456789012345 return "class Test { String s = 1234; }"; } } JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject()); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); List<String> options = Arrays.asList("-processor", EndPositions.class.getCanonicalName()); JavacTask task = (JavacTask)javac.getTask(null, null, diagnostics, options, null, compilationUnits); boolean valid = task.call(); if (valid) throw new AssertionError("Expected one error, but found none."); List<Diagnostic<? extends JavaFileObject>> errors = diagnostics.getDiagnostics(); if (errors.size() != 1) throw new AssertionError("Expected one error only, but found " + errors.size() + "; errors: " + errors); Diagnostic<?> error = errors.get(0); if (error.getStartPosition() >= error.getEndPosition()) throw new AssertionError("Expected start to be less than end position: start [" + error.getStartPosition() + "], end [" + error.getEndPosition() +"]" + "; diagnostics code: " + error.getCode()); System.out.println("All is good!"); }
Example 16
Source File: EndPositions.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) throws IOException { class MyFileObject extends SimpleJavaFileObject { MyFileObject() { super(URI.create("myfo:///Test.java"), SOURCE); } @Override public String getCharContent(boolean ignoreEncodingErrors) { // 0 1 2 3 // 012345678901234567890123456789012345 return "class Test { String s = 1234; }"; } } JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject()); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); List<String> options = Arrays.asList("-processor", EndPositions.class.getCanonicalName()); JavacTask task = (JavacTask)javac.getTask(null, null, diagnostics, options, null, compilationUnits); boolean valid = task.call(); if (valid) throw new AssertionError("Expected one error, but found none."); List<Diagnostic<? extends JavaFileObject>> errors = diagnostics.getDiagnostics(); if (errors.size() != 1) throw new AssertionError("Expected one error only, but found " + errors.size() + "; errors: " + errors); Diagnostic<?> error = errors.get(0); if (error.getStartPosition() >= error.getEndPosition()) throw new AssertionError("Expected start to be less than end position: start [" + error.getStartPosition() + "], end [" + error.getEndPosition() +"]" + "; diagnostics code: " + error.getCode()); System.out.println("All is good!"); }
Example 17
Source File: EndPositions.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) throws IOException { class MyFileObject extends SimpleJavaFileObject { MyFileObject() { super(URI.create("myfo:///Test.java"), SOURCE); } @Override public String getCharContent(boolean ignoreEncodingErrors) { // 0 1 2 3 // 012345678901234567890123456789012345 return "class Test { String s = 1234; }"; } } JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject()); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); List<String> options = Arrays.asList("-processor", EndPositions.class.getCanonicalName()); JavacTask task = (JavacTask)javac.getTask(null, null, diagnostics, options, null, compilationUnits); boolean valid = task.call(); if (valid) throw new AssertionError("Expected one error, but found none."); List<Diagnostic<? extends JavaFileObject>> errors = diagnostics.getDiagnostics(); if (errors.size() != 1) throw new AssertionError("Expected one error only, but found " + errors.size() + "; errors: " + errors); Diagnostic<?> error = errors.get(0); if (error.getStartPosition() >= error.getEndPosition()) throw new AssertionError("Expected start to be less than end position: start [" + error.getStartPosition() + "], end [" + error.getEndPosition() +"]" + "; diagnostics code: " + error.getCode()); System.out.println("All is good!"); }
Example 18
Source File: TreeEndPosTest.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
static void compile(JavaSource src) throws IOException { ByteArrayOutputStream ba = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(ba); File tempDir = new File("."); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector dc = new DiagnosticCollector(); JavaFileManager javaFileManager = getJavaFileManager(compiler, dc); List<String> options = new ArrayList<>(); options.add("-cp"); options.add(tempDir.getPath()); options.add("-d"); options.add(tempDir.getPath()); options.add("-XDshouldStopPolicy=GENERATE"); List<JavaFileObject> sources = new ArrayList<>(); sources.add(src); JavaCompiler.CompilationTask task = compiler.getTask(writer, javaFileManager, dc, options, null, sources); task.call(); for (Diagnostic diagnostic : (List<Diagnostic>) dc.getDiagnostics()) { long actualStart = diagnostic.getStartPosition(); long actualEnd = diagnostic.getEndPosition(); System.out.println("Source: " + src.source); System.out.println("Diagnostic: " + diagnostic); System.out.print("Start position: Expected: " + src.startPos); System.out.println(", Actual: " + actualStart); System.out.print("End position: Expected: " + src.endPos); System.out.println(", Actual: " + actualEnd); if (src.startPos != actualStart || src.endPos != actualEnd) { throw new RuntimeException("error: trees don't match"); } } }
Example 19
Source File: JavaCustomIndexer.java From netbeans with Apache License 2.0 | 4 votes |
public static void brokenPlatform( @NonNull final Context ctx, @NonNull final Iterable<? extends CompileTuple> files, @NullAllowed final Diagnostic<JavaFileObject> diagnostic) { if (diagnostic == null) { return; } final Diagnostic<JavaFileObject> error = new Diagnostic<JavaFileObject>() { @Override public Kind getKind() { return Kind.ERROR; } @Override public JavaFileObject getSource() { return diagnostic.getSource(); } @Override public long getPosition() { return diagnostic.getPosition(); } @Override public long getStartPosition() { return diagnostic.getStartPosition(); } @Override public long getEndPosition() { return diagnostic.getEndPosition(); } @Override public long getLineNumber() { return diagnostic.getLineNumber(); } @Override public long getColumnNumber() { return diagnostic.getColumnNumber(); } @Override public String getCode() { return diagnostic.getCode(); } @Override public String getMessage(Locale locale) { return diagnostic.getMessage(locale); } }; for (CompileTuple file : files) { if (!file.virtual) { ErrorsCache.setErrors( ctx.getRootURI(), file.indexable, Collections.<Diagnostic<JavaFileObject>>singleton(error), ERROR_CONVERTOR); } } }
Example 20
Source File: RuntimeJavaCompiler.java From spring-init with Apache License 2.0 | 4 votes |
public CompilationResult compile(InputFileDescriptor[] sources, InputFileDescriptor[] resources, CompilationOptions compilationOptions, List<File> dependencies) { logger.debug("Compiling {} source{}", sources.length, sources.length == 1 ? "" : "s"); DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>(); MemoryBasedJavaFileManager fileManager = new MemoryBasedJavaFileManager(); fileManager.addResolvedDependencies(dependencies); List<JavaFileObject> compilationUnits = new ArrayList<>(); for (InputFileDescriptor source : sources) { compilationUnits.add(InMemoryJavaFileObject .getSourceJavaFileObject(source.getClassName(), source.getContent())); } List<String> options = new ArrayList<>(); options.add("-processorpath"); options.add(new File("../processor/target/classes/").toString()); options.add("-source"); options.add("1.8"); options.add("-processor"); options.add("org.springframework.init.processor.SlimConfigurationProcessor"); CompilationTask task = compiler.getTask(null, fileManager, diagnosticCollector, options, null, compilationUnits); boolean success = task.call(); CompilationResult compilationResult = new CompilationResult(success); compilationResult.setDependencies(new ArrayList<>( fileManager.getResolvedAdditionalDependencies().values())); compilationResult.setInputResources(resources); // If successful there may be no errors but there might be info/warnings for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticCollector .getDiagnostics()) { CompilationMessage.Kind kind = (diagnostic.getKind() == Kind.ERROR ? CompilationMessage.Kind.ERROR : CompilationMessage.Kind.OTHER); String sourceCode = null; try { sourceCode = (String) diagnostic.getSource().getCharContent(true); } catch (IOException ioe) { // Unexpected, but leave sourceCode null to indicate it was not // retrievable } catch (NullPointerException npe) { // TODO: should we skip warning diagnostics in the loop altogether? } int startPosition = (int) diagnostic.getPosition(); if (startPosition == Diagnostic.NOPOS) { startPosition = (int) diagnostic.getStartPosition(); } CompilationMessage compilationMessage = new CompilationMessage(kind, diagnostic.getMessage(null), sourceCode, startPosition, (int) diagnostic.getEndPosition()); compilationResult.recordCompilationMessage(compilationMessage); } compilationResult.setGeneratedFiles(fileManager.getOutputFiles()); return compilationResult; }