Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse and forward JavaC 'Note:' level messages #312

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
import org.codehaus.plexus.compiler.CompilerMessage.Kind;
import org.codehaus.plexus.testing.PlexusTest;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.ReaderFactory;
Expand Down Expand Up @@ -127,6 +128,20 @@ protected List<String> getClasspath() throws Exception {

protected void configureCompilerConfig(CompilerConfiguration compilerConfig) {}

/**
* Called once per compile iteration to allow configuration customization for
* tests.
*
* @param compilerConfig
* configuration used for this compile iteration.
* @param filename
* file about to be compiled this iteration.
* @since 2.14.0
*/
protected void configureCompilerConfig(CompilerConfiguration compilerConfig, String filename) {
configureCompilerConfig(compilerConfig);
}

@Test
public void testCompilingSources() throws Exception {
List<CompilerMessage> messages = new ArrayList<>();
Expand All @@ -144,25 +159,24 @@ public void testCompilingSources() throws Exception {

int numCompilerErrors = compilerErrorCount(messages);

int numCompilerWarnings = messages.size() - numCompilerErrors;
int numCompilerWarnings = compilerWarningCount(messages);

int expectedErrors = expectedErrors();
int numCompilerNotes = compilerNoteCount(messages);

int expectedErrors = expectedErrors();
if (expectedErrors != numCompilerErrors) {
System.out.println(numCompilerErrors + " error(s) found:");
List<String> errors = new ArrayList<>();
for (CompilerMessage error : messages) {
if (!error.isError()) {
for (CompilerMessage msg : messages) {
if (msg.getKind() != Kind.ERROR) {
continue;
}

System.out.println("----");
System.out.println(error.getFile());
System.out.println(error.getMessage());
System.out.println(msg.getFile());
System.out.println(msg.getMessage());
System.out.println("----");
errors.add(error.getMessage());
errors.add(msg.getMessage());
}

assertThat(
"Wrong number of compilation errors (" + numCompilerErrors + "/" + expectedErrors //
+ ") : " + displayLines(errors),
Expand All @@ -174,18 +188,16 @@ public void testCompilingSources() throws Exception {
if (expectedWarnings != numCompilerWarnings) {
List<String> warnings = new ArrayList<>();
System.out.println(numCompilerWarnings + " warning(s) found:");
for (CompilerMessage error : messages) {
if (error.isError()) {
for (CompilerMessage msg : messages) {
if (msg.getKind() != Kind.WARNING && msg.getKind() != Kind.MANDATORY_WARNING) {
continue;
}

System.out.println("----");
System.out.println(error.getFile());
System.out.println(error.getMessage());
System.out.println(msg.getFile());
System.out.println(msg.getMessage());
System.out.println("----");
warnings.add(error.getMessage());
warnings.add(msg.getMessage());
}

assertThat(
"Wrong number ("
+ numCompilerWarnings + "/" + expectedWarnings + ") of compilation warnings: "
Expand All @@ -194,6 +206,28 @@ public void testCompilingSources() throws Exception {
is(expectedWarnings));
}

int expectedNotes = expectedNotes();
if (expectedNotes != numCompilerNotes) {
List<String> notes = new ArrayList<>();
System.out.println(numCompilerWarnings + " notes(s) found:");
for (CompilerMessage msg : messages) {
if (msg.getKind() != Kind.NOTE) {
continue;
}
System.out.println("----");
System.out.println(msg.getFile());
System.out.println(msg.getMessage());
System.out.println("----");
notes.add(msg.getMessage());
}
assertThat(
"Wrong number ("
+ numCompilerNotes + "/" + expectedNotes + ") of compilation notes: "
+ displayLines(notes),
numCompilerNotes,
is(expectedNotes));
}

assertThat(
files, containsInAnyOrder(normalizePaths(expectedOutputFiles()).toArray(new String[0])));
}
Expand Down Expand Up @@ -237,7 +271,7 @@ private List<CompilerConfiguration> getCompilerConfigurations() throws Exception

compilerConfig.setForceJavacCompilerUse(this.forceJavacCompilerUse);

configureCompilerConfig(compilerConfig);
configureCompilerConfig(compilerConfig, filename);

String target = getTargetVersion();
if (StringUtils.isNotEmpty(target)) {
Expand Down Expand Up @@ -269,14 +303,26 @@ private List<String> normalizePaths(Collection<String> relativePaths) {
.collect(Collectors.toList());
}

protected int compilerErrorCount(List<CompilerMessage> messages) {
int count = 0;
private int compilerErrorCount(List<CompilerMessage> messages) {
return countKind(messages, Kind.ERROR);
}

private int compilerWarningCount(List<CompilerMessage> messages) {
return messages.size() - (compilerErrorCount(messages) + compilerNoteCount(messages));
}

private int compilerNoteCount(List<CompilerMessage> messages) {
return countKind(messages, Kind.NOTE);
}

private int countKind(List<CompilerMessage> messages, Kind kind) {
int c = 0;
for (CompilerMessage message : messages) {
count += message.isError() ? 1 : 0;
if (message.getKind() == kind) {
c++;
}
}

return count;
return c;
}

protected int expectedErrors() {
Expand All @@ -287,6 +333,16 @@ protected int expectedWarnings() {
return 0;
}

/**
* Count of output generated at the {@link Kind#NOTE} level.
*
* @return count
* @since 2.14.0
*/
protected int expectedNotes() {
return 0;
}

protected Collection<String> expectedOutputFiles() {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* The MIT License
*
* Copyright (c) 2005, The Codehaus
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.codehaus.plexus.compiler.eclipse;

import java.io.File;
import java.util.List;

import org.codehaus.plexus.compiler.CompilerMessage;

/**
* Log parser interface.
*
* @author <a href="mailto:[email protected]">Jason Faust</a>
* @since 2.14.0
*/
public interface EcjLogParser {

/**
* Pares an Eclipse Compiler log file.
*
* @param logFile file to parse.
* @param errorsAsWarnings if errors should be down-graded to warnings.
* @return the messages.
* @throws Exception on parse errors.
*/
List<CompilerMessage> parse(File logFile, boolean errorsAsWarnings) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

/**
* @author <a href="mailto:[email protected]">Frits Jalvingh</a>
* @author <a href="mailto:[email protected]">Jason Faust</a>
* Created on 31-3-18.
*/
public class EcjResponseParser {
public class EcjResponseParser implements EcjLogParser {
/*--------------------------------------------------------------*/
/* CODING: Decode ECJ -log format results. */
/*--------------------------------------------------------------*/
Expand All @@ -40,6 +41,7 @@ private static XMLInputFactory getStreamFactory() {
* @param errorsAsWarnings should we treat errors as warnings
* Scan the specified response file for compilation messages.
*/
@Override
public List<CompilerMessage> parse(File xmltf, boolean errorsAsWarnings) throws Exception {
// if(xmltf.length() < 80)
// return;
Expand Down Expand Up @@ -106,20 +108,33 @@ private void decodeProblem(
throws Exception {
String id = xsr.getAttributeValue(null, "optionKey"); // Key for the problem
int startline = getInt(xsr, "line");
int column = getInt(xsr, "charStart");
int endCol = getInt(xsr, "charEnd");
int column = 0;
int endCol = 0;
String sev = xsr.getAttributeValue(null, "severity");
String message = "Unknown message?";

// -- Go watch for "message"
// -- Go watch for "message" and "source_context"
while (xsr.hasNext()) {
int type = xsr.nextTag();
if (type == XMLStreamConstants.START_ELEMENT) {
if ("message".equals(xsr.getLocalName())) {
message = xsr.getAttributeValue(null, "value");
}
if ("source_context".equals(xsr.getLocalName())) {
column = getInt(xsr, "sourceStart");
if (column != -1) {
column += 1; // Offset to 1-based index
} else {
column = 0;
}
endCol = getInt(xsr, "sourceEnd");
if (endCol != -1) {
endCol += 1; // Offset to 1-based index
} else {
endCol = 0;
}
}
ignoreTillEnd(xsr);

} else if (type == XMLStreamConstants.END_ELEMENT) {
break;
}
Expand Down
Loading