Skip to content

Commit eae07f9

Browse files
bmarwelleolivelli
authored andcommitted
[MCHECKSTYLE-381] make call to checker.setClassLoader() optional.
- try to call the method if it is available. - See also: checkstyle/checkstyle#7190 Signed-off-by: Benjamin Marwell <[email protected]>
1 parent 26848dc commit eae07f9

File tree

1 file changed

+68
-59
lines changed

1 file changed

+68
-59
lines changed

src/main/java/org/apache/maven/plugins/checkstyle/exec/DefaultCheckstyleExecutor.java

Lines changed: 68 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
*/
2121

2222
import java.io.ByteArrayInputStream;
23-
import java.io.Closeable;
2423
import java.io.File;
2524
import java.io.FileInputStream;
2625
import java.io.IOException;
@@ -157,51 +156,7 @@ public CheckstyleResults executeCheckstyle( CheckstyleExecutorRequest request )
157156
testSourceDirectories );
158157
}
159158

160-
final List<URL> urls = new ArrayList<>( classPathStrings.size() );
161-
162-
for ( String path : classPathStrings )
163-
{
164-
try
165-
{
166-
urls.add( new File( path ).toURI().toURL() );
167-
}
168-
catch ( MalformedURLException e )
169-
{
170-
throw new CheckstyleExecutorException( e.getMessage(), e );
171-
}
172-
}
173-
174-
for ( String outputDirectoryString : outputDirectories )
175-
{
176-
try
177-
{
178-
if ( outputDirectoryString != null )
179-
{
180-
File outputDirectoryFile = new File( outputDirectoryString );
181-
if ( outputDirectoryFile.exists() )
182-
{
183-
URL outputDirectoryUrl = outputDirectoryFile.toURI().toURL();
184-
getLogger().debug( "Adding the outputDirectory " + outputDirectoryUrl.toString()
185-
+ " to the Checkstyle class path" );
186-
urls.add( outputDirectoryUrl );
187-
}
188-
}
189-
}
190-
catch ( MalformedURLException e )
191-
{
192-
throw new CheckstyleExecutorException( e.getMessage(), e );
193-
}
194-
}
195-
196-
URLClassLoader projectClassLoader = AccessController.doPrivileged( new PrivilegedAction<URLClassLoader>()
197-
{
198-
public URLClassLoader run()
199-
{
200-
return new URLClassLoader( urls.toArray( new URL[urls.size()] ), null );
201-
}
202-
} );
203-
204-
checker.setClassLoader( projectClassLoader );
159+
setUpCheckstyleClassloader( checker, classPathStrings, outputDirectories );
205160

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

@@ -248,19 +203,6 @@ public URLClassLoader run()
248203

249204
checker.destroy();
250205

251-
if ( projectClassLoader instanceof Closeable )
252-
{
253-
try
254-
{
255-
( ( Closeable ) projectClassLoader ).close();
256-
}
257-
catch ( IOException ex )
258-
{
259-
// Nothing we can do - and not detrimental to the build (save running out of file handles).
260-
getLogger().info( "Failed to close custom Classloader - this indicated a bug in the code.", ex );
261-
}
262-
}
263-
264206
if ( request.getStringOutputStream() != null )
265207
{
266208
String message = request.getStringOutputStream().toString().trim();
@@ -316,6 +258,73 @@ public URLClassLoader run()
316258
return checkerListener.getResults();
317259
}
318260

261+
private void setUpCheckstyleClassloader( Checker checker,
262+
List<String> classPathStrings,
263+
List<String> outputDirectories )
264+
throws CheckstyleExecutorException
265+
{
266+
final List<URL> urls = new ArrayList<>( classPathStrings.size() );
267+
268+
for ( String path : classPathStrings )
269+
{
270+
try
271+
{
272+
urls.add( new File( path ).toURI().toURL() );
273+
}
274+
catch ( MalformedURLException e )
275+
{
276+
throw new CheckstyleExecutorException( e.getMessage(), e );
277+
}
278+
}
279+
280+
for ( String outputDirectoryString : outputDirectories )
281+
{
282+
try
283+
{
284+
if ( outputDirectoryString != null )
285+
{
286+
File outputDirectoryFile = new File( outputDirectoryString );
287+
if ( outputDirectoryFile.exists() )
288+
{
289+
URL outputDirectoryUrl = outputDirectoryFile.toURI().toURL();
290+
getLogger().debug( "Adding the outputDirectory " + outputDirectoryUrl.toString()
291+
+ " to the Checkstyle class path" );
292+
urls.add( outputDirectoryUrl );
293+
}
294+
}
295+
}
296+
catch ( MalformedURLException e )
297+
{
298+
throw new CheckstyleExecutorException( e.getMessage(), e );
299+
}
300+
}
301+
302+
URLClassLoader projectClassLoader = AccessController.doPrivileged( new PrivilegedAction<URLClassLoader>()
303+
{
304+
public URLClassLoader run()
305+
{
306+
return new URLClassLoader( urls.toArray( new URL[0] ), null );
307+
}
308+
} );
309+
310+
/*
311+
* MCHECKSTYLE-381: More recent Checkstyle versions will drop the setClassLoader() method.
312+
* However, it was used before Checkstyle 8.25.
313+
*/
314+
try
315+
{
316+
checker.setClassLoader( projectClassLoader );
317+
}
318+
catch ( NoSuchMethodError ignored )
319+
{
320+
/*
321+
* The current checkstyle version does not support the method setClassLoader anymore.
322+
* This is expected. The method call is being retained for less recent versions of checkstyle.
323+
*/
324+
}
325+
326+
}
327+
319328
protected void addSourceDirectory( CheckstyleCheckerListener sinkListener, Collection<File> sourceDirectories,
320329
Collection<File> testSourceDirectories, List<Resource> resources,
321330
CheckstyleExecutorRequest request )

0 commit comments

Comments
 (0)