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.hybris.datahub.service.impl.DefaultJsonServiceTest.java

/**
 * convertTmallAuthJson/*from   w ww. jav  a 2 s .  c  o m*/
 */
@Test
public void convertTmallAuthJson() {
    final Class<? extends DefaultJsonServiceTest> instance = getClass();
    if (instance != null) {
        final ClassLoader classLoader = instance.getClassLoader();
        if (classLoader != null) {
            String json = "";
            InputStream resourceAsStream = null;
            try {
                resourceAsStream = classLoader.getResourceAsStream(SAMPLE_TMALL_AUTH_JSON);
                json = IOUtils.toString(resourceAsStream);
            } catch (final IOException e) {
                LOG.error(e.getMessage(), e);
            } finally {
                if (resourceAsStream != null) {
                    try {
                        resourceAsStream.close();
                    } catch (final IOException e) {
                        LOG.error(e.getMessage(), e);
                    }
                }
            }
            final List<Map<String, String>> csv = service.parse(json);
            final Map<String, String> raw = csv.get(0);

            Assert.assertTrue(raw.containsKey("marketplaceStoreName"));
            Assert.assertTrue(raw.containsKey("integrationId"));
            Assert.assertTrue(raw.containsKey("authorized"));
            Assert.assertEquals("TmallStore", raw.get("marketplaceStoreName"));
            Assert.assertEquals("1234567890", raw.get("integrationId"));
            Assert.assertEquals("true", raw.get("authorized"));
        }
    } else {
        Assert.fail("Class not found.");
    }
}

From source file:com.stratuscom.harvester.classloading.VFSClassLoaderTest.java

/**
The classloader should be able to load a class that's in the jar file,
and when we get an instance of that class, it should have the UUT
as its classloader./*from  ww  w .  ja va 2 s . c  o m*/
@throws Exception
 */
@Test
public void testParentClassLoading() throws Exception {
    VirtualFileSystemClassLoader UUT = new VirtualFileSystemClassLoader(libRoot, extensionLoader, null);
    UUT.addClassPathEntry("reggie-" + JSK_VERSION + ".jar");
    Class c = UUT.loadClass("java.util.List");
    assertNotNull(c);
    assertTrue("Class had wrong classloader:" + c.getClassLoader(), c.getClassLoader() == null);
    assertTrue("java.util.List".equals(c.getName()));
}

From source file:org.sonatype.nexus.rest.client.internal.RestClientFactoryImpl.java

@Override
public <T> T proxy(final Class<T> api, final Client client, final URI baseUri) {
    WebTarget target = client.target(baseUri);

    return ProxyBuilder.builder(api, target)
            .classloader(bridgeClassLoaderCache.getUnchecked(api.getClassLoader())).build();
}

From source file:com.dangdang.ddframe.rdb.sharding.config.common.api.ShardingRuleBuilder.java

@SuppressWarnings("unchecked")
private <T> Class<? extends T> loadClass(final String className, final Class<T> superClass) {
    try {//from   w  ww  .  j  a  v  a2 s .  co m
        return (Class<? extends T>) superClass.getClassLoader().loadClass(className);
    } catch (final ClassNotFoundException ex) {
        throw new IllegalArgumentException(ex);
    }
}

From source file:atunit.spring.SpringContainer.java

protected AbstractBeanDefinition defineAutowireBean(Class<?> type) throws Exception {
    AbstractBeanDefinition beandef = BeanDefinitionReaderUtils.createBeanDefinition(null, type.getName(),
            type.getClassLoader());
    beandef.setAutowireCandidate(true);//from   ww  w  .j a  v  a2 s.c  o m
    beandef.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_AUTODETECT);
    return beandef;
}

From source file:com.hybris.datahub.service.impl.DefaultJsonServiceTest.java

/**
 * convertTmallTradesJson//from   www  . j av a2  s  . c  o m
 *
 * @throws Exception
 */
