You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by eo...@apache.org on 2020/01/03 09:51:38 UTC

[maven-checkstyle-plugin] 01/01: [MCHECKSTYLE-381] make call to checker.setClassLoader() optional.

This is an automated email from the ASF dual-hosted git repository.

eolivelli pushed a commit to branch MCHECKSTYLE-381
in repository https://gitbox.apache.org/repos/asf/maven-checkstyle-plugin.git

commit 125a659c6004a48284f0470a100ed58238d211d3
Author: Benjamin Marwell <bm...@gmail.com>
AuthorDate: Wed Dec 11 23:51:48 2019 +0100

    [MCHECKSTYLE-381] make call to checker.setClassLoader() optional.
    
      - try to call the method if it is available.
      - See also: https://github.com/checkstyle/checkstyle/issues/7190
    
    Signed-off-by: Benjamin Marwell <bm...@gmail.com>
---
 .../checkstyle/exec/DefaultCheckstyleExecutor.java | 127 +++++++++++----------
 1 file changed, 68 insertions(+), 59 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/checkstyle/exec/DefaultCheckstyleExecutor.java b/src/main/java/org/apache/maven/plugins/checkstyle/exec/DefaultCheckstyleExecutor.java
index 5afbd10..ce7f213 100644
--- a/src/main/java/org/apache/maven/plugins/checkstyle/exec/DefaultCheckstyleExecutor.java
+++ b/src/main/java/org/apache/maven/plugins/checkstyle/exec/DefaultCheckstyleExecutor.java
@@ -20,7 +20,6 @@ package org.apache.maven.plugins.checkstyle.exec;
  */
 
 import java.io.ByteArrayInputStream;
-import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -157,51 +156,7 @@ public class DefaultCheckstyleExecutor
                                     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() );
 
@@ -248,19 +203,6 @@ public class DefaultCheckstyleExecutor
 
         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();
@@ -316,6 +258,73 @@ public class DefaultCheckstyleExecutor
         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 )