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:Main.java

public static boolean overrideClassLoader(ClassLoader cl, File dex, File opt) {
    try {//from w w  w.j  av a2s.c om
        ClassLoader bootstrap = cl.getParent();
        Field fPathList = BaseDexClassLoader.class.getDeclaredField("pathList");
        fPathList.setAccessible(true);
        Object pathList = fPathList.get(cl);
        Class cDexPathList = bootstrap.loadClass("dalvik.system.DexPathList");
        Field fDexElements = cDexPathList.getDeclaredField("dexElements");
        fDexElements.setAccessible(true);
        Object dexElements = fDexElements.get(pathList);
        DexClassLoader cl2 = new DexClassLoader(dex.getAbsolutePath(), opt.getAbsolutePath(), null, bootstrap);
        Object pathList2 = fPathList.get(cl2);
        Object dexElements2 = fDexElements.get(pathList2);
        Object element2 = Array.get(dexElements2, 0);
        int n = Array.getLength(dexElements) + 1;
        Object newDexElements = Array.newInstance(fDexElements.getType().getComponentType(), n);
        Array.set(newDexElements, 0, element2);
        for (int i = 0; i < n - 1; i++) {
            Object element = Array.get(dexElements, i);
            Array.set(newDexElements, i + 1, element);
        }
        fDexElements.set(pathList, newDexElements);
        return true;
    } catch (Exception e) {
        Log.e("lcast", "fail to override classloader " + cl + " with " + dex, e);
        return false;
    }
}

From source file:com.retroduction.carma.utilities.ClassLoaderInfo.java

public static void printLoader(Class<?> clazz) {
    log.info("Class: " + clazz);
    ClassLoader cl = clazz.getClassLoader();
    log.info("Loader: " + cl);
    StringBuffer s = new StringBuffer(" ");
    while ((cl = cl.getParent()) != null) {
        log.info(s + "" + cl);
        s.append("  ");
    }/* w ww .  ja  va2  s. co m*/
}

From source file:Main.java

final public static boolean equals(ClassLoader cl, final ClassLoader other) {
    while (cl != null) {
        if (cl == other) {
            return true;
        }//from   w w w. j  av  a 2 s . com
        cl = cl.getParent();
    }
    return false;
}

From source file:ClassLoaderUtils.java

/**
 * Prints the classloader hierarchy from a given classloader - useful for debugging.
 *///  w w  w. jav a2s .com
public static void printClassLoader(ClassLoader cl) {
    System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")");

    if (cl != null) {
        printClassLoader(cl.getParent());
    }
}

From source file:com.stratuscom.harvester.Utils.java

public static void logClassLoaderHierarchy(Logger log, Level level, ClassLoader loader) {
    log.log(level, MessageNames.CLASSLOADER_IS, new Object[] { null, loader });
    ClassLoader parent = loader.getParent();
    while (parent != null) {
        log.log(level, MessageNames.PARENT_CLASS_LOADER_IS, new Object[] { parent });
        try {//ww w  . j a  va  2  s  .  c  o  m
            parent = parent.getParent();
        } catch (Throwable t) {
            parent = null;
        }
    }
}

From source file:SecuritySupport.java

static ClassLoader getParentClassLoader(final ClassLoader cl) {
    return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            ClassLoader parent = null;
            try {
                parent = cl.getParent();
            } catch (SecurityException ex) {
            }//ww w.j  a v a 2  s  .c  o m

            // eliminate loops in case of the boot
            // ClassLoader returning itself as a parent
            return (parent == cl) ? null : parent;
        }
    });
}

From source file:Main.java

/**
 * Loads a class from the classloader; // w w w .  j  a v a 2s  .c  o  m
 * If not found, the classloader of the {@code context} class specified will be used.
 * If the flag {@code checkParent} is true, the classloader's parent is included in 
 * the lookup.
 */
static Class<?> loadClass(String className, Class<?> context, boolean checkParent) {
    Class<?> clazz = null;
    try {
        clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
    } catch (ClassNotFoundException e) {
        if (context != null) {
            ClassLoader loader = context.getClassLoader();
            while (loader != null) {
                try {
                    clazz = loader.loadClass(className);
                    return clazz;
                } catch (ClassNotFoundException e1) {
                    loader = checkParent ? loader.getParent() : null;
                }
            }
        }
    }
    return clazz;
}

From source file:com.stratuscom.harvester.Utils.java

public static void logClassLoaderHierarchy(Logger log, Level level, Class cls) {
    log.log(level, MessageNames.CLASSLOADER_IS, new Object[] { cls.getName(), cls.getClassLoader() });
    try {// ww  w  .j  av  a  2  s  .  c  o m
        ClassLoader parent = cls.getClassLoader().getParent();
        while (parent != null) {
            log.log(level, MessageNames.PARENT_CLASS_LOADER_IS, new Object[] { parent });
            parent = parent.getParent();
        }
    } catch (Throwable t) {
        log.log(level, Strings.NEWLINE);
    }

}

From source file:Loader.java

public static URL getResource(Class loadClass, String name, boolean checkParents)
        throws ClassNotFoundException {
    URL url = null;/*w w  w . j a  va  2  s .c om*/
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    while (url == null && loader != null) {
        url = loader.getResource(name);
        loader = (url == null && checkParents) ? loader.getParent() : null;
    }

    loader = loadClass == null ? null : loadClass.getClassLoader();
    while (url == null && loader != null) {
        url = loader.getResource(name);
        loader = (url == null && checkParents) ? loader.getParent() : null;
    }

    if (url == null) {
        url = ClassLoader.getSystemResource(name);
    }

    return url;
}

From source file:com.taobao.tddl.common.config.impl.DefaultConfigDataHandlerFactory.java

private static ClassLoader getLowestClassLoader(ClassLoader c1, ClassLoader c2) {
    if (c1 == null)
        return c2;

    if (c2 == null)
        return c1;

    ClassLoader current;

    current = c1;/*from w w  w  .ja v a 2s . c o m*/
    while (current != null) {
        if (current == c2)
            return c1;
        current = current.getParent();
    }

    current = c2;
    while (current != null) {
        if (current == c1)
            return c2;
        current = current.getParent();
    }

    return null;
}