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.grails.beans.support.CachedIntrospectionResults.java

/**
 * Create CachedIntrospectionResults for the given bean class.
 * @param beanClass the bean class to analyze
 * @return the corresponding CachedIntrospectionResults
 * @throws org.springframework.beans.BeansException in case of introspection failure
 *///w  w w.  ja  v  a  2s  .c  om
@SuppressWarnings("unchecked")
public static CachedIntrospectionResults forClass(Class<?> beanClass) throws BeansException {
    CachedIntrospectionResults results = strongClassCache.get(beanClass);
    if (results != null) {
        return results;
    }
    results = softClassCache.get(beanClass);
    if (results != null) {
        return results;
    }

    results = new CachedIntrospectionResults(beanClass);
    ConcurrentMap<Class<?>, CachedIntrospectionResults> classCacheToUse;

    if (ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader())
            || isClassLoaderAccepted(beanClass.getClassLoader())) {
        classCacheToUse = strongClassCache;
    } else {
        classCacheToUse = softClassCache;
    }

    CachedIntrospectionResults existing = classCacheToUse.putIfAbsent(beanClass, results);
    return (existing != null ? existing : results);
}

From source file:org.apache.crunch.types.avro.Avros.java

public static final <T extends SpecificRecord> AvroType<T> specifics(Class<T> clazz) {
    AvroMode.registerSpecificClassLoaderInternal(clazz.getClassLoader());
    T t = ReflectionUtils.newInstance(clazz, null);
    Schema schema = t.getSchema();
    return new AvroType<T>(clazz, schema, new AvroDeepCopier.AvroSpecificDeepCopier<T>(schema));
}

From source file:Main.java

/**
 * Load a given resource. <p/> This method will try to load the resource
 * using the following methods (in order):
 * <ul>//from   w  w w .  j  ava 2s.  c om
 * <li>From Thread.currentThread().getContextClassLoader()
 * <li>From ClassLoaderUtil.class.getClassLoader()
 * <li>callingClass.getClassLoader()
 * </ul>
 * 
 * @param resourceName The name of the resource to load
 * @param callingClass The Class object of the calling object
 */
public static URL getResource(String resourceName, Class callingClass) {
    URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
    if (url == null && resourceName.startsWith("/")) {
        //certain classloaders need it without the leading /
        url = Thread.currentThread().getContextClassLoader().getResource(resourceName.substring(1));
    }

    ClassLoader cluClassloader = Main.class.getClassLoader();
    if (cluClassloader == null) {
        cluClassloader = ClassLoader.getSystemClassLoader();
    }
    if (url == null) {
        url = cluClassloader.getResource(resourceName);
    }
    if (url == null && resourceName.startsWith("/")) {
        //certain classloaders need it without the leading /
        url = cluClassloader.getResource(resourceName.substring(1));
    }

    if (url == null) {
        ClassLoader cl = callingClass.getClassLoader();

        if (cl != null) {
            url = cl.getResource(resourceName);
        }
    }

    if (url == null) {
        url = callingClass.getResource(resourceName);
    }

    if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) {
        return getResource('/' + resourceName, callingClass);
    }

    return url;
}

From source file:org.apache.hadoop.hbase.ipc.HBaseRPC.java

private static synchronized RpcEngine getProtocolEngine(Class protocol, Configuration conf) {
    RpcEngine engine = PROTOCOL_ENGINES.get(protocol);
    if (engine == null) {
        // check for a configured default engine
        Class<?> defaultEngine = conf.getClass(RPC_ENGINE_PROP, WritableRpcEngine.class);

        // check for a per interface override
        Class<?> impl = conf.getClass(RPC_ENGINE_PROP + "." + protocol.getName(), defaultEngine);
        LOG.debug("Using " + impl.getName() + " for " + protocol.getName());
        engine = (RpcEngine) ReflectionUtils.newInstance(impl, conf);
        if (protocol.isInterface())
            PROXY_ENGINES.put(Proxy.getProxyClass(protocol.getClassLoader(), protocol), engine);
        PROTOCOL_ENGINES.put(protocol, engine);
    }/* w  w w.j  a v  a 2 s  . com*/
    return engine;
}

From source file:org.dhatim.util.ClassUtil.java

/**
* Load the specified class./*w  w w .ja v  a 2  s  .c om*/
*
* @param className
*            The name of the class to load.
* @param caller
*            The class of the caller.
* @return The specified class.
* @throws ClassNotFoundException
*             If the class cannot be found.
*/
public static Class forName(final String className, final Class caller) throws ClassNotFoundException {
    final ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();

    Class primitiveClass = primitives.get(className);
    if (primitiveClass != null) {
        return primitiveClass;
    }

    if (threadClassLoader != null) {
        try {
            return threadClassLoader.loadClass(className);
        } catch (final ClassNotFoundException cnfe) {
        } // ignore
    }

    ClassLoader classLoader = caller.getClassLoader();
    if (classLoader != null) {
        try {
            return classLoader.loadClass(className);
        } catch (final ClassNotFoundException cnfe) {
        } // ignore
    }

    return Class.forName(className, true, ClassLoader.getSystemClassLoader());
}

From source file:com.taobao.adfs.distributed.rpc.RPC.java

/**
 * Construct a client-side proxy object that implements the named protocol, talking to a server at the named address.
 *///from  w  w  w.  jav  a 2s.c  o  m
public static VersionedProtocol getProxy(Class<?> protocol, long clientVersion, InetSocketAddress addr,
        Configuration conf, SocketFactory factory) throws IOException {

    VersionedProtocol proxy = (VersionedProtocol) Proxy.newProxyInstance(protocol.getClassLoader(),
            new Class[] { protocol }, new Invoker(addr, conf, factory));
    long serverVersion = proxy.getProtocolVersion(protocol.getName(), clientVersion);
    if (serverVersion == clientVersion) {
        return proxy;
    } else {
        throw new VersionMismatch(protocol.getName(), clientVersion, serverVersion);
    }
}

From source file:com.jeeframework.util.classes.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   ww w  .ja v a 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 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:org.apache.crunch.types.avro.Avros.java

public static final <T> AvroType<T> reflects(Class<T> clazz, Schema schema) {
    AvroMode.registerSpecificClassLoaderInternal(clazz.getClassLoader());
    return new AvroType<T>(clazz, schema, new AvroDeepCopier.AvroReflectDeepCopier<T>(schema));
}

From source file:com.appleframework.core.utils.ClassUtility.java

/** class loaderclass? */
public static String locateClass(Class<?> clazz) {
    return locateClass(clazz.getName(), clazz.getClassLoader());
}

From source file:com.google.code.ddom.commons.cl.ClassRef.java

public ClassRef(Class<?> clazz) {
    this.classLoader = clazz.getClassLoader();
    this.className = clazz.getName();
    this.clazz = clazz;
}