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.pentaho.big.data.bundles.impl.shim.common.ShimBridgingClassloader.java

@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
    Class<?> result = null;
    synchronized (this) {
        result = findLoadedClass(name);/*from   w w w  . j  a va  2  s.  c o m*/
    }
    if (result == null) {
        try {
            result = findClass(name);
        } catch (Exception e) {
            // Ignore
        }
    }
    if (result == null) {
        try {
            Class<?> osgiProvidedClass = bundleWiringClassloader.loadClass(name, resolve);
            if (osgiProvidedClass.getClassLoader() == PluginRegistry.class.getClassLoader()) {
                // Give parent a chance to supercede the system classloader (workaround for boot delegation of packages we
                // should have loaded from the parent)
                try {
                    return super.loadClass(name, resolve);
                } catch (Exception e) {
                    // Ignore
                }
            }
            return osgiProvidedClass;
        } catch (Exception e) {
            // Ignore
        }
    }
    if (result == null) {
        return super.loadClass(name, resolve);
    }
    if (resolve) {
        resolveClass(result);
    }
    return result;
}

From source file:org.akita.proxy.ProxyInvocationHandler.java

public Object bind(Class<?> clazz) {
    Class<?>[] clazzs = { clazz };
    Object newProxyInstance = Proxy.newProxyInstance(clazz.getClassLoader(), clazzs, this);
    return newProxyInstance;
}

From source file:com.dianping.resource.io.util.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 w  ww  .  j  a  va  2s .co m
 * @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 true;
    }
    ClassLoader cur = classLoader;
    if (cur == target) {
        return true;
    }
    while (cur != null) {
        cur = cur.getParent();
        if (cur == target) {
            return true;
        }
    }
    return false;
}

From source file:de.perdian.commons.i18n.polyglot.PolyglotImpl.java

@Override
public T getInstance(Locale locale) {
    if (locale == null) {
        throw new IllegalArgumentException("Parameter 'locale' must not be null");
    } else {/*from   w ww  . java2 s.com*/
        T cachedInstance = this.getLocaleToInstanceCache().get(locale);
        if (cachedInstance == null) {
            Class<T> instanceClass = this.getInstanceClass();
            cachedInstance = instanceClass.cast(Proxy.newProxyInstance(instanceClass.getClassLoader(),
                    new Class[] { instanceClass }, this.createInvocationHandler(locale)));
            this.getLocaleToInstanceCache().put(locale, cachedInstance);
        }
        return cachedInstance;
    }
}

From source file:io.fabric8.fab.DependencyTestSupport.java

protected void assertDifferentClass(DependencyClassLoader cl1, DependencyClassLoader cl2, String name)
        throws ClassNotFoundException {
    Class<?> class1 = assertLoadClass(cl1, name);
    Class<?> class2 = assertLoadClass(cl2, name);

    LOG.debug("Found class " + class1 + " in " + class1.getClassLoader());
    LOG.debug("Found class " + class2 + " in " + class2.getClassLoader());

    assertNotSame(//from  w w  w .  j a  v  a2s. co  m
            "Should have loaded different classes for: " + name + " in class loaders " + cl1 + " and " + cl2,
            class1, class2);
    assertTrue("Should have loaded different classes: " + name + " in class loaders " + cl1 + " and " + cl2,
            !class1.equals(class2));
}

From source file:io.fabric8.fab.DependencyTestSupport.java

protected void assertSameClass(DependencyClassLoader cl1, DependencyClassLoader cl2, String name)
        throws ClassNotFoundException {
    Class<?> class1 = assertLoadClass(cl1, name);
    Class<?> class2 = assertLoadClass(cl2, name);

    LOG.debug("Found class " + class1 + " in " + class1.getClassLoader());
    LOG.debug("Found class " + class2 + " in " + class2.getClassLoader());

    ClassLoader foundClassLoader1 = class1.getClassLoader();
    ClassLoader foundClassLoader2 = class2.getClassLoader();
    assertSame("Should have loaded same class: " + name + " in class loaders " + cl1 + " and " + cl2
            + " when found in " + foundClassLoader1 + " and " + foundClassLoader2, class1, class2);
    assertEquals("Should have loaded equal class: " + name + " in class loaders " + cl1 + " and " + cl2, class1,
            class2);
}

From source file:minium.cucumber.MiniumCucumber.java

