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:jp.ac.u.tokyo.m.resource.ResourceLoadUtil.java

/**
 * This method read Ini in the jar. <br>
 * If the file does not exist, throws exception. <br>
 * <br>//from   ww  w .jav  a  2 s .c  o m
 * aFileName ??jar? Ini ???? <br>
 * ?????? <br>
 */
public static Ini loadNecessaryPublicIni(Class<?> aClass, String aFileName) {
    return loadNecessaryIni(aClass, aFileName, aClass.getClassLoader().getResourceAsStream(aFileName));
}

From source file:org.drombler.commons.client.util.ResourceBundleUtils.java

private static ResourceBundle getResourceBundle(Class<?> type, String baseName) {
    return getResourceBundle(type.getPackage().getName(), baseName, type.getClassLoader());
}

From source file:org.mule.tools.rhinodo.impl.NodeModuleImplBuilder.java

private static URI getRoot(Class<?> klass, String rootDirectory) {
    ClassLoader classLoader = klass.getClassLoader();
    URI root;/*from   w w  w. ja  v  a  2  s. c  o  m*/
    try {
        URL resource = classLoader.getResource(rootDirectory);
        if (resource == null) {
            throw new IllegalArgumentException("Invalid resource at: " + rootDirectory);
        }
        root = resource.toURI();
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

    if (root == null) {
        throw new IllegalStateException("Error: path not found.");
    }

    try {
        root = new URI(root.toString() + "/");
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

    return root;
}

From source file:Main.java

public static String getApplicationPath(Class cls) {
    if (cls == null)
        throw new java.lang.IllegalArgumentException("parameter is not null !");
    ClassLoader loader = cls.getClassLoader();
    String clsName = cls.getName() + ".class";
    Package pack = cls.getPackage();
    System.out.println("package name is : " + (pack == null));
    String path = "";
    if (pack != null) {
        String packName = pack.getName();
        if (packName.startsWith("java.") || packName.startsWith("javax."))
            throw new java.lang.IllegalArgumentException("This is system class");
        clsName = clsName.substring(packName.length() + 1);
        if (packName.indexOf(".") < 0)
            path = packName + "/";
        else {/*ww  w .j  a  v  a 2s. c om*/
            int start = 0, end = 0;
            end = packName.indexOf(".");
            while (end != -1) {
                path = path + packName.substring(start, end) + "/";
                start = end + 1;
                end = packName.indexOf(".", start);
            }
            path = path + packName.substring(start) + "/";
        }
    }
    java.net.URL url = loader.getResource(path + clsName);
    String realPath = url.getPath();
    int pos = realPath.indexOf("file:");
    if (pos > -1)
        realPath = realPath.substring(pos + 5);
    pos = realPath.indexOf(path + clsName);
    realPath = realPath.substring(0, pos - 1);
    if (realPath.endsWith("!"))
        realPath = realPath.substring(0, realPath.lastIndexOf("/"));
    try {
        realPath = java.net.URLDecoder.decode(realPath, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return realPath;
}

From source file:org.jcurl.core.helpers.Service.java

@SuppressWarnings("unchecked")
public static <E> Iterable<Class<E>> providerClasses(final Class<E> clz, ClassLoader cl) {
    try {/*from w  w  w.j ava  2  s. c  om*/
        if (cl == null)
            cl = clz.getClassLoader();
        final Collection<Class<E>> ret = new LinkedHashSet<Class<E>>();
        // scan all
        for (final Enumeration<URL> e = cl.getResources(META_INF_SERVICES + clz.getName()); e
                .hasMoreElements();) {
            final URL o = e.nextElement();
            final BufferedReader r = new BufferedReader(new InputStreamReader(o.openStream(), UTF_8));
            for (CharSequence line = r.readLine(); line != null; line = r.readLine()) {
                final Matcher m = pat.matcher(line);
                if (!m.matches())
                    continue;
                try {
                    final Class<?> c = Class.forName(m.group(1), true, cl);
                    if (clz.isAssignableFrom(c)) {
                        final Class<E> clz_ = (Class<E>) c;
                        if (ret.contains(clz_))
                            log.warn("Duplicate class " + clz_.getName() + " in " + o.toString());
                        else
                            ret.add(clz_);
                    }
                } catch (final ClassCastException e1) {
                    // ignore.
                } catch (final ClassNotFoundException e1) {
                    // ignore.
                }
            }
        }
        return ret;
    } catch (final IOException e) {
        throw new RuntimeException("Uncaught Exception", e);
    }
}

From source file:de.anhquan.config4j.ConfigFactory.java

public static <T extends Config> T getConfig(Class<T> configType) {
    T configInst = (T) instances.get(configType);
    if (configInst != null) {
        return configInst;
    }/*from   www  .jav  a2 s.  c  o  m*/

    ClassLoader classLoader = configType.getClassLoader();
    Class[] classes = { configType };
    InvocationHandler handler = new ConfigHandler(configType);
    Object proxy = Proxy.newProxyInstance(classLoader, classes, handler);
    configInst = configType.cast(proxy);
    instances.put(configType, configInst);

    return configInst;
}

From source file:org.apache.axis2.classloader.BeanInfoCache.java

/**
 * Locate an appropriate {@link BeanInfoCache} and return a cached {@link BeanInfo} object.
 * This method ensures that caching the {@link BeanInfo} object will not result in a class
 * loader leak.// w w w .ja va2 s.  co  m
 * 
 * @param beanClass
 *            The bean class to be analyzed.
 * @param stopClass
 *            The base class at which to stop the analysis; may be <code>null</code>.
 * @return A {@link BeanInfo} object describing the target bean. 
 * @exception IntrospectionException
 *                if an exception occurs during introspection.
 */
public static BeanInfo getCachedBeanInfo(Class<?> beanClass, Class<?> stopClass) throws IntrospectionException {
    ClassLoader classLoader = beanClass.getClassLoader();
    BeanInfoCache cache;
    if (classLoader instanceof BeanInfoCachingClassLoader) {
        cache = ((BeanInfoCachingClassLoader) classLoader).getBeanInfoCache();
    } else if (classLoader == BeanInfoCache.class.getClassLoader()) {
        cache = localCache;
    } else {
        cache = null;
    }
    if (cache != null) {
        return cache.getBeanInfo(beanClass, stopClass);
    } else {
        if (log.isWarnEnabled()) {
            log.warn("Unable to locate a BeanInfo cache for " + beanClass + " (stopClass=" + stopClass
                    + "). This will negatively affect performance!");
        }
        return Introspector.getBeanInfo(beanClass, stopClass);
    }
}

From source file:org.drombler.commons.client.util.ResourceBundleUtils.java

public static String getPackageResourceStringPrefixed(Class<?> type, String resourceKey) {
    return getPackageResourceStringPrefixed(type.getPackage().getName(), resourceKey, type.getClassLoader());
}

From source file:net.sourceforge.pmd.lang.java.typeresolution.TypeHelper.java

public static boolean isA(TypedNameDeclaration vnd, String className) {
    Class<?> type = vnd.getType();
    if (type != null) {
        Class<?> clazz = loadClass(type.getClassLoader(), className);
        if (clazz != null) {
            return clazz.isAssignableFrom(type);
        }//from  w w w  .j  a va  2s.  c o m
    }
    return false;
}

From source file:com.stratuscom.harvester.Utils.java

public static void logClassLoaderHierarchy(Logger log, Level level, Class cls) {
    log.log(level, MessageNames.CLASSLOADER_IS, new Object[] { cls.getName(), cls.getClassLoader() });
    try {//from  ww  w .ja v a2 s  . c o m
        ClassLoader parent = cls.getClassLoader().getParent();
        while (parent != null) {
            log.log(level, MessageNames.PARENT_CLASS_LOADER_IS, new Object[] { parent });
            parent = parent.getParent();
        }
    } catch (Throwable t) {
        log.log(level, Strings.NEWLINE);
    }

}