Example usage for java.lang Class getClassLoader

List of usage examples for java.lang Class getClassLoader

Introduction

In this page you can find the example usage for java.lang Class getClassLoader.

Prototype

@CallerSensitive
@ForceInline 
public ClassLoader getClassLoader() 

Source Link

Document

Returns the class loader for the class.

Usage

From source file:com.github.ukase.TestHelper.java

public static String getFileContent(String fileName, Class clazz) throws IOException {
    return getFileContent(fileName, clazz.getClassLoader());
}

From source file:Main.java

private static URL getRootUrlForClass(Class<?> cls) {
    ClassLoader classLoader = cls.getClassLoader();
    String resource = cls.getName().replace('.', '/') + ".class";
    if (classLoader == null) {
        // A null class loader means the bootstrap class loader. In this case we use the
        // system class loader. This is safe since we can assume that the system class
        // loader uses parent first as delegation policy.
        classLoader = ClassLoader.getSystemClassLoader();
    }// www.j a va2  s . c  o m
    URL url = classLoader.getResource(resource);
    if (url == null) {
        return null;
    }
    String file = url.getFile();
    if (file.endsWith(resource)) {
        try {
            return new URL(url.getProtocol(), url.getHost(), url.getPort(),
                    file.substring(0, file.length() - resource.length()));
        } catch (MalformedURLException ex) {
            return null;
        }
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Tries to load the class from the current thread's context class loader. If
 * not successful, tries to load the class from the current instance.
 * @param classname Desired class.//from   w w w  .  j av  a2  s . c  o  m
 * @param clazz     Class object used to obtain a class loader
 *                  if no context class loader is available.
 * @return Class, or null on failure.
 */
public static Class loadClass(String classname, Class clazz) throws ClassNotFoundException {
    return loadClass(classname, clazz.getClassLoader());
}

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("  ");
    }/*from ww w .j ava  2s.  c om*/
}

From source file:Main.java

public static String getResourceUrlString(String resourceFileName, Class runningClass) {
    URL url = runningClass.getClassLoader().getResource(resourceFileName);
    if (url == null)
        throw new MissingResourceException("Resource not found: " + resourceFileName, runningClass.getName(),
                resourceFileName);// ww w  .j  ava 2s .  c o m
    return url.toExternalForm();
}

From source file:Main.java

private static JAXBContext getContext(Class<?> clazz) throws JAXBException {
    return JAXBContext.newInstance(clazz.getPackage().getName(), clazz.getClassLoader());
}

From source file:Main.java

/**
 * Use instrumentation to get the class loader for a given class.
 *
 * @param name/* w w  w .j  a v a2s.  c  om*/
 *            the fully qualified classname
 * @return the classloader
 */
static public ClassLoader getClassLoaderByName(String name) {

    Class<?>[] allLoadedClasses = instrumentation.getAllLoadedClasses();

    ClassLoader cl = null;
    for (Class<?> c : allLoadedClasses) {
        if (c.getName().equals(name)) {
            cl = c.getClassLoader();
            break;
        }
    }

    return cl;
}

From source file:org.jcurl.core.helpers.Service.java

/** Compatible to <code>sun.misc.Service#providers</code>. */
public static <E> Iterator<E> providers(final Class<E> clz) {
    return providers(clz, clz.getClassLoader());
}

From source file:Main.java

/**
 * Get an URL to a schema file. This implementation finds the schema file using the ClassLoader
 * that loaded the argument class.//w  w w  .jav a 2  s  .  co m
 * 
 * @param resourceFileName
 * @param runningClass 
 * @return
 */
public static String getResourceUrlString(String resourceFileName, Class<?> runningClass) {
    String rtn = null;
    URL url = runningClass.getClassLoader().getResource(resourceFileName);
    if (url == null)
        throw new MissingResourceException("Resource not found: " + resourceFileName, runningClass.getName(),
                resourceFileName);
    rtn = url.toString();
    return rtn;
}

From source file:SecurityActions.java

static ClassLoader getClassLoader(final Class<?> clazz) {
    if (System.getSecurityManager() == null) {
        return clazz.getClassLoader();
    } else {/*from  w  ww.  ja v  a2 s .  c  o m*/
        return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
            public ClassLoader run() {
                return clazz.getClassLoader();
            }
        });
    }

}