Skip to content

Commit 84eba2e

Browse files
authored
Update Error prone 2.4 (#81)
* Fix method and attributes visibility * Convert JavaxToolsCompiler from static to instance usage to allow subclassing * Introduce provider methods to allow replacement by subclassing * Introduce InProcessCompiler interface to ease isolated classloading * Update JavacCompilerWithErrorProne to use ErrorProneJavaCompiler as javax.tools.JavaCompiler * Update error-prone to 2.4.0 * Update tests for error-prone 2.4.0
1 parent 8cf02f6 commit 84eba2e

File tree

14 files changed

+117
-213
lines changed

14 files changed

+117
-213
lines changed

plexus-compilers/plexus-compiler-javac-errorprone/pom.xml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,8 @@
2929
<dependency>
3030
<groupId>com.google.errorprone</groupId>
3131
<artifactId>error_prone_core</artifactId>
32-
<version>2.3.4</version>
32+
<version>2.4.0</version>
3333
</dependency>
3434
</dependencies>
3535

36-
<build>
37-
<plugins>
38-
<plugin>
39-
<groupId>org.apache.maven.plugins</groupId>
40-
<artifactId>maven-surefire-plugin</artifactId>
41-
<configuration>
42-
<!--
43-
FIXME enable that with fixing classloader hierarchy
44-
it's a bit different from the one used by maven plugins
45-
-->
46-
<skipTests>true</skipTests>
47-
</configuration>
48-
</plugin>
49-
</plugins>
50-
</build>
51-
5236
</project>

plexus-compilers/plexus-compiler-javac-errorprone/src/main/java/org/codehaus/plexus/compiler/javac/errorprone/JavacCompilerWithErrorProne.java

Lines changed: 23 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,22 @@
1616

1717
package org.codehaus.plexus.compiler.javac.errorprone;
1818

19-
import com.google.errorprone.ErrorProneCompiler;
20-
import org.codehaus.plexus.compiler.AbstractCompiler;
19+
import com.google.errorprone.ErrorProneJavaCompiler;
20+
2121
import org.codehaus.plexus.compiler.CompilerConfiguration;
2222
import org.codehaus.plexus.compiler.CompilerException;
2323
import org.codehaus.plexus.compiler.CompilerMessage;
24-
import org.codehaus.plexus.compiler.CompilerOutputStyle;
2524
import org.codehaus.plexus.compiler.CompilerResult;
25+
import org.codehaus.plexus.compiler.javac.InProcessCompiler;
2626
import org.codehaus.plexus.compiler.javac.JavacCompiler;
27+
import org.codehaus.plexus.compiler.javac.JavaxToolsCompiler;
28+
29+
import javax.tools.JavaCompiler;
30+
import javax.tools.ToolProvider;
2731

28-
import javax.tools.Diagnostic;
29-
import javax.tools.DiagnosticListener;
30-
import javax.tools.JavaFileObject;
31-
import java.io.File;
32-
import java.lang.reflect.Method;
3332
import java.net.MalformedURLException;
3433
import java.net.URL;
3534
import java.net.URLClassLoader;
36-
import java.util.ArrayList;
37-
import java.util.List;
38-
import java.util.Locale;
3935

4036
/**
4137
* This class overrides JavacCompiler with modifications to use the error-prone
@@ -45,58 +41,8 @@
4541
* @plexus.component role="org.codehaus.plexus.compiler.Compiler" role-hint="javac-with-errorprone"
4642
*/
4743
public class JavacCompilerWithErrorProne
48-
extends AbstractCompiler
44+
extends JavacCompiler
4945
{
50-
public JavacCompilerWithErrorProne()
51-
{
52-
super( CompilerOutputStyle.ONE_OUTPUT_FILE_PER_INPUT_FILE, ".java", ".class", null );
53-
}
54-
55-
public String[] createCommandLine( CompilerConfiguration config )
56-
throws CompilerException
57-
{
58-
return new String[0];
59-
}
60-
61-
@Override
62-
public CompilerResult performCompile( CompilerConfiguration config )
63-
throws CompilerException
64-
{
65-
File destinationDir = new File( config.getOutputLocation() );
66-
67-
if ( !destinationDir.exists() )
68-
{
69-
destinationDir.mkdirs();
70-
}
71-
72-
String[] sourceFiles = getSourceFiles( config );
73-
74-
if ( ( sourceFiles == null ) || ( sourceFiles.length == 0 ) )
75-
{
76-
return new CompilerResult();
77-
}
78-
79-
if ( ( getLogger() != null ) && getLogger().isInfoEnabled() )
80-
{
81-
getLogger().info( "Compiling " + sourceFiles.length + " " //
82-
+ "source file" //
83-
+ ( sourceFiles.length == 1 ? "" : "s" ) //
84-
+ " to " + destinationDir.getAbsolutePath() );
85-
}
86-
87-
String[] args = JavacCompiler.buildCompilerArguments( config, sourceFiles );
88-
89-
try
90-
{
91-
CompilerResult compilerResult = (CompilerResult) getInvoker().invoke( null, new Object[]{ args } );
92-
return compilerResult;
93-
}
94-
catch ( Exception e )
95-
{
96-
throw new CompilerException( e.getMessage(), e );
97-
}
98-
}
99-
10046
private static class NonDelegatingClassLoader
10147
extends URLClassLoader
10248
{
@@ -115,6 +61,10 @@ public Class<?> loadClass( String name, boolean complete )
11561
{
11662
// Classes loaded inside CompilerInvoker that need to reach back to the caller
11763
if ( name.contentEquals( CompilerResult.class.getName() )
64+
|| name.contentEquals( InProcessCompiler.class.getName() )
65+
|| name.contentEquals( CompilerConfiguration.class.getName() )
66+
|| name.contentEquals( CompilerConfiguration.CompilerReuseStrategy.class.getName() )
67+
|| name.contentEquals( CompilerException.class.getName() )
11868
|| name.contentEquals( CompilerMessage.class.getName() )
11969
|| name.contentEquals( CompilerMessage.Kind.class.getName() ) )
12070
{
@@ -140,12 +90,9 @@ public Class<?> loadClass( String name, boolean complete )
14090
}
14191
}
14292

143-
private Method invokerMethod;
144-
145-
private Method getInvoker()
146-
throws CompilerException
93+
protected InProcessCompiler inProcessCompiler()
14794
{
148-
if ( invokerMethod == null )
95+
if ( Thread.currentThread().getContextClassLoader().getResource("java/lang/module/ModuleReference.class") == null )
14996
{
15097
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
15198
URL[] urls = ( (URLClassLoader) contextClassLoader ).getURLs();
@@ -154,14 +101,14 @@ private Method getInvoker()
154101
{
155102
loader = new NonDelegatingClassLoader( urls, contextClassLoader );
156103
Class<?> clazz = Class.forName( CompilerInvoker.class.getName(), true, loader );
157-
invokerMethod = clazz.getMethod( "compile", String[].class );
104+
return ( InProcessCompiler ) clazz.newInstance();
158105
}
159106
catch ( Exception e )
160107
{
161-
throw new CompilerException( e.getMessage(), e );
108+
throw new IllegalStateException( e );
162109
}
163110
}
164-
return invokerMethod;
111+
return new CompilerInvoker();
165112
}
166113

167114
/**
@@ -170,57 +117,12 @@ private Method getInvoker()
170117
* javac.jar instead of from the bootclasspath.
171118
*/
172119
public static class CompilerInvoker
120+
extends JavaxToolsCompiler
173121
{
174-
private static class MessageListener
175-
implements DiagnosticListener<JavaFileObject>
176-
{
177-
private final List<CompilerMessage> messages;
178-
179-
MessageListener( List<CompilerMessage> messages )
180-
{
181-
this.messages = messages;
182-
}
183-
184-
public static CompilerMessage.Kind convertKind( Diagnostic<? extends JavaFileObject> diagnostic )
185-
{
186-
switch ( diagnostic.getKind() )
187-
{
188-
case ERROR:
189-
return CompilerMessage.Kind.ERROR;
190-
case WARNING:
191-
return CompilerMessage.Kind.WARNING;
192-
case MANDATORY_WARNING:
193-
return CompilerMessage.Kind.MANDATORY_WARNING;
194-
case NOTE:
195-
return CompilerMessage.Kind.NOTE;
196-
default:
197-
return CompilerMessage.Kind.OTHER;
198-
}
199-
}
200-
201-
public void report( Diagnostic<? extends JavaFileObject> diagnostic )
202-
{
203-
CompilerMessage compilerMessage =
204-
new CompilerMessage( diagnostic.getSource() == null ? null : diagnostic.getSource().getName(), //
205-
convertKind( diagnostic ), //
206-
(int) diagnostic.getLineNumber(), //
207-
(int) diagnostic.getColumnNumber(), //
208-
-1, //
209-
-1,
210-
// end pos line:column is hard to calculate
211-
diagnostic.getMessage( Locale.getDefault() ) );
212-
messages.add( compilerMessage );
213-
}
214-
}
215-
216-
public static CompilerResult compile( String[] args )
217-
{
218-
List<CompilerMessage> messages = new ArrayList<>();
219-
ErrorProneCompiler compiler = //
220-
ErrorProneCompiler.builder() //
221-
.listenToDiagnostics( new MessageListener( messages ) ) //
222-
.build();
223-
return new CompilerResult( compiler.run( args ).isOK(), messages );
224-
}
122+
@Override
123+
protected JavaCompiler newJavaCompiler()
124+
{
125+
return new ErrorProneJavaCompiler();
126+
}
225127
}
226128
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.Set;
2+
import java.util.HashSet;
3+
4+
public class ShortSet {
5+
public static void main (String[] args) {
6+
Set<Short> s = new HashSet<>();
7+
for (short i = 0; i < 100; i++) {
8+
s.add(i);
9+
s.remove(i - 1);
10+
}
11+
System.out.println(s.size());
12+
}
13+
}

plexus-compilers/plexus-compiler-javac-errorprone/src/test-input/src/main/org/codehaus/foo/Bad.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

plexus-compilers/plexus-compiler-javac-errorprone/src/test-input/src/main/org/codehaus/foo/Deprecation.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

plexus-compilers/plexus-compiler-javac-errorprone/src/test-input/src/main/org/codehaus/foo/ExternalDeps.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

plexus-compilers/plexus-compiler-javac-errorprone/src/test-input/src/main/org/codehaus/foo/Person.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

plexus-compilers/plexus-compiler-javac-errorprone/src/test-input/src/main/org/codehaus/foo/ReservedWord.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

plexus-compilers/plexus-compiler-javac-errorprone/src/test-input/src/main/org/codehaus/foo/UnknownSymbol.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

plexus-compilers/plexus-compiler-javac-errorprone/src/test-input/src/main/org/codehaus/foo/WrongClassname.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)