@Test
public void convertTmallTradesJson() throws Exception {
    final Class<? extends DefaultJsonServiceTest> instance = getClass();
    if (instance != null) {
        final ClassLoader classLoader = instance.getClassLoader();
        if (classLoader != null) {

            String json = "";
            InputStream resourceAsStream = null;
            try {
                resourceAsStream = classLoader.getResourceAsStream(SAMPLE_TMALL_TRADES_JSON);
                json = IOUtils.toString(resourceAsStream);
            } catch (final IOException e) {
                LOG.error(e.getMessage(), e);
            } finally {
                if (resourceAsStream != null) {
                    try {
                        resourceAsStream.close();
                    } catch (final IOException e) {
                        LOG.error(e.getMessage(), e);
                    }
                }
            }
            final List<Map<String, String>> csv = service.parse(json);
            final Map<String, String> raw = csv.get(0);

            Assert.assertTrue(raw.containsKey("marketplaceStoreName"));
            Assert.assertTrue(raw.containsKey("currency"));
            Assert.assertTrue(raw.containsKey("productCatalogVersion"));
            Assert.assertTrue(raw.containsKey("trades"));

            Assert.assertEquals("CNY", raw.get("currency"));
            Assert.assertEquals("electronics-cnProductCatalog:Online", raw.get("productCatalogVersion"));

            final Map<String, String> rawTrade = service.parse(raw.get("trades")).get(0);

            Assert.assertEquals("2100703549093", rawTrade.get("numIid"));
            Assert.assertEquals("TRADE_CLOSED_BY_TAOBAO", rawTrade.get("status"));
        }
    } else {
        Assert.fail("Class not found.");
    }
}

From source file:com.seleniumtests.core.runner.CustomTestNGCucumberRunner.java

/**
 * Bootstrap the cucumber runtime/* w w w.java  2  s  .  c o  m*/
 *
 * @param clazz Which has the cucumber.api.CucumberOptions and org.testng.annotations.Test annotations
 */
public CustomTestNGCucumberRunner(Class<?> clazz) {
    classLoader = clazz.getClassLoader();
    resourceLoader = new MultiLoader(classLoader);

    cucumberFeatures = initCucumberOptions(clazz);

    resultListener = new FeatureResultListener(runtimeOptions.reporter(classLoader), runtimeOptions.isStrict());
}

From source file:ch.digitalfondue.npjt.QueryFactory.java

@SuppressWarnings("unchecked")
public <T> T from(final Class<T> clazz) {

    return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, new InvocationHandler() {
        @Override//from w w  w. j  a v  a 2 s.  co m
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            boolean hasAnnotation = method.getAnnotation(Query.class) != null;
            if (hasAnnotation) {
                QueryTypeAndQuery qs = extractQueryAnnotation(clazz, method);
                return qs.type.apply(qs.query, qs.rowMapperClass, jdbc, method, args, columnMapperFactories.set,
                        parameterConverters.set);
            } else if (method.getReturnType().equals(NamedParameterJdbcTemplate.class) && args == null) {
                return jdbc;
            } else if (IS_DEFAULT_METHOD != null && (boolean) IS_DEFAULT_METHOD.invoke(method)) {
                final Class<?> declaringClass = method.getDeclaringClass();
                return LOOKUP_CONSTRUCTOR.newInstance(declaringClass, MethodHandles.Lookup.PRIVATE)
                        .unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args);
            } else {
                throw new IllegalArgumentException(
                        String.format("missing @Query annotation for method %s in interface %s",
                                method.getName(), clazz.getSimpleName()));
            }

        }
    }

    );
}

From source file:hudson.gridmaven.gridlayer.HadoopInstance.java

public HadoopInstance(Class c) {
    Configuration conf = new Configuration();
    conf.set("fs.default.name", "hdfs://localhost:9000/");
    conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
    conf.set("fs.file.impl", "org.apache.hadoop.fs.LocalFileSystem");

    // Workaround bug with hadoop Configuration classloader, which
    // do not know abou current classloader and replaces it with
    // CurrentThreadContextClassloader, and that cant see hadoop libraries loaded.
    Class a = c;
    ClassLoader loader = a.getClassLoader();
    conf.setClassLoader(loader);/*from  ww w . ja  v  a 2 s  .c  o m*/

    this.conf = conf;
    // Retrieve fs if it can be done
    try {
        this.fs = FileSystem.get(conf);
    } catch (IOException ex) {
        ex.printStackTrace();
        Logger.getLogger(HadoopInstance.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.stratuscom.harvester.classloading.VFSClassLoaderTest.java

/**
The classloader should be able to load a class that's in the jar file,
and when we get an instance of that class, it should have the UUT
as its classloader./*w w  w  .j  ava  2s.c om*/
@throws Exception
 */
@Test
public void testClassLoading() throws Exception {
    VirtualFileSystemClassLoader UUT = new VirtualFileSystemClassLoader(libRoot, extensionLoader, null);
    UUT.addClassPathEntry("reggie-" + JSK_VERSION + ".jar");
    Class c = UUT.loadClass("com.sun.jini.reggie.ClassMapper");
    assertNotNull(c);
    assertTrue("Class had wrong classloader:" + c.getClassLoader(), c.getClassLoader() == UUT);
}