Skip to content

Parse Javac warnings #40

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

Merged
merged 2 commits into from
Jan 7, 2018
Merged
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 @@ -669,14 +669,27 @@ static List<CompilerMessage> parseModernStream( int exitCode, BufferedReader inp
{
// javac output not detected by other parsing
// maybe better to ignore only the summary an mark the rest as error
if ( buffer.length() > 0 && ( buffer.toString().startsWith( "javac:" )
|| buffer.toString().startsWith( "An exception has occurred in the compiler" ) ) )
if (buffer.length() > 0 && buffer.toString().startsWith("javac:"))
{
errors.add( new CompilerMessage( buffer.toString(), CompilerMessage.Kind.ERROR ) );
}
return errors;
}

// A compiler error occurred, treat everything that follows as part of the error.
if (line.startsWith( "An exception has occurred in the compiler") ) {
buffer = new StringBuilder();

while (line != null) {
buffer.append(line);
buffer.append(EOL);
line = input.readLine();
}

errors.add( new CompilerMessage( buffer.toString(), CompilerMessage.Kind.ERROR ) );
return errors;
}

// new error block?
if ( !line.startsWith( " " ) && hasPointer )
{
Expand All @@ -692,7 +705,11 @@ static List<CompilerMessage> parseModernStream( int exitCode, BufferedReader inp
// TODO: there should be a better way to parse these
if ( ( buffer.length() == 0 ) && line.startsWith( "error: " ) )
{
errors.add( new CompilerMessage( line, true ) );
errors.add( new CompilerMessage( line, CompilerMessage.Kind.ERROR ) );
}
else if ( ( buffer.length() == 0 ) && line.startsWith( "warning: " ) )
{
errors.add( new CompilerMessage( line, CompilerMessage.Kind.WARNING ) );
}
else if ( ( buffer.length() == 0 ) && isNote( line ) )
{
Expand Down Expand Up @@ -748,12 +765,10 @@ private static boolean startsWithPrefix( String line, String[] prefixes )
*/
static CompilerMessage parseModernError( int exitCode, String error )
{
StringTokenizer tokens = new StringTokenizer( error, ":" );
final StringTokenizer tokens = new StringTokenizer( error, ":" );

boolean isError = exitCode != 0;

StringBuilder msgBuffer;

try
{
// With Java 6 error output lines from the compiler got longer. For backward compatibility
Expand All @@ -762,7 +777,7 @@ static CompilerMessage parseModernError( int exitCode, String error )

boolean tokenIsAnInteger;

String file = null;
StringBuilder file = null;

String currentToken = null;

Expand All @@ -772,11 +787,11 @@ static CompilerMessage parseModernError( int exitCode, String error )
{
if ( file == null )
{
file = currentToken;
file = new StringBuilder(currentToken);
}
else
{
file = file + ':' + currentToken;
file.append(':').append(currentToken);
}
}

Expand All @@ -797,23 +812,23 @@ static CompilerMessage parseModernError( int exitCode, String error )
}
while ( !tokenIsAnInteger );

String lineIndicator = currentToken;
final String lineIndicator = currentToken;

int startOfFileName = file.lastIndexOf( ']' );
final int startOfFileName = file.toString().lastIndexOf( ']' );

if ( startOfFileName > -1 )
{
file = file.substring( startOfFileName + 1 + EOL.length() );
file = new StringBuilder(file.substring(startOfFileName + 1 + EOL.length()));
}

int line = Integer.parseInt( lineIndicator );
final int line = Integer.parseInt( lineIndicator );

msgBuffer = new StringBuilder();
final StringBuilder msgBuffer = new StringBuilder();

String msg = tokens.nextToken( EOL ).substring( 2 );

// Remove the 'warning: ' prefix
String warnPrefix = getWarnPrefix( msg );
final String warnPrefix = getWarnPrefix( msg );
if ( warnPrefix != null )
{
isError = false;
Expand All @@ -834,7 +849,7 @@ static CompilerMessage parseModernError( int exitCode, String error )

do
{
String msgLine = tokens.nextToken( EOL );
final String msgLine = tokens.nextToken( EOL );

if ( pointer != null )
{
Expand All @@ -859,18 +874,18 @@ else if ( msgLine.endsWith( "^" ) )

msgBuffer.append( EOL );

String message = msgBuffer.toString();
final String message = msgBuffer.toString();

int startcolumn = pointer.indexOf( "^" );
final int startcolumn = pointer.indexOf( "^" );

int endcolumn = context == null ? startcolumn : context.indexOf( " ", startcolumn );
int endcolumn = (context == null) ? startcolumn : context.indexOf(" ", startcolumn);

if ( endcolumn == -1 )
{
endcolumn = context.length();
}

return new CompilerMessage( file, isError, line, startcolumn, line, endcolumn, message.trim() );
return new CompilerMessage(file.toString(), isError, line, startcolumn, line, endcolumn, message.trim() );
}
catch ( NoSuchElementException e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ protected int expectedErrors()

protected int expectedWarnings()
{
if (getJavaVersion().contains("1.8")){
// lots of new warnings about obsoletions for future releases
return 30;
}

if ( "1.6".compareTo( getJavaVersion() ) < 0 )
{
// with 1.7 some warning with bootstrap class path not set in conjunction with -source 1.3
return 9;
}

return 2;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,17 @@ public void testCRLF_windows()
"4 warnings" + CRLF;
List<CompilerMessage> compilerMessages =
JavacCompiler.parseModernStream( 0, new BufferedReader( new StringReader( errors ) ) );
assertEquals( "count", 107, compilerMessages.size() );
assertEquals( "count", 187, compilerMessages.size() );
List<CompilerMessage> compilerErrors = new ArrayList<CompilerMessage>( 3 );
for ( CompilerMessage message : compilerMessages ) {
if ( message.getKind() != CompilerMessage.Kind.OTHER ) {
compilerErrors.add( message );
}
}
CompilerMessage error1 = compilerErrors.get( 0 );

assertEquivalent(new CompilerMessage("[options] bootstrap class path not set in conjunction with -source " +
"1.6", CompilerMessage.Kind.WARNING), compilerErrors.get(0));
CompilerMessage error1 = compilerErrors.get( 1 );
assertEquals( "file",
"C:\\commander\\pre\\ec\\ec-http\\src\\main\\java\\com\\electriccloud\\http\\HttpUtil.java",
error1.getFile() );
Expand All @@ -639,7 +642,7 @@ public void testCRLF_windows()
error1.getMessage() );
assertEquals( "line", 31, error1.getStartLine() );
assertEquals( "column", 38, error1.getStartColumn() );
CompilerMessage error2 = compilerErrors.get( 1 );
CompilerMessage error2 = compilerErrors.get( 2 );
assertEquals( "file",
"C:\\commander\\pre\\ec\\ec-http\\src\\main\\java\\com\\electriccloud\\http\\HttpUtil.java",
error2.getFile() );
Expand All @@ -648,7 +651,7 @@ public void testCRLF_windows()
error2.getMessage() );
assertEquals( "line", 151, error2.getStartLine() );
assertEquals( "column", 8, error2.getStartColumn() );
CompilerMessage error3 = compilerErrors.get( 2 );
CompilerMessage error3 = compilerErrors.get( 3 );
assertEquals( "file",
"C:\\commander\\pre\\ec\\ec-http\\src\\main\\java\\com\\electriccloud\\http\\HttpUtil.java",
error3.getFile() );
Expand Down Expand Up @@ -681,7 +684,7 @@ public void testJava6Error() throws Exception

assertTrue( message1.isError() );

assertEquals( "cannot find symbol" + EOL +
assertEquals( "cannot find symbol" + EOL +
"symbol : class Properties" + EOL +
"location: class Error", message1.getMessage() );

Expand All @@ -697,7 +700,7 @@ public void testJava6Error() throws Exception

assertTrue( message2.isError() );

assertEquals( "cannot find symbol" + EOL +
assertEquals( "cannot find symbol" + EOL +
"symbol : class Properties" + EOL +
"location: class Error", message2.getMessage() );

Expand Down Expand Up @@ -732,7 +735,7 @@ public void testJava7Error() throws Exception

assertTrue( message1.isError() );

assertEquals( "error: cannot find symbol" + EOL +
assertEquals( "error: cannot find symbol" + EOL +
" symbol: class Properties" + EOL +
" location: class Error", message1.getMessage() );

Expand All @@ -748,7 +751,7 @@ public void testJava7Error() throws Exception

assertTrue( message2.isError() );

assertEquals( "error: cannot find symbol" + EOL +
assertEquals( "error: cannot find symbol" + EOL +
" symbol: class Properties" + EOL +
" location: class Error", message2.getMessage() );

Expand All @@ -774,5 +777,130 @@ public void testBugParade() throws Exception

assertEquals( 1, compilerErrors.size() );
}


public final void testNonAnchoredWarning() throws IOException {
final String error =
"warning: [options] bootstrap class path not set in conjunction with -source 1.6" + EOL +
"1 warning" + EOL;

final List<CompilerMessage> compilerErrors = JavacCompiler.parseModernStream(0, new BufferedReader(new
StringReader(
error)));

assertNotNull(compilerErrors);
assertEquals(1, compilerErrors.size());
assertEquivalent(
new CompilerMessage(
"[options] bootstrap class path not set in conjunction with -source 1.6", CompilerMessage.Kind.WARNING),
compilerErrors.get(0));
}

public final void testAnchoredWarning() throws IOException {
final String error =
"C:\\repo\\src\\it\\includes-output-when-compiler-forked\\src\\main" +
"\\java\\MyClass.java:23: warning: [divzero] division by zero" + EOL +
" System.out.println(1/0);" + EOL +
" ^" + EOL +
"1 warnings" + EOL;

final List<CompilerMessage> compilerErrors =
JavacCompiler.parseModernStream(0, new BufferedReader(new StringReader(error)));

assertNotNull(compilerErrors);
assertEquals(1, compilerErrors.size());
assertEquivalent(
new CompilerMessage("C:\\repo\\src\\it\\includes-output-when-compiler-forked\\src\\main\\java\\MyClass" +
".java", CompilerMessage.Kind.WARNING, 23, 27, 23, 30, "[divzero] division by zero"),
compilerErrors.get(0));
}

public final void testMixedWarnings() throws IOException {
final String error =
"warning: [options] bootstrap class path not set in conjunction with -source 1.6" + EOL +
"C:\\repo\\src\\it\\includes-output-when-compiler-forked\\src\\main\\java" +
"\\MyClass.java:23: warning: [divzero] division by zero" + EOL +
" System.out.println(1/0);" + EOL +
" ^" + EOL +
"2 warnings";

final List<CompilerMessage> compilerErrors =
JavacCompiler.parseModernStream(0, new BufferedReader(new StringReader(error)));

assertNotNull(compilerErrors);
assertEquals(2, compilerErrors.size());
assertEquivalent(
new CompilerMessage(
"[options] bootstrap class path not set in conjunction with -source 1.6", CompilerMessage.Kind.WARNING),
compilerErrors.get(0));
assertEquivalent(
new CompilerMessage("C:\\repo\\src\\it\\includes-output-when-compiler-forked\\src\\main\\java\\MyClass" +
".java", CompilerMessage.Kind.WARNING, 23, 27, 23, 30, "[divzero] division by zero"),
compilerErrors.get(1));
}

public final void testIssue37() throws IOException {
final String error =
"warning: [path] bad path element \"d:\\maven_repo\\.m2\\repository\\org\\ow2\\asm\\asm-xml\\5.0.3\\asm-5.0.3.jar\": no such file or directory" + EOL +
"warning: [path] bad path element \"d:\\maven_repo\\.m2\\repository\\org\\ow2\\asm\\asm-xml\\5.0.3\\asm-util-5.0.3.jar\": no such file or directory" + EOL +
"warning: [options] bootstrap class path not set in conjunction with -source 1.7" + EOL +
"3 warnings" + EOL +
"An exception has occurred in the compiler (9). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you." + EOL +
"java.lang.NullPointerException" + EOL +
"\tat jdk.zipfs/jdk.nio.zipfs.JarFileSystem.getVersionMap(JarFileSystem.java:137)" + EOL +
"\tat jdk.zipfs/jdk.nio.zipfs.JarFileSystem.createVersionedLinks(JarFileSystem.java:112)" + EOL +
"\tat jdk.zipfs/jdk.nio.zipfs.JarFileSystem.<init>(JarFileSystem.java:85)" + EOL +
"\tat jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:134)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.file.JavacFileManager$ArchiveContainer.<init>(JavacFileManager.java:517)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.file.JavacFileManager.getContainer(JavacFileManager.java:319)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.file.JavacFileManager.list(JavacFileManager.java:715)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.code.ClassFinder.list(ClassFinder.java:722)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.code.ClassFinder.scanUserPaths(ClassFinder.java:655)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.code.ClassFinder.fillIn(ClassFinder.java:526)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.code.ClassFinder.complete(ClassFinder.java:293)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.code.Symbol.complete(Symbol.java:633)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.code.Symbol$PackageSymbol.members(Symbol.java:1120)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.code.Symtab.listPackageModules(Symtab.java:810)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.comp.Enter.visitTopLevel(Enter.java:344)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:529)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.comp.Enter.classEnter(Enter.java:285)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.comp.Enter.classEnter(Enter.java:300)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.comp.Enter.complete(Enter.java:570)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.comp.Enter.main(Enter.java:554)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.main.JavaCompiler.enterTrees(JavaCompiler.java:1052)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:923)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:302)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:162)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:57)" + EOL +
"\tat jdk.compiler/com.sun.tools.javac.Main.main(Main.java:43)";

final List<CompilerMessage> compilerErrors =
JavacCompiler.parseModernStream(0, new BufferedReader(new StringReader(error)));

assertNotNull( compilerErrors );
assertEquals(4, compilerErrors.size());

assertEquivalent(new CompilerMessage("[path] bad path element \"d:\\maven_repo\\" +
".m2\\repository\\org\\ow2\\asm\\asm-xml\\5.0.3\\asm-5.0.3.jar\": no such file or directory",
CompilerMessage.Kind.WARNING), compilerErrors.get(0));
assertEquivalent(new CompilerMessage("warning: [path] bad path element \"d:\\maven_repo\\.m2\\repository\\org\\ow2\\asm\\asm-xml\\5.0.3\\asm-util-5.0.3.jar\": no such file or directory",
CompilerMessage.Kind.WARNING), compilerErrors.get(1));
assertEquivalent(new CompilerMessage("[options] bootstrap class path not set in conjunction with -source 1.7",
CompilerMessage.Kind.WARNING), compilerErrors.get(2));

final CompilerMessage finalMessage = compilerErrors.get(3);
assertEquals(CompilerMessage.Kind.ERROR, finalMessage.getKind());
assertTrue("Starts correctly", finalMessage.getMessage().startsWith("An exception has occurred in the compiler"));
assertTrue("continues through end of output", finalMessage.getMessage().endsWith("\tat jdk.compiler/com.sun" +
".tools.javac.Main.main(Main.java:43)" + EOL));
}

private static void assertEquivalent(CompilerMessage expected, CompilerMessage actual){
assertEquals("Message did not match", expected.getMessage(), actual.getMessage());
assertEquals("Kind did not match", expected.getKind(), actual.getKind());
assertEquals("File did not match", expected.getFile(), actual.getFile());
assertEquals( "Start line did not match", expected.getStartLine(), actual.getStartLine());
assertEquals( "Start column did not match", expected.getStartColumn(), actual.getStartColumn());
assertEquals("End line did not match", expected.getEndLine(), actual.getEndLine());
assertEquals("End column did not match", expected.getEndColumn(), actual.getEndColumn());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,4 @@ public class JavaxToolsCompilerTest
extends AbstractJavacCompilerTest
{
// no op default is to javax.tools if available

protected int expectedWarnings()
{
if (getJavaVersion().contains("1.8")){
// lots of new warnings about obsoletions for future releases
return 30;
}
else if ( "1.6".compareTo( getJavaVersion() ) < 0 )
{
// with 1.7 some warning with bootstrap class path not set in conjunction with -source 1.3
return 9;
}
else
{
return 2;
}
}
}