public MiniumCucumber(Class<?> clazz) throws InitializationError, IOException {
    super(clazz);
    ClassLoader classLoader = clazz.getClassLoader();
    Assertions.assertNoCucumberAnnotatedMethods(clazz);

    MiniumRhinoTestContextManager contextManager = new MiniumRhinoTestContextManager(MiniumCucumberTest.class);
    ConfigurableListableBeanFactory beanFactory = contextManager.getBeanFactory();

    // this will populate @Autowired fields
    initializeInstance(MiniumCucumber.class, beanFactory, this);

    // now we populate RhinoEngine with minium modules, etc.
    new MiniumJsEngineAdapter(browser, factory).adapt(rhinoEngine);

    // and set configuration
    rhinoEngine.putJson("config", configProperties.toJson());

    // we now build cucumber runtime and load glues
    RuntimeBuilder runtimeBuilder = rhinoEngine
            .runWithContext(rhinoEngine.new RhinoCallable<RuntimeBuilder, IOException>() {
                @Override/* w  ww . j a  va  2  s.  c  o m*/
                protected RuntimeBuilder doCall(Context cx, Scriptable scope) throws IOException {
                    RuntimeBuilder runtimeBuilder = new RuntimeBuilder();
                    runtimeBuilder.withArgs(cucumberProperties.getOptions().toArgs())
                            .withClassLoader(Thread.currentThread().getContextClassLoader())
                            .withResourceLoader(resourceLoader).withBackends(allBackends()).build();
                    return runtimeBuilder;
                }
            });
    runtime = runtimeBuilder.getRuntime();
    RuntimeOptions runtimeOptions = runtimeBuilder.getRuntimeOptions();

    final List<CucumberFeature> cucumberFeatures = runtimeOptions.cucumberFeatures(resourceLoader);
    jUnitReporter = new JUnitReporter(runtimeOptions.reporter(classLoader),
            runtimeOptions.formatter(classLoader), runtimeOptions.isStrict());
    addChildren(cucumberFeatures);
}

From source file:com.techtrip.dynbl.web.controllers.DynamicBeanController.java

@RequestMapping(method = RequestMethod.GET, value = "/registerBean")
@ResponseBody//www  . ja v a2 s .  c  om
private String registerBean(@RequestParam(value = "beanName", required = true) String beanName,
        @RequestParam(value = "beanType", required = true) String beanType) {
    try {

        /* String demoMessage = getDemoMessage(); */

        Class<?> c;

        c = Class.forName(beanType);

        CachedIntrospectionResults.clearClassLoader(c.getClassLoader());

        beanFactory.registerBean(c, beanName, "prototype", false, true);

        Object myBean = beanFactory.getBean(beanName);

        String ret = PAGE_HEADER + SPRING_VERSION + BEAN_INFO_MSG + CLASSLOADER_DELAY + BEAN_INFO_HEADER
                + String.format(BEAN_INFO_FORMAT, beanName, myBean.getClass().getName(), myBean.toString()) /*
                                                                                                            * +
                                                                                                            * DEMO_MESSAGE_HEADER
                                                                                                            * +
                                                                                                            * tagHTML(
                                                                                                            * "p",
                                                                                                            * demoMessage
                                                                                                            * )
                                                                                                            */;

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(beanType);
        }

        /* CachedIntrospectionResults cir; */
        /*
         * 
         * if (ClassUtils.isCacheSafe(beanClass,
         * CachedIntrospectionResults.class.getClassLoader()) ||
         * isClassLoaderAccepted(beanClass.getClassLoader())) {
         */
        // CachedIntrospectionResults.clearClassLoader(c.getClassLoader());

        /* tripsDemo.getMessage(); */

        return ret;
    } catch (Exception e) {
        return String.format("Unable to create bean of type %s with the following error:</br>%s!", beanType,
                e.getMessage());
    }
}

From source file:org.javelin.sws.ext.bind.SweJaxbContextFactoryTest.java

@Test
public void nonDefaultClassLoader() throws Exception {
    // parent-last class loader
    // see org.apache.cocoon.servlet.ParanoidClassLoader
    URLClassLoader cl = new URLClassLoader(
            new URL[] { new File("target/test-classes").getCanonicalFile().toURI().toURL() }) {
        @Override/* w w w  .  j  a va 2 s.  c o  m*/
        protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
            Class<?> c = this.findLoadedClass(name);
            if (c == null) {
                try {
                    c = this.findClass(name);
                } catch (ClassNotFoundException e) {
                    if (this.getParent() != null) {
                        c = this.getParent().loadClass(name);
                    } else {
                        throw e;
                    }
                }
            }
            if (resolve)
                this.resolveClass(c);
            return c;
        }
    };

    JAXBContext ctx = SweJaxbContextFactory.createContext("org.javelin.sws.ext.bind.context2", cl);
    @SuppressWarnings("unchecked")
    Map<Class<?>, TypedPattern<?>> patterns = (Map<Class<?>, TypedPattern<?>>) ReflectionTestUtils.getField(ctx,
            "patterns");
    Class<?> mc = null;
    for (Class<?> c : patterns.keySet()) {
        if (c.getName().equals("org.javelin.sws.ext.bind.context2.MyClass2"))
            mc = c;
    }
    assertSame(mc.getClassLoader(), cl);
    Class<?> c1 = ClassUtils.resolveClassName("org.javelin.sws.ext.bind.context2.MyClass2",
            this.getClass().getClassLoader());
    assertFalse(patterns.containsKey(c1));
    Class<?> c2 = ClassUtils.resolveClassName("org.javelin.sws.ext.bind.context2.MyClass2", cl);
    assertTrue(patterns.containsKey(c2));
}

From source file:org.jaxygen.client.jaxygenclient.JaxygenClient.java

public <T> T lookup(final String className, Class<T> remoteInterface) {
    Class<?> interfaces[] = { remoteInterface };
    return (T) java.lang.reflect.Proxy.newProxyInstance(remoteInterface.getClassLoader(), interfaces,
            new Handler(url + "/" + className, session));
}