You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2019/11/12 09:52:21 UTC

[groovy] branch GROOVY_2_5_X updated: GROOVY-9257: ZipException, FNFE -> IOException, InvalidPathException

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

paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new ef51b13  GROOVY-9257: ZipException, FNFE -> IOException, InvalidPathException
ef51b13 is described below

commit ef51b13745525f026b7f59d9dc7fb7afa65ec7bd
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Oct 31 12:21:09 2019 -0500

    GROOVY-9257: ZipException, FNFE -> IOException, InvalidPathException
---
 .../tools/shell/util/PackageHelperImpl.groovy      | 119 ++++++++-------------
 1 file changed, 46 insertions(+), 73 deletions(-)

diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/PackageHelperImpl.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/PackageHelperImpl.groovy
index 17d0e09..afcb634 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/PackageHelperImpl.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/PackageHelperImpl.groovy
@@ -18,50 +18,53 @@
  */
 package org.codehaus.groovy.tools.shell.util
 
+import groovy.transform.AutoFinal
 import groovy.transform.CompileDynamic
 import groovy.transform.CompileStatic
 
+import java.nio.file.InvalidPathException
 import java.util.jar.JarEntry
 import java.util.jar.JarFile
 import java.util.prefs.PreferenceChangeEvent
 import java.util.prefs.PreferenceChangeListener
 import java.util.regex.Pattern
-import java.util.zip.ZipException
 
 /**
  * Helper class that crawls all items of the classpath for packages.
  * Retrieves from those sources the list of subpackages and classes on demand.
  */
