vancetang / reflections

Automatically exported from code.google.com/p/reflections
Do What The F*ck You Want To Public License
0 stars 0 forks source link

NullPointer in getUrlsForCurrentClasspath in ClasspathHelper #61

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In our Envirenment (Websphere 7) occurs a Nullpointer in 
getUrlsForCurrentClasspath() in the ClasspathHelper.

I don't know why but the Method getUrls() on the URLClassLoader returns 
sometimes null and so on the Collections.addAll method can not handle such a 
Null Value.

I am using the latest reflections-0.9.5-RC2.jar

I have modified the Method to prevent a NullPointer

    public static Set<URL> getUrlsForCurrentClasspath() {
        Set<URL> urls = Sets.newHashSet();

        //is URLClassLoader?
        ClassLoader loader = Utils.getContextClassLoader();
        while (loader != null) {
            if (loader instanceof URLClassLoader) {
                URL[] theUrls = ((URLClassLoader) loader).getURLs();
                if (theUrls != null) 
                    Collections.addAll(urls, theUrls);
            }
            loader = loader.getParent();
        }

        if (urls.isEmpty()) {
            //get from java.class.path
            String javaClassPath = System.getProperty("java.class.path");
            if (javaClassPath != null) {

                for (String path : javaClassPath.split(File.pathSeparator)) {
                    try {
                        urls.add(new File(path).toURI().toURL());
                    } catch (Exception e) {
                        throw new ReflectionsException("could not create url from " + path, e);
                    }
                }
            }
        }

        return ImmutableSet.copyOf(urls);
    }

Original issue reported on code.google.com by c.ko...@gmail.com on 18 Nov 2010 at 9:17

GoogleCodeExporter commented 9 years ago
fixed on trunk

Original comment by ronm...@gmail.com on 12 May 2011 at 7:49