Skip to content

Commit

Permalink
[MCHECKSTYLE-381] make call to checker.setClassLoader() optional.
Browse files Browse the repository at this point in the history
  - try to call the method if it is available.
  - See also: checkstyle/checkstyle#7190

Signed-off-by: Benjamin Marwell <[email protected]>
  • Loading branch information
bmarwell authored and eolivelli committed Jan 3, 2020
1 parent 26848dc commit eae07f9
Showing 1 changed file with 68 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/

import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -157,51 +156,7 @@ public CheckstyleResults executeCheckstyle( CheckstyleExecutorRequest request )
testSourceDirectories );
}

final List<URL> urls = new ArrayList<>( classPathStrings.size() );

for ( String path : classPathStrings )
{
try
{
urls.add( new File( path ).toURI().toURL() );
}
catch ( MalformedURLException e )
{
throw new CheckstyleExecutorException( e.getMessage(), e );
}
}

for ( String outputDirectoryString : outputDirectories )
{
try
{
if ( outputDirectoryString != null )
{
File outputDirectoryFile = new File( outputDirectoryString );
if ( outputDirectoryFile.exists() )
{
URL outputDirectoryUrl = outputDirectoryFile.toURI().toURL();
getLogger().debug( "Adding the outputDirectory " + outputDirectoryUrl.toString()
+ " to the Checkstyle class path" );
urls.add( outputDirectoryUrl );
}
}
}
catch ( MalformedURLException e )
{
throw new CheckstyleExecutorException( e.getMessage(), e );
}
}

URLClassLoader projectClassLoader = AccessController.doPrivileged( new PrivilegedAction<URLClassLoader>()
{
public URLClassLoader run()
{
return new URLClassLoader( urls.toArray( new URL[urls.size()] ), null );
}
} );

checker.setClassLoader( projectClassLoader );
setUpCheckstyleClassloader( checker, classPathStrings, outputDirectories );

checker.setModuleClassLoader( Thread.currentThread().getContextClassLoader() );

Expand Down Expand Up @@ -248,19 +203,6 @@ public URLClassLoader run()

checker.destroy();

if ( projectClassLoader instanceof Closeable )
{
try
{
( ( Closeable ) projectClassLoader ).close();
}
catch ( IOException ex )
{
// Nothing we can do - and not detrimental to the build (save running out of file handles).
getLogger().info( "Failed to close custom Classloader - this indicated a bug in the code.", ex );
}
}

if ( request.getStringOutputStream() != null )
{
String message = request.getStringOutputStream().toString().trim();
Expand Down Expand Up @@ -316,6 +258,73 @@ public URLClassLoader run()
return checkerListener.getResults();
}

private void setUpCheckstyleClassloader( Checker checker,
List<String> classPathStrings,
List<String> outputDirectories )
throws CheckstyleExecutorException
{
final List<URL> urls = new ArrayList<>( classPathStrings.size() );

for ( String path : classPathStrings )
{
try
{
urls.add( new File( path ).toURI().toURL() );
}
catch ( MalformedURLException e )
{
throw new CheckstyleExecutorException( e.getMessage(), e );
}
}

for ( String outputDirectoryString : outputDirectories )
{
try
{
if ( outputDirectoryString != null )
{
File outputDirectoryFile = new File( outputDirectoryString );
if ( outputDirectoryFile.exists() )
{
URL outputDirectoryUrl = outputDirectoryFile.toURI().toURL();
getLogger().debug( "Adding the outputDirectory " + outputDirectoryUrl.toString()
+ " to the Checkstyle class path" );
urls.add( outputDirectoryUrl );
}
}
}
catch ( MalformedURLException e )
{
throw new CheckstyleExecutorException( e.getMessage(), e );
}
}

URLClassLoader projectClassLoader = AccessController.doPrivileged( new PrivilegedAction<URLClassLoader>()
{
public URLClassLoader run()
{
return new URLClassLoader( urls.toArray( new URL[0] ), null );
}
} );

/*
* MCHECKSTYLE-381: More recent Checkstyle versions will drop the setClassLoader() method.
* However, it was used before Checkstyle 8.25.
*/
try
{
checker.setClassLoader( projectClassLoader );
}
catch ( NoSuchMethodError ignored )
{
/*
* The current checkstyle version does not support the method setClassLoader anymore.
* This is expected. The method call is being retained for less recent versions of checkstyle.
*/
}

}

protected void addSourceDirectory( CheckstyleCheckerListener sinkListener, Collection<File> sourceDirectories,
Collection<File> testSourceDirectories, List<Resource> resources,
CheckstyleExecutorRequest request )
Expand Down

0 comments on commit eae07f9

Please sign in to comment.