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

Update Error prone 2.4 #81

Merged
merged 7 commits into from
Jul 12, 2020
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
18 changes: 1 addition & 17 deletions plexus-compilers/plexus-compiler-javac-errorprone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,8 @@
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.3.4</version>
<version>2.4.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!--
FIXME enable that with fixing classloader hierarchy
it's a bit different from the one used by maven plugins
-->
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,22 @@

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

import com.google.errorprone.ErrorProneCompiler;
import org.codehaus.plexus.compiler.AbstractCompiler;
import com.google.errorprone.ErrorProneJavaCompiler;

import org.codehaus.plexus.compiler.CompilerConfiguration;
import org.codehaus.plexus.compiler.CompilerException;
import org.codehaus.plexus.compiler.CompilerMessage;
import org.codehaus.plexus.compiler.CompilerOutputStyle;
import org.codehaus.plexus.compiler.CompilerResult;
import org.codehaus.plexus.compiler.javac.InProcessCompiler;
import org.codehaus.plexus.compiler.javac.JavacCompiler;
import org.codehaus.plexus.compiler.javac.JavaxToolsCompiler;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileObject;
import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* This class overrides JavacCompiler with modifications to use the error-prone
Expand All @@ -45,58 +41,8 @@
* @plexus.component role="org.codehaus.plexus.compiler.Compiler" role-hint="javac-with-errorprone"
*/
public class JavacCompilerWithErrorProne
extends AbstractCompiler
extends JavacCompiler
{
public JavacCompilerWithErrorProne()
{
super( CompilerOutputStyle.ONE_OUTPUT_FILE_PER_INPUT_FILE, ".java", ".class", null );
}

public String[] createCommandLine( CompilerConfiguration config )
throws CompilerException
{
return new String[0];
}

@Override
public CompilerResult performCompile( CompilerConfiguration config )
throws CompilerException
{
File destinationDir = new File( config.getOutputLocation() );

if ( !destinationDir.exists() )
{
destinationDir.mkdirs();
}

String[] sourceFiles = getSourceFiles( config );

if ( ( sourceFiles == null ) || ( sourceFiles.length == 0 ) )
{
return new CompilerResult();
}

if ( ( getLogger() != null ) && getLogger().isInfoEnabled() )
{
getLogger().info( "Compiling " + sourceFiles.length + " " //
+ "source file" //
+ ( sourceFiles.length == 1 ? "" : "s" ) //
+ " to " + destinationDir.getAbsolutePath() );
}

String[] args = JavacCompiler.buildCompilerArguments( config, sourceFiles );

try
{
CompilerResult compilerResult = (CompilerResult) getInvoker().invoke( null, new Object[]{ args } );
return compilerResult;
}
catch ( Exception e )
{
throw new CompilerException( e.getMessage(), e );
}
}

private static class NonDelegatingClassLoader
extends URLClassLoader
{
Expand All @@ -115,6 +61,10 @@ public Class<?> loadClass( String name, boolean complete )
{
// Classes loaded inside CompilerInvoker that need to reach back to the caller
if ( name.contentEquals( CompilerResult.class.getName() )
|| name.contentEquals( InProcessCompiler.class.getName() )
|| name.contentEquals( CompilerConfiguration.class.getName() )
|| name.contentEquals( CompilerConfiguration.CompilerReuseStrategy.class.getName() )
|| name.contentEquals( CompilerException.class.getName() )
|| name.contentEquals( CompilerMessage.class.getName() )
|| name.contentEquals( CompilerMessage.Kind.class.getName() ) )
{
Expand All @@ -140,12 +90,9 @@ public Class<?> loadClass( String name, boolean complete )
}
}

private Method invokerMethod;

private Method getInvoker()
throws CompilerException
protected InProcessCompiler inProcessCompiler()
{
if ( invokerMethod == null )
if ( Thread.currentThread().getContextClassLoader().getResource("java/lang/module/ModuleReference.class") == null )
{
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
URL[] urls = ( (URLClassLoader) contextClassLoader ).getURLs();
Expand All @@ -154,14 +101,14 @@ private Method getInvoker()
{
loader = new NonDelegatingClassLoader( urls, contextClassLoader );
Class<?> clazz = Class.forName( CompilerInvoker.class.getName(), true, loader );
invokerMethod = clazz.getMethod( "compile", String[].class );
return ( InProcessCompiler ) clazz.newInstance();
}
catch ( Exception e )
{
throw new CompilerException( e.getMessage(), e );
throw new IllegalStateException( e );
}
}
return invokerMethod;
return new CompilerInvoker();
}

/**
Expand All @@ -170,57 +117,12 @@ private Method getInvoker()
* javac.jar instead of from the bootclasspath.
*/
public static class CompilerInvoker
extends JavaxToolsCompiler
{
private static class MessageListener
implements DiagnosticListener<JavaFileObject>
{
private final List<CompilerMessage> messages;

MessageListener( List<CompilerMessage> messages )
{
this.messages = messages;
}

public static CompilerMessage.Kind convertKind( Diagnostic<? extends JavaFileObject> diagnostic )
{
switch ( diagnostic.getKind() )
{
case ERROR:
return CompilerMessage.Kind.ERROR;
case WARNING:
return CompilerMessage.Kind.WARNING;
case MANDATORY_WARNING:
return CompilerMessage.Kind.MANDATORY_WARNING;
case NOTE:
return CompilerMessage.Kind.NOTE;
default:
return CompilerMessage.Kind.OTHER;
}
}

public void report( Diagnostic<? extends JavaFileObject> diagnostic )
{
CompilerMessage compilerMessage =
new CompilerMessage( diagnostic.getSource() == null ? null : diagnostic.getSource().getName(), //
convertKind( diagnostic ), //
(int) diagnostic.getLineNumber(), //
(int) diagnostic.getColumnNumber(), //
-1, //
-1,
// end pos line:column is hard to calculate
diagnostic.getMessage( Locale.getDefault() ) );
messages.add( compilerMessage );
}
}

public static CompilerResult compile( String[] args )
{
List<CompilerMessage> messages = new ArrayList<>();
ErrorProneCompiler compiler = //
ErrorProneCompiler.builder() //
.listenToDiagnostics( new MessageListener( messages ) ) //
.build();
return new CompilerResult( compiler.run( args ).isOK(), messages );
}
@Override
protected JavaCompiler newJavaCompiler()
{
return new ErrorProneJavaCompiler();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.Set;
import java.util.HashSet;

public class ShortSet {
public static void main (String[] args) {
Set<Short> s = new HashSet<>();
for (short i = 0; i < 100; i++) {
s.add(i);
s.remove(i - 1);
}
System.out.println(s.size());
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading