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.nearinfinity.blur.thrift.AsyncClientPool.java

/**
 * Gets a client instance that implements the AsyncIface interface that
 * connects to the given connection string.
 * /*from   ww  w  .j a va 2  s .  co m*/
 * @param <T>
 * @param asyncIfaceClass
 *          the AsyncIface interface to pool.
 * @param connectionStr
 *          the connection string.
 * @return the client instance.
 */
@SuppressWarnings("unchecked")
public <T> T getClient(final Class<T> asyncIfaceClass, final String connectionStr) {
    List<Connection> connections = BlurClientManager.getConnections(connectionStr);
    Collections.shuffle(connections, random);
    //randomness ftw
    final Connection connection = connections.get(0);
    return (T) Proxy.newProxyInstance(asyncIfaceClass.getClassLoader(), new Class[] { asyncIfaceClass },
            new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    return execute(new AsyncCall(asyncIfaceClass, method, args, connection));
                }
            });
}

From source file:net.pms.external.ExternalFactory.java

private static void purgeCode(String mainClass, URL newUrl) {
    Class<?> clazz1 = null;

    for (Class<?> clazz : externalListenerClasses) {
        if (mainClass.equals(clazz.getCanonicalName())) {
            clazz1 = clazz;// w ww .  j  a v  a  2 s . co  m
            break;
        }
    }

    if (clazz1 == null) {
        return;
    }

    externalListenerClasses.remove(clazz1);
    ExternalListener remove = null;
    for (ExternalListener list : externalListeners) {
        if (list.getClass().equals(clazz1)) {
            remove = list;
            break;
        }
    }

    RendererConfiguration.resetAllRenderers();

    if (remove != null) {
        externalListeners.remove(remove);
        remove.shutdown();
        LooksFrame frame = (LooksFrame) PMS.get().getFrame();
        frame.getPt().removePlugin(remove);
    }

    for (int i = 0; i < 3; i++) {
        System.gc();
    }

    URLClassLoader cl = (URLClassLoader) clazz1.getClassLoader();
    URL[] urls = cl.getURLs();
    for (URL url : urls) {
        String mainClass1 = getMainClass(url);

        if (mainClass1 == null || !mainClass.equals(mainClass1)) {
            continue;
        }

        File f = url2file(url);
        File f1 = url2file(newUrl);

        if (f1 == null || f == null) {
            continue;
        }

        if (!f1.getName().equals(f.getName())) {
            addToPurgeFile(f);
        }
    }
}

From source file:org.apache.blur.thrift.AsyncClientPool.java

/**
 * Gets a client instance that implements the AsyncIface interface that
 * connects to the given connection string.
 * /*w w w.  j  a va  2  s  . c o  m*/
 * @param <T>
 * @param asyncIfaceClass
 *          the AsyncIface interface to pool.
 * @param connectionStr
 *          the connection string.
 * @return the client instance.
 */
@SuppressWarnings("unchecked")
public <T> T getClient(final Class<T> asyncIfaceClass, final String connectionStr) {
    List<Connection> connections = BlurClientManager.getConnections(connectionStr);
    Collections.shuffle(connections, random);
    // randomness ftw
    final Connection connection = connections.get(0);
    return (T) Proxy.newProxyInstance(asyncIfaceClass.getClassLoader(), new Class[] { asyncIfaceClass },
            new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    return execute(new AsyncCall(asyncIfaceClass, method, args, connection));
                }
            });
}

From source file:org.chromium.sdk.internal.protocolparser.dynamicimpl.ParserRootImpl.java

ParserRootImpl(Class<R> rootClass, Map<Class<?>, TypeHandler<?>> type2TypeHandler)
        throws JsonProtocolModelParseException {
    this.rootClass = rootClass;
    ParseInterfaceSession session = new ParseInterfaceSession(type2TypeHandler);
    session.run(rootClass);//from   w  w w.  j  a  va  2 s.  co m
    this.invocationHandler = session.createInvocationHandler();
    Object result = Proxy.newProxyInstance(rootClass.getClassLoader(), new Class<?>[] { rootClass },
            invocationHandler);
    this.instance = (R) result;
}

From source file:com.mmnaseri.dragonfly.entity.impl.DefaultEntityContext.java

private <E> Class<? extends E> enhanceClass(Class<E> original, EntityProxy<E> entityProxy) {
    final StaticNamingPolicy namingPolicy = new StaticNamingPolicy("Entity");
    final String className = namingPolicy.getClassName(original, null);
    Class<? extends E> enhancedClass = null;
    try {/*from  w  ww .j  a v a 2  s.c  om*/
        enhancedClass = ((Class<?>) ClassUtils.forName(className, original.getClassLoader()))
                .asSubclass(original);
    } catch (ClassNotFoundException ignored) {
    }
    if (enhancedClass != null) {
        return enhancedClass;
    }
    final ClassEnhancer<E> classEnhancer = new GeneratingClassEnhancer<E>(defaultClassLoader);
    if (defaultClassLoader == null) {
        log.warn(
                "No class loader has been specified for the entity enhancer. This is likely to cause issues when "
                        + "using data access in a container (such as a web application container)");
    } else {
        final GeneratingClassEnhancer<?> enhancer = (GeneratingClassEnhancer<?>) classEnhancer;
        final DefaultDynamicClassCompiler compiler = new DefaultDynamicClassCompiler(defaultClassLoader);
        final String classPath = getClassPath(defaultClassLoader);
        log.debug("Using classpath " + classPath);
        compiler.setOption(SimpleJavaSourceCompiler.Option.CLASSPATH, classPath);
        enhancer.setCompiler(compiler);
    }
    classEnhancer.setInterfaces(entityProxy.getInterfaces());
    classEnhancer.setSuperClass(original);
    classEnhancer.setNamingPolicy(namingPolicy);
    return classEnhancer.enhance();
}

