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:com.alibaba.rocketmq.filtersrv.filter.DynaCode.java

private static String extractClasspath(ClassLoader cl) {
    StringBuffer buf = new StringBuffer();
    while (cl != null) {
        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);
                }//from  w  w  w  .  j  a v a  2 s.  c o  m
                String s = urls[i].getFile();
                try {
                    s = URLDecoder.decode(s, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    continue;
                }
                File f = new File(s);
                buf.append(f.getAbsolutePath());
            }
        }
        cl = cl.getParent();
    }
    return buf.toString();
}

From source file:ThreadDemo.java

public void run() {

    // returns the context ClassLoader for this Thread
    ClassLoader c = getContextClassLoader();

    // sets the context ClassLoader for this Thread
    setContextClassLoader(c);//from   w w  w  .  ja v  a  2s .  c  om
    System.out.println("Class = " + c.getClass());
    System.out.println("Parent = " + c.getParent());
}

From source file:Main.java

ThreadDemo() {

    Thread t = new Thread(this);
    t.start();// w w w .ja va  2s  . co m
    ClassLoader c = t.getContextClassLoader();
    // sets the context ClassLoader for this Thread
    t.setContextClassLoader(c);
    System.out.println("Class = " + c.getClass());
    System.out.println("Parent = " + c.getParent());
}

From source file:com.laidians.utils.ClassUtils.java

/**
 * Check whether the given class is cache-safe in the given context,
 * i.e. whether it is loaded by the given ClassLoader or a parent of it.
 * @param clazz the class to analyze// ww w  . j a va  2  s  .c o  m
 * @param classLoader the ClassLoader to potentially cache metadata in
 */
public static boolean isCacheSafe(Class<?> clazz, ClassLoader classLoader) {
    Assert.notNull(clazz, "Class must not be null");
    ClassLoader target = clazz.getClassLoader();
    if (target == null) {
        return false;
    }
    ClassLoader cur = classLoader;
    if (cur == target) {
        return true;
    }
    while (cur != null) {
        cur = cur.getParent();
        if (cur == target) {
            return true;
        }
    }
    return false;
}

From source file:com.legstar.pli2cob.task.PLIStructureToCobolTask.java

/**
 * Help debug class loading issues.//from  w w  w .  j  ava  2 s. co  m
 * @param startCl a class loader to start from
 */
private void debugLoaderChain(final ClassLoader startCl) {
    _log.debug("--- Class loader chain starts");
    ClassLoader cl = startCl;
    while (cl != null) {
        _log.debug(cl);
        cl = cl.getParent();
    }
    _log.debug("--- chain ends");
}

From source file:com.dianping.resource.io.util.ClassUtils.java

/**
 * Check whether the given class is cache-safe in the given context,
 * i.e. whether it is loaded by the given ClassLoader or a parent of it.
 * @param clazz the class to analyze/*from  w w w .j a va 2  s .  com*/
 * @param classLoader the ClassLoader to potentially cache metadata in
 */
public static boolean isCacheSafe(Class<?> clazz, ClassLoader classLoader) {
    Assert.notNull(clazz, "Class must not be null");
    ClassLoader target = clazz.getClassLoader();
    if (target == null) {
        return true;
    }
    ClassLoader cur = classLoader;
    if (cur == target) {
        return true;
    }
    while (cur != null) {
        cur = cur.getParent();
        if (cur == target) {
            return true;
        }
    }
    return false;
}

From source file:com.freetmp.common.util.ClassUtils.java

public static boolean isCacheSafe(Class<?> clazz, ClassLoader classLoader) {
    Assert.notNull(clazz, "Class must not be null");
    try {//from w  ww. j  a v a2  s .  c  o  m
        ClassLoader target = clazz.getClassLoader();
        if (target == null) {
            return true;
        }
        ClassLoader cur = classLoader;
        if (cur == target) {
            return true;
        }
        while (cur != null) {
            cur = cur.getParent();
            if (cur == target) {
                return true;
            }
        }
        return false;
    } catch (SecurityException ex) {
        // Probably from the system ClassLoader - let's consider it safe.
        return true;
    }
}

From source file:org.crazydog.util.spring.ClassUtils.java

/**
 * Check whether the given class is cache-safe in the given context,
 * i.e. whether it is loaded by the given ClassLoader or a parent of it.
 * @param clazz the class to analyze//from  w  w w.j  a va2  s.c  o m
 * @param classLoader the ClassLoader to potentially cache metadata in
 */
public static boolean isCacheSafe(Class<?> clazz, ClassLoader classLoader) {
    org.springframework.util.Assert.notNull(clazz, "Class must not be null");
    try {
        ClassLoader target = clazz.getClassLoader();
        if (target == null) {
            return true;
        }
        ClassLoader cur = classLoader;
        if (cur == target) {
            return true;
        }
        while (cur != null) {
            cur = cur.getParent();
            if (cur == target) {
                return true;
            }
        }
        return false;
    } catch (SecurityException ex) {
        // Probably from the system ClassLoader - let's consider it safe.
        return true;
    }
}

From source file:com.videobox.web.util.dwr.AutoAnnotationDiscoveryContainer.java

private URL findResource(ClassLoader loader, String pkgPath) {
    if (loader == null) {
        return null;
    }//from  w  ww  .  j  av  a  2 s  .  c  o  m
    if (!(loader instanceof URLClassLoader)) {
        return findResource(loader.getParent(), pkgPath);
    }
    URLClassLoader ucl = (URLClassLoader) loader;
    URL url = ucl.getResource(pkgPath);
    if (url == null) {
        logger.warn("Not found in " + ucl + " -> " + Arrays.asList(ucl.getURLs()));
        return findResource(loader.getParent(), pkgPath);
    }
    return url;
}

From source file:io.cloudslang.dependency.impl.services.DependencyServiceImpl.java

@PostConstruct
private void initMaven() throws ClassNotFoundException, NoSuchMethodException, MalformedURLException {
    ClassLoader parentClassLoader = DependencyServiceImpl.class.getClassLoader();
    while (parentClassLoader.getParent() != null) {
        parentClassLoader = parentClassLoader.getParent();
    }/*from  ww w. jav a 2  s. c  o  m*/

    if (isMavenConfigured()) {
        File libDir = new File(mavenHome, "boot");
        if (libDir.exists()) {
            URL[] mavenJarUrls = getUrls(libDir);

            mavenClassLoader = new URLClassLoader(mavenJarUrls, parentClassLoader);
            MAVEN_EXECUTE_METHOD = Class.forName(MAVEN_LAUNCHER_CLASS_NAME, true, mavenClassLoader)
                    .getMethod(MAVEN_LANUCHER_METHOD_NAME, String[].class);
            initMavenLogs();
        }
    }
}