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.savoirtech.eos.pattern.whiteboard.EventListenerWhiteboard.java

/**
 * Constructs a new EventListenerWhiteboard which tracks services of the specified listener type and
 * adds them to its {@link EventListenerSupport}.  The services will be "decorated" using the supplied
 * deocrator function./*from  ww  w .jav a 2s. co  m*/
 *
 * @param bundleContext the bundle context
 * @param listenerType  the listener interface
 * @param decorator     the decorator function
 */
public EventListenerWhiteboard(BundleContext bundleContext, Class<L> listenerType,
        BiFunction<L, ServiceProperties, L> decorator) {
    super(bundleContext, listenerType);
    this.listenerSupport = new EventListenerSupport<>(listenerType, listenerType.getClassLoader());
    this.decorator = decorator;
    start();
}

From source file:org.echocat.jomon.runtime.i18n.RecursiveResourceBundleFactory.java

public void flushEntriesOf(@Nonnull ClassLoader classLoader) {
    synchronized (this) {
        _classLoaderToPackageNameAndNotRecursiveBundleCache.remove(classLoader);
        final Iterator<Class<?>> i = _typeToNotRecursiveBundleCache.keySet().iterator();
        while (i.hasNext()) {
            final Class<?> type = i.next();
            if (classLoader.equals(type.getClassLoader())) {
                i.remove();//from w  w  w  . ja v  a 2 s.com
            }
        }
    }
}

From source file:info.dolezel.fatrat.plugins.helpers.JarClassLoader.java

public String loadPackagedFile(Class c, String path) {
    try {/*from   ww  w.j  a  v  a 2 s . c o m*/
        InputStream stream = c.getClassLoader().getResourceAsStream(path);
        if (stream == null)
            return null;
        return IOUtils.toString(stream, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:de.dfki.kiara.jsonrpc.JsonRpcProtocol.java

@Override
public InterfaceCodeGen createInterfaceCodeGen(final ConnectionBase connection) {
    final JsonRpcProtocol thisProtocol = this;
    return new InterfaceCodeGen() {
        @Override//from   w  ww .  j a v a2  s  .  co m
        public <T> T generateInterfaceImpl(Class<T> interfaceClass, InterfaceMapping<T> mapping) {
            Object impl = Proxy.newProxyInstance(interfaceClass.getClassLoader(),
                    new Class<?>[] { interfaceClass, RemoteInterface.class },
                    new DefaultInvocationHandler(connection, mapping, thisProtocol));
            return interfaceClass.cast(impl);
        }
    };
}

From source file:com.brighttag.agathon.resources.ValidatingJacksonJsonProviderTest.java

/**
 * Necessary to "instantiate" an annotation, since mocking #annotationType() fails.
 * @see http://stackoverflow.com/questions/2786292
 *//*w w  w . j  av a  2 s .  c o m*/
private <T extends Annotation> Annotation createMockAnnotation(final Class<T> klass) {
    return (Annotation) Proxy.newProxyInstance(klass.getClassLoader(), new Class[] { Annotation.class },
            new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) {
                    return klass; // only getClass() or annotationType() should be called.
                }
            });
}

From source file:net.ymate.platform.plugin.handle.PluginHandler.java

@SuppressWarnings("unchecked")
public Object handle(Class<?> targetClass) throws Exception {
    if (ClassUtils.isInterfaceOf(targetClass, IPlugin.class)) {
        Plugin _plugin = targetClass.getAnnotation(Plugin.class);
        PluginMeta _meta = new PluginMeta(targetClass.getClassLoader());
        ///*w ww  .  java2s. com*/
        _meta.setId(StringUtils.defaultIfBlank(_plugin.id(), DigestUtils.md5Hex(targetClass.getName())));
        _meta.setName(StringUtils.defaultIfBlank(_plugin.name(), targetClass.getSimpleName()));
        _meta.setAlias(_plugin.alias());
        _meta.setInitClass((Class<? extends IPlugin>) targetClass);
        _meta.setVersion(_plugin.version());
        _meta.setAuthor(_plugin.version());
        _meta.setEmail(_plugin.email());
        _meta.setAutomatic(_plugin.automatic());
        _meta.setDescription(_plugin.description());
        //
        if (targetClass.getClassLoader() instanceof PluginClassLoader) {
            _meta.setPath(((PluginClassLoader) targetClass.getClassLoader()).getPluginHome());
        }
        //
        return _meta;
    }
    return null;
}

From source file:org.apache.axis2.jaxws.spi.ServiceDelegate.java

/** @return ClassLoader */
private static ClassLoader getClassLoader(final Class cls) {
    // NOTE: This method must remain private because it uses AccessController
    ClassLoader cl = null;/*from  www  .j a v  a2s .c om*/
    try {
        cl = (ClassLoader) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws ClassNotFoundException {
                return cls.getClassLoader();
            }
        });
    } catch (PrivilegedActionException e) {
        if (log.isDebugEnabled()) {
            log.debug("Exception thrown from AccessController: " + e);
        }
        throw ExceptionFactory.makeWebServiceException(e.getException());
    }

    return cl;
}