-@CompileStatic
+@AutoFinal @CompileStatic
 class PackageHelperImpl implements PreferenceChangeListener, PackageHelper {
 
-    // Pattern for regular Classnames
+    protected static final Logger LOG = Logger.create(PackageHelperImpl)
+
+    /** Pattern for regular class names. */
     public static final Pattern NAME_PATTERN = ~('^[A-Z][^.\$_]+\$')
 
     private static final String CLASS_SUFFIX = '.class'
-    protected static final Logger LOG = Logger.create(PackageHelperImpl)
 
-    Map<String, CachedPackage> rootPackages = null
+    Map<String, CachedPackage> rootPackages
     final ClassLoader groovyClassLoader
 
-    PackageHelperImpl(final ClassLoader groovyClassLoader=null) {
+    PackageHelperImpl(ClassLoader groovyClassLoader = null) {
         this.groovyClassLoader = groovyClassLoader
         initializePackages()
         Preferences.addChangeListener(this)
     }
 
+    @Override
     void reset() {
         initializePackages()
     }
 
     private void initializePackages() {
-        if (! Boolean.valueOf(Preferences.get(IMPORT_COMPLETION_PREFERENCE_KEY))) {
+        if (!Boolean.valueOf(Preferences.get(IMPORT_COMPLETION_PREFERENCE_KEY))) {
             rootPackages = getPackages(this.groovyClassLoader)
         }
     }
 
     @Override
-    void preferenceChange(final PreferenceChangeEvent evt) {
+    void preferenceChange(PreferenceChangeEvent evt) {
         if (evt.key == IMPORT_COMPLETION_PREFERENCE_KEY) {
             if (Boolean.valueOf(evt.getNewValue())) {
                 rootPackages = null
@@ -71,9 +74,9 @@ class PackageHelperImpl implements PreferenceChangeListener, PackageHelper {
         }
     }
 
-    private static Map<String, CachedPackage> getPackages(final ClassLoader groovyClassLoader) throws IOException {
+    private static Map<String, CachedPackage> getPackages(ClassLoader groovyClassLoader) throws IOException {
         Map<String, CachedPackage> rootPackages = new HashMap()
-        Set<URL> urls = new HashSet<URL>()
+        Set<URL> urls = new HashSet<>()
 
         // classes in CLASSPATH
         for (ClassLoader loader = groovyClassLoader; loader != null; loader = loader.parent) {
@@ -86,11 +89,11 @@ class PackageHelperImpl implements PreferenceChangeListener, PackageHelper {
         }
 
         // System classes
-        Class[] systemClasses = [String, javax.swing.JFrame, GroovyObject] as Class[]
+        Class<?>[] systemClasses = [String, javax.swing.JFrame, GroovyObject] as Class[]
         boolean jigsaw = false
         systemClasses.each { Class systemClass ->
             // normal slash even in Windows
-            String classfileName = systemClass.name.replace('.', '/') + '.class'
+            String classfileName = systemClass.name.replace('.', '/') + CLASS_SUFFIX
             URL classURL = systemClass.getResource(classfileName)
             if (classURL == null) {
                 // this seems to work on Windows better than the earlier approach
@@ -136,10 +139,9 @@ class PackageHelperImpl implements PreferenceChangeListener, PackageHelper {
     }
 
     /**
-     * This method returns packages or classes listed from Jigsaw modules.
-     * It makes use of a GroovyShell in order to avoid a hard dependency
-     * to JDK 7+ when building the Groovysh module (uses nio2)
-     * @return
+     * Returns packages or classes listed from Jigsaw modules. It makes use of a
+     * GroovyShell in order to avoid a hard dependency to JDK 7+ when building
+     * the Groovysh module (uses nio2).
      */
     private static Set<String> getPackagesAndClassesFromJigsaw(URL jigsawURL, Closure<Boolean> predicate) {
         def shell = new GroovyShell()
@@ -196,8 +198,7 @@ Files.walkFileTree(fs.getPath('modules'),
         jigsawPackages
     }
 
-    static mergeNewPackages(final Collection<String> packageNames, final URL url,
-                            final Map<String, CachedPackage> rootPackages) {
+    static mergeNewPackages(Collection<String> packageNames, URL url, Map<String, CachedPackage> rootPackages) {
         StringTokenizer tokenizer
         packageNames.each { String packname ->
             tokenizer = new StringTokenizer(packname, '.')
@@ -235,46 +236,34 @@ Files.walkFileTree(fs.getPath('modules'),
     }
 
     /**
-     * Returns all packagenames found at URL, accepts jar files and folders
-     * @param url
-     * @return
+     * Returns all package names found at URL; accepts jar files and folders.
      */
-    static Collection<String> getPackageNames(final URL url) {
-        //log.debug(url)
-        String path = URLDecoder.decode(url.getFile(), 'UTF-8')
-        File urlfile = new File(path)
-        if (urlfile.isDirectory()) {
-            Set<String> packnames = new HashSet<String>()
-            collectPackageNamesFromFolderRecursive(urlfile, '', packnames)
-            return packnames
+    static Collection<String> getPackageNames(URL url) {
+        File urlFile = new File(URLDecoder.decode(url.file, 'UTF-8'))
+
+        if (urlFile.isDirectory()) {
+            return new HashSet<>().tap {
+                collectPackageNamesFromFolderRecursive(urlFile, '', it)
+            }
         }
 
-        if (urlfile.path.endsWith('.jar')) {
+        if (urlFile.path.endsWith('.jar')) {
             try {
-                JarFile jf = new JarFile(urlfile)
-                return getPackageNamesFromJar(jf)
-            } catch(ZipException ze) {
-                if (LOG.debugEnabled) {
-                    ze.printStackTrace()
-                }
-                LOG.debug("Error opening zipfile : '${url.getFile()}',  ${ze.toString()}")
-            } catch (FileNotFoundException fnfe) {
-                LOG.debug("Error opening file : '${url.getFile()}',  ${fnfe.toString()}")
+                JarFile jarFile = new JarFile(urlFile)
+                return getPackageNamesFromJar(jarFile)
+            } catch (IOException | InvalidPathException e) {
+                if (LOG.isDebugEnabled()) e.printStackTrace()
+                LOG.warn("Error opening jar file : '${url.file}' : ${e.toString()}")
             }
         }
+
         return []
     }
 
     /**
      * Crawls a folder, iterates over subfolders, looking for class files.
-     * @param directory
-     * @param prefix
-     * @param packnames
-     * @return
      */
-    static Collection<String> collectPackageNamesFromFolderRecursive(final File directory, final String prefix,
-                                                                     final Set<String> packnames) {
-        //log.debug(directory)
+    static Collection<String> collectPackageNamesFromFolderRecursive(File directory, String prefix, Set<String> packnames) {
         File[] files = directory.listFiles()
         boolean packageAdded = false
 
@@ -296,9 +285,8 @@ Files.walkFileTree(fs.getPath('modules'),
         }
     }
 
-
-    static Collection<String> getPackageNamesFromJar(final JarFile jf) {
-        Set<String> packnames = new HashSet<String>()
+    static Collection<String> getPackageNamesFromJar(JarFile jf) {
+        Set<String> packnames = new HashSet<>()
         for (Enumeration e = jf.entries(); e.hasMoreElements();) {
             JarEntry entry = (JarEntry) e.nextElement()
 
@@ -322,27 +310,15 @@ Files.walkFileTree(fs.getPath('modules'),
         return packnames
     }
 
-    // following block does not work, because URLClassLoader.packages only ever returns SystemPackages
-    /*static Collection<String> getPackageNames(URL url) {
-        URLClassLoader urlLoader = new URLClassLoader([url] as URL[])
-        //log.debug(urlLoader.packages.getClass())
-
-        urlLoader.getPackages().collect {Package pack ->
-            pack.name
-        }
-    }*/
-
     /**
-     * returns the names of Classes and direct subpackages contained in a package
-     * @param packagename
-     * @return
+     * Returns the names of Classes and direct subpackages contained in a package.
      */
-    @CompileStatic
-    Set<String> getContents(final String packagename) {
-        if (! rootPackages) {
+    @Override
+    Set<String> getContents(String packagename) {
+        if (!rootPackages) {
             return [] as Set
         }
-        if (! packagename) {
+        if (!packagename) {
             return rootPackages.collect { String key, CachedPackage v -> key } as Set
         }
         String sanitizedPackageName
@@ -385,13 +361,10 @@ Files.walkFileTree(fs.getPath('modules'),
     }
 
     /**
-     * Copied from JLine 1.0 ClassNameCompletor
-     * @param urls
-     * @param packagename
-     * @return
+     * Copied from JLine 1.0 ClassNameCompletor.
      */
-    static Set<String> getClassnames(final Set<URL> urls, final String packagename) {
-        Set<String> classes = new TreeSet<String>()
+    static Set<String> getClassnames(Set<URL> urls, String packagename) {
+        Set<String> classes = new TreeSet<>()
         // normal slash even in Windows
         String pathname = packagename.replace('.', '/')
         for (Iterator<URL> it = urls.iterator(); it.hasNext();) {
@@ -477,7 +450,7 @@ class CachedPackage {
     Set<URL> sources
 
     CachedPackage(String name, Set<URL> sources) {
-        this.sources = sources
         this.name = name
+        this.sources = sources
     }
 }