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:org.ebayopensource.turmeric.runtime.tests.config.CompareUtils.java

public static String getCompareString(Class<?> clazz, String compareFilename) throws Exception {
    StringBuffer compare = new StringBuffer();
    String fullResourcePath = getClassRelativeResourceName(clazz, compareFilename);
    URL compareURL = clazz.getClassLoader().getResource(fullResourcePath);
    Assert.assertThat("Attempting to find compare string resource: " + fullResourcePath, compareURL,
            is(notNullValue()));/* w  w  w .j  ava2 s .  c o m*/
    InputStream stream = null;
    BufferedReader br = null;
    try {
        stream = compareURL.openStream();
        br = new BufferedReader(new InputStreamReader(stream));
        String line;
        while ((line = br.readLine()) != null) {
            compare.append(line);
            compare.append('\n');
        }
        br.close();
    } finally {
        IOUtils.closeQuietly(br);
        IOUtils.closeQuietly(stream);
    }

    return compare.toString();
}

From source file:ReflectionUtils.java

/**
 * beanClassAnnotation/*ww w  .  jav a2  s .  c o m*/
 * 
 * @return
 */
public static boolean isAnnotationPresent(String beanClass, Class annotation) {
    ClassLoader classLoader = annotation.getClassLoader();
    Class clz = null;
    try {
        clz = classLoader.loadClass(beanClass);
    } catch (ClassNotFoundException e) {
        logger.warn("" + beanClass);
        return false;
    }
    return clz.isAnnotationPresent(annotation);
}

From source file:org.eclipse.gemini.blueprint.util.LogUtils.java

private static Log doCreateLogger(Class<?> logName) {
    Log logger;// www.  j a v a2s.  c  om

    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    // push the logger class classloader (useful when dealing with commons-logging 1.0.x
    Thread.currentThread().setContextClassLoader(logName.getClassLoader());
    try {
        logger = LogFactory.getLog(logName);
    } catch (Throwable th) {
        logger = new SimpleLogger();
        logger.fatal(
                "logger infrastructure not properly set up. If commons-logging jar is used try switching to slf4j (see the FAQ for more info).",
                th);
    } finally {
        Thread.currentThread().setContextClassLoader(ccl);
    }
    return logger;
}

From source file:org.apache.bval.util.reflection.Reflection.java

/**
 * Get a usable {@link ClassLoader}: that of {@code clazz} if {@link Thread#getContextClassLoader()} returns {@code null}.
 * @param clazz/*  ww  w . j  a va2  s .c  o m*/
 * @return {@link ClassLoader}
 */
public static ClassLoader getClassLoader(final Class<?> clazz) {
    final ClassLoader cl = Thread.currentThread().getContextClassLoader();
    return cl == null ? clazz.getClassLoader() : cl;
}

From source file:org.gsweb.components.util.ComponentResourceUtils.java

public static String getResourceSrc(Class<?> c, String metaInfJSFile) {
    String key = c.getName();//  w  w  w . j a v  a2  s. c  o m
    String out = (String) get(key);
    if (out == null) {
        try {
            out = org.apache.commons.io.IOUtils.toString(
                    c.getClassLoader().getResource(metaInfJSFile).openStream(), Constants.BASE_ENCODING);
            put(key, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return out;
}

From source file:Main.java

/**
 * <P>Attempts to find the most suitable {@link ClassLoader} as follows:</P>
 * <UL>//from w w  w .j av a2 s .  c om
 * <LI><P>
 * Check the {@link Thread#getContextClassLoader()} value
 * </P></LI>
 *
 * <LI><P>
 * If no thread context class loader then check the anchor
 * class (if given) for its class loader
 * </P></LI>
 *
 * <LI><P>
 * If still no loader available, then use {@link ClassLoader#getSystemClassLoader()}
 * </P></LI>
 * </UL>
 *
 * @param anchor The anchor {@link Class} to use if no current thread
 *               - ignored if {@code null}
 *               context class loader
 * @return The resolver {@link ClassLoader}
 */
public static ClassLoader resolveDefaultClassLoader(Class<?> anchor) {
    Thread thread = Thread.currentThread();
    ClassLoader cl = thread.getContextClassLoader();
    if (cl != null) {
        return cl;
    }

    if (anchor != null) {
        cl = anchor.getClassLoader();
    }

    if (cl == null) { // can happen for core Java classes
        cl = ClassLoader.getSystemClassLoader();
    }

    return cl;
}

From source file:org.apache.ws.security.util.Loader.java

/**
 * Get the class loader of the class argument
 * <p/>/*w w w  .  j  a  va2  s.co m*/
 *
 * @return the class loader of the argument
 */
public static ClassLoader getClassLoader(final Class clazz) {
    return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return clazz.getClassLoader();
        }
    });
}

From source file:architecture.common.util.ClassUtils.java

public static URL getResource(String resourceName, Class callingClass) {
    URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
    if (url == null)
        url = ClassUtils.class.getClassLoader().getResource(resourceName);
    if (url == null)
        url = callingClass.getClassLoader().getResource(resourceName);
    return url;//ww  w  .  j a  v a 2 s .  c  om
}

From source file:me.tfeng.toolbox.avro.AvroHelper.java

public static Schema getSchema(Class<?> schemaClass) {
    return new SpecificData(schemaClass.getClassLoader()).getSchema(schemaClass);
}

From source file:org.gsweb.components.util.ComponentResourceUtils.java

public static String getResourceSrc(Class<?> c, String type, String metaInfFile) {
    String key = c.getName() + "." + type;
    String out = (String) get(key);
    if (out == null) {
        try {// w w w . ja v  a  2s  . c  o  m
            out = org.apache.commons.io.IOUtils.toString(
                    c.getClassLoader().getResource(metaInfFile).openStream(), Constants.BASE_ENCODING);
            put(key, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return out;
}