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:lite.log.intercept.Modifier.java

@SuppressWarnings("unchecked")
static public <T> Modifier<T> addLogging(Class<T> clazz, ExecutionContext executionContext,
        LogFactory logFactory) {/*from ww  w . ja  v  a 2  s .c o  m*/

    Class<T> dynamicType = (Class<T>) new ByteBuddy().subclass(clazz)
            .method(any().and(isAnnotatedWith(Log.class)))
            .intercept(MethodDelegation.to(new Interceptor(executionContext, logFactory)))

            .make().load(clazz.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded();

    return new Modifier<T>(dynamicType);
}

From source file:com.revolsys.util.JavaBeanUtil.java

public static void initialize(final Class<?> clazz) {
    if (clazz != null) {
        try {//  w  w  w. j a va 2 s  .  c  o m
            Class.forName(clazz.getName(), true, clazz.getClassLoader());
        } catch (final ClassNotFoundException e) {
            Logs.error(clazz, "Unable to iniaitlize", e);
        }
    }
}

From source file:com.sunchenbin.store.feilong.core.lang.ClassLoaderUtil.java

/**
 * ? {@link ClassLoader}./*from  www  .j  a v a 2s . c om*/
 * 
 * @param callingClass
 *            the calling class
 * @return the class loader by class
 * @see java.lang.Class#getClassLoader()
 */
public static ClassLoader getClassLoaderByClass(Class<?> callingClass) {
    ClassLoader classLoader = callingClass.getClassLoader();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("{}.getClassLoader():{}", callingClass.getSimpleName(),
                JsonUtil.format(getClassLoaderInfoMapForLog(classLoader)));
    }
    return classLoader;
}

From source file:com.springframework.beans.CachedIntrospectionResults.java

/**
 * Clear the introspection cache for the given ClassLoader, removing the
 * introspection results for all classes underneath that ClassLoader, and
 * removing the ClassLoader (and its children) from the acceptance list.
 * @param classLoader the ClassLoader to clear the cache for
 *///w  ww.j  av  a 2s  . c  o m
public static void clearClassLoader(ClassLoader classLoader) {
    for (Iterator<ClassLoader> it = acceptedClassLoaders.iterator(); it.hasNext();) {
        ClassLoader registeredLoader = it.next();
        if (isUnderneathClassLoader(registeredLoader, classLoader)) {
            it.remove();
        }
    }
    for (Iterator<Class<?>> it = strongClassCache.keySet().iterator(); it.hasNext();) {
        Class<?> beanClass = it.next();
        if (isUnderneathClassLoader(beanClass.getClassLoader(), classLoader)) {
            it.remove();
        }
    }
    for (Iterator<Class<?>> it = softClassCache.keySet().iterator(); it.hasNext();) {
        Class<?> beanClass = it.next();
        if (isUnderneathClassLoader(beanClass.getClassLoader(), classLoader)) {
            it.remove();
        }
    }
}

From source file:org.cloudata.core.common.testhelper.FaultInjectionProxy.java

@SuppressWarnings("unchecked")
public static <T> T wrap(Object body, Class<? extends T> c) {
    synchronized (fmLock) {
        if (fm == null) {
            try {
                fm = FaultManager.create(new CloudataConf());
            } catch (IOException e) {
                LOG.warn("Fail to create fault manager", e);
            }/* w ww  . j  a v a  2 s  . c  o  m*/
        }
    }
    return (T) Proxy.newProxyInstance(c.getClassLoader(), new Class<?>[] { c },
            new FaultInjectionHandler(body));
}

From source file:com.plugin.excel.xsd.node.store.impl.FileHelper.java

public static File copyFolder(Class clazz, List<String> files, File destination, String rootDirName) {

    File rootDir = null;//  w  ww.j  a  v  a  2  s .c  o  m

    if (clazz != null && files != null && !files.isEmpty()) {

        for (String file : files) {
            InputStream stream = clazz.getClassLoader().getResourceAsStream(file);
            try {
                File temp = new File(destination.getAbsoluteFile() + "/" + file);
                if (!file.contains(".")) {
                    temp.delete();
                    temp.mkdir();
                } else {
                    FileUtils.copyInputStreamToFile(stream, temp);
                }

                if (file.replaceAll("/", "").equalsIgnoreCase(rootDirName)) {
                    rootDir = temp;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    return rootDir;
}

From source file:me.hurel.hqlbuilder.internal.ProxyUtil.java

@SuppressWarnings("unchecked")
public static <T> T buildProxy(T entity, Class<?> returnType, Class<?> implementation,
        HQBInvocationHandler handler, Class<?>[] paramTypes, Object[] params) {
    T o = entity;//www  . j  ava  2 s . co  m
    if (entity == null || !Enhancer.isEnhanced(entity.getClass())) {
        Enhancer e = new Enhancer();
        e.setClassLoader(returnType.getClassLoader());
        if (returnType != null) {
            e.setSuperclass(returnType);
        }
        if (implementation != null) {
            e.setInterfaces(new Class[] { implementation });
        }
        e.setCallback(handler);

        e.setUseFactory(true);
        if (paramTypes == null) {
            o = (T) e.create();
        } else {
            o = (T) e.create(paramTypes, params);
        }
    }
    handler.declareAlias(o);
    return o;
}

From source file:com.chiorichan.plugin.loader.Plugin.java

/**
 * This method provides fast access to the plugin that has provided the
 * given class.// ww  w. j a va 2 s  . c  o  m
 * 
 * @throws IllegalArgumentException
 *             if the class is not provided by a
 *             Plugin
 * @throws IllegalArgumentException
 *             if class is null
 * @throws IllegalStateException
 *             if called from the static initializer for
 *             given Plugin
 */
public static Plugin getProvidingPlugin(Class<?> clazz) {
    Validate.notNull(clazz, "Null class cannot have a plugin");
    final ClassLoader cl = clazz.getClassLoader();
    if (!(cl instanceof PluginClassLoader)) {
        throw new IllegalArgumentException(clazz + " is not provided by " + PluginClassLoader.class);
    }
    Plugin plugin = ((PluginClassLoader) cl).plugin;
    if (plugin == null) {
        throw new IllegalStateException("Cannot get plugin for " + clazz + " from a static initializer");
    }
    return plugin;
}

From source file:org.jfan.an.utils.VersionInfo.java

/**
 * Sets the user agent to {@code "<name>/<release> (Java 1.5 minimum; Java/<java.version>)"}.
 * <p/>/*from w ww  .  j a v a  2 s. c o  m*/
 * For example:
 * <pre>"Apache-HttpClient/4.3 (Java 1.5 minimum; Java/1.6.0_35)"</pre>
 *
 * @param name the component name, like "Apache-HttpClient".
 * @param pkg
 *            the package for which to load version information, for example "org.apache.http". The package name
 *            should NOT end with a dot.
 * @param cls
 *            the class' class loader to load from, or <code>null</code> for the thread context class loader
 * @since 4.3
 */
public static String getUserAgent(final String name, final String pkg, final Class<?> cls) {
    // determine the release version from packaged version info
    final VersionInfo vi = VersionInfo.loadVersionInfo(pkg, cls.getClassLoader());
    final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE;
    final String javaVersion = System.getProperty("java.version");
    return name + "/" + release + " (Java 1.5 minimum; Java/" + javaVersion + ")";
}

From source file:main.java.refinement_class.Useful.java

static public URL getUrl(Class c, String fileName) throws Exception {
    return c.getClassLoader().getResource(fileName);
}