Example usage for java.lang ClassLoader getParent

List of usage examples for java.lang ClassLoader getParent

Introduction

In this page you can find the example usage for java.lang ClassLoader getParent.

Prototype

@CallerSensitive
public final ClassLoader getParent() 

Source Link

Document

Returns the parent class loader for delegation.

Usage

From source file:org.springframework.beans.CachedIntrospectionResults.java

/**
 * Check whether the given ClassLoader is underneath the given parent,
 * that is, whether the parent is within the candidate's hierarchy.
 * @param candidate the candidate ClassLoader to check
 * @param parent the parent ClassLoader to check for
 *//*from   w w  w  .j  ava 2  s.  com*/
private static boolean isUnderneathClassLoader(@Nullable ClassLoader candidate, @Nullable ClassLoader parent) {
    if (candidate == parent) {
        return true;
    }
    if (candidate == null) {
        return false;
    }
    ClassLoader classLoaderToCheck = candidate;
    while (classLoaderToCheck != null) {
        classLoaderToCheck = classLoaderToCheck.getParent();
        if (classLoaderToCheck == parent) {
            return true;
        }
    }
    return false;
}

From source file:org.springframework.boot.devtools.restart.server.RestartServer.java

private Set<URL> getClassLoaderUrls() {
    Set<URL> urls = new LinkedHashSet<URL>();
    ClassLoader classLoader = this.classLoader;
    while (classLoader != null) {
        if (classLoader instanceof URLClassLoader) {
            for (URL url : ((URLClassLoader) classLoader).getURLs()) {
                urls.add(url);/*from   w  ww.ja v a  2 s .c  om*/
            }
        }
        classLoader = classLoader.getParent();
    }
    return urls;

}

From source file:org.springframework.cloud.function.deployer.ApplicationBootstrap.java

private URLClassLoader createClassLoader(Class<?> mainClass) {
    URL[] urls = findClassPath(mainClass);
    if (urls.length == 1) {
        URL[] classpath = extractClasspath(urls[0]);
        if (classpath != null) {
            urls = classpath;// ww w . jav a 2s .c o m
        }
    }
    List<URL> child = new ArrayList<>();
    List<URL> parent = new ArrayList<>();
    for (URL url : urls) {
        child.add(url);
    }
    for (URL url : urls) {
        if (isRoot(StringUtils.getFilename(clean(url.toString())))) {
            parent.add(url);
            child.remove(url);
        }
    }
    logger.debug("Parent: " + parent);
    logger.debug("Child: " + child);
    ClassLoader base = getClass().getClassLoader();
    if (!parent.isEmpty()) {
        base = new URLClassLoader(parent.toArray(new URL[0]), base.getParent());
    }
    return new URLClassLoader(child.toArray(new URL[0]), base);
}

From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java

/**
 * Search all {@link URLClassLoader} URLs for jar file references and add them to the
 * given set of resources in the form of pointers to the root of the jar file content.
 * @param classLoader the ClassLoader to search (including its ancestors)
 * @param result the set of resources to add jar roots to
 * @since 4.1.1/*from   w  w  w . ja va  2s.  c o  m*/
 */
protected void addAllClassLoaderJarRoots(@Nullable ClassLoader classLoader, Set<Resource> result) {
    if (classLoader instanceof URLClassLoader) {
        try {
            for (URL url : ((URLClassLoader) classLoader).getURLs()) {
                try {
                    UrlResource jarResource = new UrlResource(
                            ResourceUtils.JAR_URL_PREFIX + url + ResourceUtils.JAR_URL_SEPARATOR);
                    if (jarResource.exists()) {
                        result.add(jarResource);
                    }
                } catch (MalformedURLException ex) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Cannot search for matching files underneath [" + url
                                + "] because it cannot be converted to a valid 'jar:' URL: " + ex.getMessage());
                    }
                }
            }
        } catch (Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Cannot introspect jar files since ClassLoader [" + classLoader
                        + "] does not support 'getURLs()': " + ex);
            }
        }
    }

    if (classLoader == ClassLoader.getSystemClassLoader()) {
        // "java.class.path" manifest evaluation...
        addClassPathManifestEntries(result);
    }

    if (classLoader != null) {
        try {
            // Hierarchy traversal...
            addAllClassLoaderJarRoots(classLoader.getParent(), result);
        } catch (Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Cannot introspect jar files in parent ClassLoader since [" + classLoader
                        + "] does not support 'getParent()': " + ex);
            }
        }
    }
}

From source file:org.springframework.instrument.classloading.InstrumentableClassLoaderFactoryBean.java

public void afterPropertiesSet() {
    if (!StringUtils.hasText(this.className)) {
        throw new IllegalArgumentException("className is required");
    }/*from  w  ww .j  a v a  2  s  . c o m*/

    ClassLoader loader = ClassUtils.getDefaultClassLoader();

    // parse class loading hierarchy
    for (Class clazz = loader.getClass(); loader != null
            && this.classLoader == null; loader = loader.getParent()) {
        // check class itself
        if (analyzeClasses(loader, clazz)) {
            return;
        }
        // check interfaces
        if (analyzeClasses(loader, clazz.getInterfaces())) {
            return;
        }
        // check superclasses
        for (Class superClass = clazz.getSuperclass(); superClass != Object.class
                && superClass != null; superClass = clazz.getSuperclass())
            if (analyzeClasses(loader, superClass)) {
                return;
            }
    }

    throw new IllegalArgumentException(this.className + " was not found in the current classloader hierarchy - "
            + "see docs on how to use instrumented classloaders inside various environments");
}