From source file:com.intuit.karate.cucumber.CucumberRunner.java

public CucumberRunner(Class clazz) {
    logger.debug("init test class: {}", clazz);
    classLoader = clazz.getClassLoader();
    RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(clazz);
    runtimeOptions = runtimeOptionsFactory.create();
    resourceLoader = new MultiLoader(classLoader);
    List<CucumberFeature> cfs = runtimeOptions.cucumberFeatures(resourceLoader);
    featureFiles = new ArrayList<>(cfs.size());
    for (CucumberFeature cf : cfs) {
        featureFiles.add(new FeatureFile(cf, new File(cf.getPath())));
    }/* ww  w  .j  a  v  a2 s  .  c  o  m*/
}

From source file:com.netflix.dyno.contrib.ElasticConnectionPoolConfigurationPublisher.java

/**
 * Get library version by iterating through the classloader jar list and obtain the name from the jar filename.
 * This will not open the library jar files.
 * <p>//from  w w w  .ja v  a2 s. c  om
 * This function assumes the conventional jar naming format and relies on the dash character to separate the
 * name of the jar from the version. For example, foo-bar-baz-1.0.12-CANDIDATE.
 *
 * @param libraryNames unique list of library names, i.e. "dyno-core"
 * @param classLoadedWithURLClassLoader For this to work, must have a URL based classloader
 *        This has been tested to be the case in Tomcat (WebAppClassLoader) and basic J2SE classloader
 * @return the version of the library (everything between library name, dash, and .jar)
 */
Map<String, String> getLibraryVersion(Class<?> classLoadedWithURLClassLoader, Set<String> libraryNames) {
    ClassLoader cl = classLoadedWithURLClassLoader.getClassLoader();
    Map<String, String> libraryVersionMapping = new HashMap<String, String>();

    if (cl instanceof URLClassLoader) {
        @SuppressWarnings("resource")
        URLClassLoader uCl = (URLClassLoader) cl;
        URL urls[] = uCl.getURLs();

        for (URL url : urls) {
            String fullNameWithVersion = url.toString().substring(url.toString().lastIndexOf('/'));
            if (fullNameWithVersion.length() > 4) { // all entries we attempt to parse must end in ".jar"
                String nameWithVersion = fullNameWithVersion.substring(1, fullNameWithVersion.length() - 4);
                int idx = findVersionStartIndex(nameWithVersion);
                if (idx > 0) {
                    String name = nameWithVersion.substring(0, idx - 1);
                    if (libraryNames.contains(name)) {
                        libraryVersionMapping.put(name, nameWithVersion.substring(idx));
                    }
                }
            }
        }
    }

    return libraryVersionMapping;
}

From source file:org.echocat.jomon.runtime.ManifestInformationFactory.java

public ManifestInformationFactory(@Nonnull Class<?> baseClass) {
    this(baseClass.getName().replace('.', '/') + ".class", baseClass.getClassLoader());
}