From source file:com.liferay.httpservice.internal.servlet.BundleServletContextTest.java

@Test
public void testGetClassloader() {
    mockBundleWiring();/*from  w ww  .  ja  va 2  s  .  c om*/

    Class<?> clazz = getClass();

    ClassLoader classLoader = clazz.getClassLoader();

    Assert.assertEquals(classLoader, _bundleServletContext.getClassLoader());

    verifyBundleWiring();
}

From source file:org.gradle.configuration.internal.DefaultListenerBuildOperationDecorator.java

@SuppressWarnings("unchecked")
public <T> T decorate(String registrationPoint, Class<T> targetClass, T listener) {
    if (listener instanceof InternalListener || !isSupported(listener)) {
        return listener;
    }//  w  w w. j  ava 2 s  .co  m

    UserCodeApplicationId applicationId = userCodeApplicationContext.current();
    if (applicationId == null) {
        return listener;
    }

    Class<?> listenerClass = listener.getClass();
    List<Class<?>> allInterfaces = ClassUtils.getAllInterfaces(listenerClass);
    BuildOperationEmittingInvocationHandler handler = new BuildOperationEmittingInvocationHandler(applicationId,
            registrationPoint, listener);
    return targetClass.cast(Proxy.newProxyInstance(listenerClass.getClassLoader(),
            allInterfaces.toArray(new Class[0]), handler));
}

From source file:com.pentaho.big.data.bundles.impl.shim.common.ShimBridgingClassloaderTest.java

@Test
public void testFindClassSuccess() throws ClassNotFoundException, IOException, KettleFileException {
    String canonicalName = ShimBridgingClassloader.class.getCanonicalName();
    FileObject myFile = KettleVFS.getFileObject("ram://testFindClassSuccess");
    try (FileObject fileObject = myFile) {
        try (OutputStream outputStream = fileObject.getContent().getOutputStream(false)) {
            IOUtils.copy(//  www  . j ava2s  .c  om
                    getClass().getClassLoader().getResourceAsStream(canonicalName.replace(".", "/") + ".class"),
                    outputStream);
        }
        String packageName = ShimBridgingClassloader.class.getPackage().getName();
        when(bundleWiring.findEntries("/" + packageName.replace(".", "/"),
                ShimBridgingClassloader.class.getSimpleName() + ".class", 0))
                        .thenReturn(Arrays.asList(fileObject.getURL()));
        when(parentClassLoader.loadClass(anyString(), anyBoolean())).thenAnswer(new Answer<Class<?>>() {
            @Override
            public Class<?> answer(InvocationOnMock invocation) throws Throwable {
                Object[] arguments = invocation.getArguments();
                return new ShimBridgingClassloader.PublicLoadResolveClassLoader(getClass().getClassLoader())
                        .loadClass((String) arguments[0], (boolean) arguments[1]);
            }
        });
        Class<?> shimBridgingClassloaderClass = shimBridgingClassloader.findClass(canonicalName);
        assertEquals(canonicalName, shimBridgingClassloaderClass.getCanonicalName());
        assertEquals(shimBridgingClassloader, shimBridgingClassloaderClass.getClassLoader());
        assertEquals(packageName, shimBridgingClassloaderClass.getPackage().getName());
    } finally {
        myFile.delete();
    }
}

From source file:com.google.gdt.eclipse.designer.util.GwtInvocationEvaluatorInterceptor.java

@Override
public Method resolveMethod(Class<?> clazz, String signature) throws Exception {
    if (isJavaScriptObject_rewrittenInterface(clazz)) {
        String implClassName = clazz.getName() + "$";
        Class<?> implClass = clazz.getClassLoader().loadClass(implClassName);
        return ReflectionUtils.getMethodBySignature(implClass, signature);
    }/*from w w w . ja  v  a  2 s .  c o  m*/
    return null;
}

From source file:com.sqewd.open.dal.core.persistence.DataManager.java

private void scanEntities(final List<Class<?>> classes) throws Exception {
    for (Class<?> type : classes) {
        if (type.isAnnotationPresent(Entity.class)) {
            log.debug("Found entity : [" + type.getCanonicalName() + "]["
                    + type.getClassLoader().getClass().getCanonicalName() + "]");
            ReflectionUtils.get().load(type);
        }/*w ww  .j av  a 2  s  .  c  o m*/
    }
}