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

Allow -log argument for Eclipse compiler - Issue #55 #57

Closed
wants to merge 1 commit into from
Closed
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 @@ -29,11 +29,10 @@
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.test.ArtifactTestCase;
import org.apache.maven.artifact.versioning.VersionRange;

import org.codehaus.plexus.compiler.CompilerMessage.Kind;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;

import javax.print.DocFlavor;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -91,6 +90,22 @@ 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.8.6
*/
protected void configureCompilerConfig(CompilerConfiguration compilerConfig, String filename)
{
configureCompilerConfig( compilerConfig );
}

public void testCompilingSources()
throws Exception
{
Expand All @@ -113,7 +128,9 @@ public void testCompilingSources()

int numCompilerErrors = compilerErrorCount( messages );

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

int numCompilerNotes = compilerNoteCount( messages );

int expectedErrors = expectedErrors();

Expand All @@ -123,7 +140,7 @@ public void testCompilingSources()
List<String> errors = new ArrayList<>();
for ( CompilerMessage error : messages )
{
if ( !error.isError() )
if ( error.getKind() != Kind.ERROR )
{
continue;
}
Expand All @@ -146,7 +163,7 @@ public void testCompilingSources()
System.out.println( numCompilerWarnings + " warning(s) found:" );
for ( CompilerMessage error : messages )
{
if ( error.isError() )
if ( error.getKind() == Kind.ERROR || error.getKind() == Kind.NOTE )
{
continue;
}
Expand All @@ -162,6 +179,25 @@ public void testCompilingSources()
expectedWarnings, numCompilerWarnings );
}

if ( expectedNotes() != numCompilerNotes )
{
System.out.println( numCompilerWarnings + " notes(s) found:" );
for (CompilerMessage error : messages)
{
if ( error.getKind() != Kind.NOTE )
{
continue;
}

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

assertEquals( "Wrong number of compilation notes.", expectedNotes(), numCompilerNotes );
}

assertEquals( new TreeSet<>( normalizePaths( expectedOutputFiles() ) ), files );
}

Expand Down Expand Up @@ -199,7 +235,7 @@ private List<CompilerConfiguration> getCompilerConfigurations()

compilerConfig.setForceJavacCompilerUse( this.forceJavacCompilerUse );

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

String target = getTargetVersion();
if( StringUtils.isNotEmpty( target) )
Expand Down Expand Up @@ -241,16 +277,32 @@ private List<String> normalizePaths( Collection<String> relativePaths )
return normalizedPaths;
}

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

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

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

for ( CompilerMessage message : messages )
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 @@ -263,6 +315,17 @@ protected int expectedWarnings()
return 0;
}

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

protected Collection<String> expectedOutputFiles()
{
return Collections.emptyList();
Expand Down
2 changes: 1 addition & 1 deletion plexus-compilers/plexus-compiler-eclipse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>ecj</artifactId>
<version>3.15.1</version>
<version>3.17.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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.8.6
*/
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 @@ -17,15 +17,17 @@

/**
* @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. */
/*--------------------------------------------------------------*/
/**
* 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 @@ -87,18 +89,32 @@ private void decodeProblems(List<CompilerMessage> list, String sourcePath, XMLSt
private void decodeProblem(List<CompilerMessage> list, String sourcePath, XMLStreamReader xsr, boolean errorsAsWarnings) 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.codehaus.plexus.compiler.eclipse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.codehaus.plexus.compiler.CompilerMessage;
import org.codehaus.plexus.compiler.CompilerMessage.Kind;

/**
* Parser for non-XML Eclipse Compiler output.
*
* @author <a href="mailto:[email protected]">Jason Faust</a>
* @since 2.8.6
*/
public class EcjTextLogParser implements EcjLogParser {

private static final String TEN_DASH = "----------";
private static final String PATTERN_LEVEL_FILE = "^\\d+\\.\\s+(\\w+)\\s+in\\s+(.*)\\s+\\(at line (\\d+)\\)$";

private Pattern levelFile = Pattern.compile(PATTERN_LEVEL_FILE);

@Override
public List<CompilerMessage> parse(File logFile, boolean errorsAsWarnings) throws Exception {
List<CompilerMessage> ret = new ArrayList<>();
try (BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(logFile), StandardCharsets.UTF_8))) {
String line;
List<String> buffer = new LinkedList<>();
boolean header = true;

while ((line = in.readLine()) != null) {
if (header) {
if (!line.startsWith(TEN_DASH)) {
continue;
}
header = false;
}
if (line.startsWith(TEN_DASH)) {
if (!buffer.isEmpty()) {
ret.add(parse(buffer, errorsAsWarnings));
}
buffer.clear();
} else {
buffer.add(line);
}
}
}
return ret;
}

private CompilerMessage parse(List<String> buffer, boolean errorsAsWarnings) {

Kind kind = null;
String file = null;
int lineNum = 0;
int startCol = 0;
int endCol = 0;
String message = null;

// First line, kind, file, lineNum
if (buffer.size() > 1) {
String str = buffer.get(0);
Matcher matcher = levelFile.matcher(str);
if (matcher.find()) {
file = matcher.group(2);
kind = decodeKind(matcher.group(1), errorsAsWarnings);
lineNum = Integer.parseInt(matcher.group(3));
}
}

// Last line, message
if (buffer.size() >= 2) {
String str = buffer.get(buffer.size() - 1);
message = str.trim();
}

// 2nd to last line, columns
if (buffer.size() >= 3) {
String str = buffer.get(buffer.size() - 2);
startCol = str.indexOf('^');
endCol = str.lastIndexOf('^');
}

return new CompilerMessage(file, kind, lineNum, startCol, lineNum, endCol, message);
}

private Kind decodeKind(String str, boolean errorsAsWarnings) {
if (str.equals("ERROR")) {
return errorsAsWarnings ? Kind.WARNING : Kind.ERROR;
}
if (str.equals("INFO")) {
return Kind.NOTE;
}
return Kind.WARNING;
}

}
Loading