|
20 | 20 | */ |
21 | 21 |
|
22 | 22 | import java.io.ByteArrayInputStream; |
23 | | -import java.io.Closeable; |
24 | 23 | import java.io.File; |
25 | 24 | import java.io.FileInputStream; |
26 | 25 | import java.io.IOException; |
@@ -157,51 +156,7 @@ public CheckstyleResults executeCheckstyle( CheckstyleExecutorRequest request ) |
157 | 156 | testSourceDirectories ); |
158 | 157 | } |
159 | 158 |
|
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 ); |
205 | 160 |
|
206 | 161 | checker.setModuleClassLoader( Thread.currentThread().getContextClassLoader() ); |
207 | 162 |
|
@@ -248,19 +203,6 @@ public URLClassLoader run() |
248 | 203 |
|
249 | 204 | checker.destroy(); |
250 | 205 |
|
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 | | - |
264 | 206 | if ( request.getStringOutputStream() != null ) |
265 | 207 | { |
266 | 208 | String message = request.getStringOutputStream().toString().trim(); |
@@ -316,6 +258,73 @@ public URLClassLoader run() |
316 | 258 | return checkerListener.getResults(); |
317 | 259 | } |
318 | 260 |
|
| 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 | + |
319 | 328 | protected void addSourceDirectory( CheckstyleCheckerListener sinkListener, Collection<File> sourceDirectories, |
320 | 329 | Collection<File> testSourceDirectories, List<Resource> resources, |
321 | 330 | CheckstyleExecutorRequest request ) |
|
0 commit comments