From 158fed9b166da1749fac4f779e71fad84f122b00 Mon Sep 17 00:00:00 2001 From: Sven Ketelsen Date: Mon, 2 Feb 2015 13:51:59 +0100 Subject: [PATCH 1/4] support for generating m2e eclipse markers through buildContext --- .../maven/apt/AbstractProcessorMojo.java | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java b/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java index e68e944..06c9e1e 100644 --- a/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java +++ b/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java @@ -5,8 +5,11 @@ */ package com.mysema.maven.apt; +import javax.tools.Diagnostic; +import javax.tools.Diagnostic.Kind; import javax.tools.JavaCompiler; import javax.tools.JavaCompiler.CompilationTask; +import javax.tools.DiagnosticListener; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.ToolProvider; @@ -279,6 +282,41 @@ private Set filterFiles(Set directories) { return files; } + /** + * Add messages through the buildContext: + *
    + *
  • cli build creates log output
  • + *
  • m2e build creates markers for eclipse
  • + *
+ * @param diagnostics + */ + private void processDiagnostics(final List> diagnostics) { + for (Diagnostic diagnostic : diagnostics) { + if (diagnostic != null) { + final JavaFileObject javaFileObject = diagnostic.getSource(); + if (javaFileObject != null) { // message was created without element parameter + final File file = new File(javaFileObject.toUri().getPath()); + final Kind kind = diagnostic.getKind(); + final int lineNumber = (int) diagnostic.getLineNumber(); + final int columnNumber = (int) diagnostic.getColumnNumber(); + final String message = diagnostic.getMessage(Locale.getDefault()); + switch (kind) { + case NOTE: + case OTHER: + break; + case WARNING: + case MANDATORY_WARNING: + buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_WARNING, null); + break; + default: + buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_ERROR, null); + break; + } + } + } + } + } + public void execute() throws MojoExecutionException { if (getOutputDirectory() == null) { return; @@ -319,6 +357,10 @@ public void execute() throws MojoExecutionException { fileManager = compiler.getStandardFileManager(null, null, null); Iterable compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(files); + // clean all markers + for (JavaFileObject javaFileObject : compilationUnits1) { + buildContext.removeMessages(new File(javaFileObject.toUri().getPath())); + } String compileClassPath = buildCompileClasspath(); @@ -341,13 +383,21 @@ public void execute() throws MojoExecutionException { } ExecutorService executor = Executors.newSingleThreadExecutor(); try { - CompilationTask task = compiler.getTask(out, fileManager, null, compilerOptions, null, compilationUnits1); + // the access to buildContext has to made from the current thread (uses ThreadLocal) + final List> diagnostics = new ArrayList>(); + CompilationTask task = compiler.getTask(out, fileManager, new DiagnosticListener() { + @Override + public void report(Diagnostic diagnostic) { + diagnostics.add(diagnostic); + } + }, compilerOptions, null, compilationUnits1); Future future = executor.submit(task); Boolean rv = future.get(); if (Boolean.FALSE.equals(rv) && logOnlyOnError) { getLog().error(out.toString()); } + processDiagnostics(diagnostics); } finally { executor.shutdown(); if (tempDirectory != null) { From 26dad6dbeac7781925ff3fe0b94742583b77deea Mon Sep 17 00:00:00 2001 From: Sven Ketelsen Date: Tue, 3 Feb 2015 09:26:39 +0100 Subject: [PATCH 2/4] indent with 4 spaces --- .../maven/apt/AbstractProcessorMojo.java | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java b/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java index 06c9e1e..d6d5397 100644 --- a/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java +++ b/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java @@ -291,30 +291,30 @@ private Set filterFiles(Set directories) { * @param diagnostics */ private void processDiagnostics(final List> diagnostics) { - for (Diagnostic diagnostic : diagnostics) { - if (diagnostic != null) { - final JavaFileObject javaFileObject = diagnostic.getSource(); - if (javaFileObject != null) { // message was created without element parameter - final File file = new File(javaFileObject.toUri().getPath()); - final Kind kind = diagnostic.getKind(); - final int lineNumber = (int) diagnostic.getLineNumber(); - final int columnNumber = (int) diagnostic.getColumnNumber(); - final String message = diagnostic.getMessage(Locale.getDefault()); - switch (kind) { - case NOTE: - case OTHER: - break; - case WARNING: - case MANDATORY_WARNING: - buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_WARNING, null); - break; - default: - buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_ERROR, null); - break; + for (Diagnostic diagnostic : diagnostics) { + if (diagnostic != null) { + final JavaFileObject javaFileObject = diagnostic.getSource(); + if (javaFileObject != null) { // message was created without element parameter + final File file = new File(javaFileObject.toUri().getPath()); + final Kind kind = diagnostic.getKind(); + final int lineNumber = (int) diagnostic.getLineNumber(); + final int columnNumber = (int) diagnostic.getColumnNumber(); + final String message = diagnostic.getMessage(Locale.getDefault()); + switch (kind) { + case NOTE: + case OTHER: + break; + case WARNING: + case MANDATORY_WARNING: + buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_WARNING, null); + break; + default: + buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_ERROR, null); + break; + } + } } - } } - } } public void execute() throws MojoExecutionException { From 9027fb9acc1064712268b52c5cc0554b5d87d461 Mon Sep 17 00:00:00 2001 From: Sven Ketelsen Date: Tue, 3 Feb 2015 23:01:37 +0100 Subject: [PATCH 3/4] finals removed and changed the order for the switch block --- .../maven/apt/AbstractProcessorMojo.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java b/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java index d6d5397..ff8abc3 100644 --- a/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java +++ b/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java @@ -293,23 +293,24 @@ private Set filterFiles(Set directories) { private void processDiagnostics(final List> diagnostics) { for (Diagnostic diagnostic : diagnostics) { if (diagnostic != null) { - final JavaFileObject javaFileObject = diagnostic.getSource(); + JavaFileObject javaFileObject = diagnostic.getSource(); if (javaFileObject != null) { // message was created without element parameter - final File file = new File(javaFileObject.toUri().getPath()); - final Kind kind = diagnostic.getKind(); - final int lineNumber = (int) diagnostic.getLineNumber(); - final int columnNumber = (int) diagnostic.getColumnNumber(); - final String message = diagnostic.getMessage(Locale.getDefault()); + File file = new File(javaFileObject.toUri().getPath()); + Kind kind = diagnostic.getKind(); + int lineNumber = (int) diagnostic.getLineNumber(); + int columnNumber = (int) diagnostic.getColumnNumber(); + String message = diagnostic.getMessage(Locale.getDefault()); switch (kind) { - case NOTE: - case OTHER: + case ERROR: + buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_ERROR, null); break; case WARNING: case MANDATORY_WARNING: buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_WARNING, null); break; + case NOTE: + case OTHER: default: - buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_ERROR, null); break; } } From 4319a22bd6ec7175b37fc64c0c66a1a2c0e2f3cb Mon Sep 17 00:00:00 2001 From: Sven Ketelsen Date: Wed, 4 Feb 2015 12:47:43 +0100 Subject: [PATCH 4/4] Use DiagnosticCollector instead of DiagnosticListener --- .../maven/apt/AbstractProcessorMojo.java | 54 ++++++++----------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java b/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java index ff8abc3..2b8d352 100644 --- a/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java +++ b/src/main/java/com/mysema/maven/apt/AbstractProcessorMojo.java @@ -9,7 +9,7 @@ import javax.tools.Diagnostic.Kind; import javax.tools.JavaCompiler; import javax.tools.JavaCompiler.CompilationTask; -import javax.tools.DiagnosticListener; +import javax.tools.DiagnosticCollector; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.ToolProvider; @@ -292,27 +292,25 @@ private Set filterFiles(Set directories) { */ private void processDiagnostics(final List> diagnostics) { for (Diagnostic diagnostic : diagnostics) { - if (diagnostic != null) { - JavaFileObject javaFileObject = diagnostic.getSource(); - if (javaFileObject != null) { // message was created without element parameter - File file = new File(javaFileObject.toUri().getPath()); - Kind kind = diagnostic.getKind(); - int lineNumber = (int) diagnostic.getLineNumber(); - int columnNumber = (int) diagnostic.getColumnNumber(); - String message = diagnostic.getMessage(Locale.getDefault()); - switch (kind) { - case ERROR: - buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_ERROR, null); - break; - case WARNING: - case MANDATORY_WARNING: - buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_WARNING, null); - break; - case NOTE: - case OTHER: - default: - break; - } + JavaFileObject javaFileObject = diagnostic.getSource(); + if (javaFileObject != null) { // message was created without element parameter + File file = new File(javaFileObject.toUri().getPath()); + Kind kind = diagnostic.getKind(); + int lineNumber = (int) diagnostic.getLineNumber(); + int columnNumber = (int) diagnostic.getColumnNumber(); + String message = diagnostic.getMessage(Locale.getDefault()); + switch (kind) { + case ERROR: + buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_ERROR, null); + break; + case WARNING: + case MANDATORY_WARNING: + buildContext.addMessage(file, lineNumber, columnNumber, message, BuildContext.SEVERITY_WARNING, null); + break; + case NOTE: + case OTHER: + default: + break; } } } @@ -384,21 +382,15 @@ public void execute() throws MojoExecutionException { } ExecutorService executor = Executors.newSingleThreadExecutor(); try { - // the access to buildContext has to made from the current thread (uses ThreadLocal) - final List> diagnostics = new ArrayList>(); - CompilationTask task = compiler.getTask(out, fileManager, new DiagnosticListener() { - @Override - public void report(Diagnostic diagnostic) { - diagnostics.add(diagnostic); - } - }, compilerOptions, null, compilationUnits1); + DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); + CompilationTask task = compiler.getTask(out, fileManager, diagnosticCollector, compilerOptions, null, compilationUnits1); Future future = executor.submit(task); Boolean rv = future.get(); if (Boolean.FALSE.equals(rv) && logOnlyOnError) { getLog().error(out.toString()); } - processDiagnostics(diagnostics); + processDiagnostics(diagnosticCollector.getDiagnostics()); } finally { executor.shutdown(); if (tempDirectory != null) {