From source file:org.squidy.common.dynamiccode.DynamicCodeClassLoader.java

/**
 * Extracts a classpath string from a given class loader. Recognizes only URLClassLoader.
 *//*from ww  w .ja v a2s  .  c  om*/
private static String extractClasspath(ClassLoader cl) {
    StringBuffer buf = new StringBuffer();

    while (cl != null) {

        if (LOG.isDebugEnabled()) {
            LOG.debug("Trying to extract classpath of class loader: " + cl.getClass().getName());
        }

        if (cl instanceof URLClassLoader) {
            URL urls[] = ((URLClassLoader) cl).getURLs();
            for (int i = 0; i < urls.length; i++) {
                if (buf.length() > 0) {
                    buf.append(File.pathSeparatorChar);
                }

                String path = urls[i].getFile();
                if (path.startsWith("/C:/") || path.startsWith("/c:/")) {
                    path = path.substring(1, path.length());
                    path = path.replace('/', '\\');
                    path = path.replace("%20", " ");
                }
                buf.append(path);
            }
        }
        cl = cl.getParent();
    }

    return buf.toString();
}

From source file:org.tinygroup.jspengine.compiler.TldLocationsCache.java

private void scanJars() throws Exception {

    ClassLoader webappLoader = Thread.currentThread().getContextClassLoader();
    ClassLoader loader = webappLoader;

    // START Glassfish 747
    if (localTldsProcessed) {
        if (loader != null) {
            loader = loader.getParent();
        }//w  ww  . ja  v a 2s  . c  o  m
    }
    // END GlassFish 747

    while (loader != null) {
        if (loader instanceof URLClassLoader) {
            boolean isLocal = (loader == webappLoader);
            URL[] urls = ((URLClassLoader) loader).getURLs();
            for (int i = 0; i < urls.length; i++) {
                URLConnection conn = urls[i].openConnection();
                if (conn instanceof JarURLConnection) {
                    if (needScanJar(((JarURLConnection) conn).getJarFile().getName(), isLocal)) {
                        scanJar((JarURLConnection) conn, true);
                    }
                } else {
                    String urlStr = urls[i].toString();
                    if (urlStr.startsWith(FILE_PROTOCOL) && urlStr.endsWith(JAR_FILE_SUFFIX)
                            && needScanJar(urlStr, isLocal)) {
                        URL jarURL = new URL("jar:" + urlStr + "!/");
                        scanJar((JarURLConnection) jarURL.openConnection(), true);
                    }
                }
            }
        }

        loader = loader.getParent();
    }
}

From source file:rapture.dp.DefaultDecisionProcessExecutor.java

private static String dumpClasspath(ClassLoader loader) {
    StringBuilder sb = new StringBuilder();

    sb.append("Classloader ").append(loader).append(":");

    if (loader instanceof URLClassLoader) {
        URLClassLoader ucl = (URLClassLoader) loader;
        sb.append("\n").append(Arrays.toString(ucl.getURLs()));
    } else/*from w  ww.j a  v  a2  s  .c o  m*/
        sb.append("\n(cannot display components as not a URLClassLoader)");

    if (loader.getParent() != null)
        sb.append(dumpClasspath(loader.getParent()));
    return sb.toString();
}

From source file:ru.runa.notifier.util.ClassLoaderUtil.java

public static URL getAsURL(String resourceName, Class<?> callingClass) {
    Preconditions.checkNotNull(resourceName, "resourceName");
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL url = null;/*from   w w w.  ja  v  a2s .c  om*/
    while ((loader != null) && (url == null)) {
        url = loader.getResource(resourceName);
        loader = loader.getParent();
    }
    if (url == null) {
        loader = extensionClassLoader;
        url = loader.getResource(resourceName);
    }
    if (url == null) {
        loader = callingClass.getClassLoader();
        if (loader != null) {
            url = loader.getResource(resourceName);
        }
    }
    if (url == null) {
        url = callingClass.getResource(resourceName);
    }
    if (url == null && resourceName.length() > 0 && resourceName.charAt(0) != '/') {
        return getAsURL('/' + resourceName, callingClass);
    }
    return url;
}

From source file:ru.runa.wfe.commons.ClassLoaderUtil.java

public static URL getAsURL(String resourceName, Class<?> callingClass) {
    Preconditions.checkNotNull(resourceName, "resourceName");
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL url = null;// w w w .j av a2s . co m
    while (loader != null && url == null) {
        url = loader.getResource(resourceName);
        loader = loader.getParent();
    }
    if (url == null) {
        loader = extensionClassLoader;
        url = loader.getResource(resourceName);
    }
    if (url == null) {
        loader = callingClass.getClassLoader();
        if (loader != null) {
            url = loader.getResource(resourceName);
        }
    }
    if (url == null) {
        url = callingClass.getResource(resourceName);
    }
    if (url == null && resourceName.length() > 0 && resourceName.charAt(0) != '/') {
        return getAsURL('/' + resourceName, callingClass);
    }
    return url;
}