List of usage examples for java.lang Class getClassLoader
@CallerSensitive
@ForceInline
public ClassLoader getClassLoader()
From source file:com.chiorichan.plugin.loader.Plugin.java
/** * This method provides fast access to the plugin that has {@link #getProvidingPlugin(Class) provided} the given plugin class, which is * usually the plugin that implemented it. * <p>//from w w w.ja v a2 s. c o m * An exception to this would be if plugin's jar that contained the class does not extend the class, where the intended plugin would have resided in a different jar / classloader. * * @param clazz * the class desired * @return the plugin that provides and implements said class * @throws IllegalArgumentException * if clazz is null * @throws IllegalArgumentException * if clazz does not extend {@link Plugin} * @throws IllegalStateException * if clazz was not provided by a plugin, * for example, if called with <code>Plugin.getPlugin(Plugin.class)</code> * @throws IllegalStateException * if called from the static initializer for * given Plugin * @throws ClassCastException * if plugin that provided the class does not * extend the class */ public static <T extends Plugin> T getPlugin(Class<T> clazz) { Validate.notNull(clazz, "Null class cannot have a plugin"); if (!Plugin.class.isAssignableFrom(clazz)) { throw new IllegalArgumentException(clazz + " does not extend " + Plugin.class); } final ClassLoader cl = clazz.getClassLoader(); if (!(cl instanceof PluginClassLoader)) { throw new IllegalArgumentException(clazz + " is not initialized by " + PluginClassLoader.class); } Plugin plugin = ((PluginClassLoader) cl).plugin; if (plugin == null) { throw new IllegalStateException("Cannot get plugin for " + clazz + " from a static initializer"); } return clazz.cast(plugin); }
From source file:com.smart.utils.ReflectionUtils.java
/** * beanClass?Annotation//from w w w . j a va 2 s . co m * * @return */ public static boolean isAnnotationPresent(String beanClass, Class annotation) { ClassLoader classLoader = annotation.getClassLoader(); Class clz = null; try { clz = classLoader.loadClass(beanClass); } catch (ClassNotFoundException e) { logger.warn("" + beanClass); return false; } return clz.isAnnotationPresent(annotation); }
From source file:net.ymate.platform.commons.util.ResourceUtils.java
/** * //from ww w .j a v a 2 s .c o m * @param resourceName * @param callingClass * @return */ public static URL getResource(String resourceName, Class<?> callingClass) { URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName); if (url == null) { url = ClassUtils.getDefaultClassLoader().getResource(resourceName); } if (url == null) { url = callingClass.getResource(resourceName); if (url == null) { ClassLoader cl = callingClass.getClassLoader(); if (cl != null) { url = cl.getResource(resourceName); } } } if ((url == null) && (resourceName != null) && (((resourceName.length() == 0) || (resourceName.charAt(0) != '/')))) { return getResource('/' + resourceName, callingClass); } return url; }
From source file:com.iksgmbh.sql.pojomemodb.utils.FileUtil.java
public static String readTextResourceContentFromClassPath(final Class<?> clazz, final String pathToResource) throws IOException { final InputStream resource = clazz.getClassLoader().getResourceAsStream(pathToResource); if (resource == null) { throw new RuntimeException("Cannot find resource '" + pathToResource + "'"); }/*w w w.ja va 2s. c o m*/ final int pos = pathToResource.lastIndexOf('/'); final String filename; if (pos > -1) { filename = pathToResource.substring(pos + 1); } else { filename = pathToResource; } return FileUtil.getFileContent(resource, "/" + filename); }
From source file:eu.stratosphere.nephele.ipc.RPC.java
/** * Construct a client-side proxy object that implements the named protocol, * talking to a server at the named address. *//* ww w .j a v a2 s . co m*/ public static <V extends VersionedProtocol> V getProxy(Class<V> protocol, InetSocketAddress addr, SocketFactory factory) throws IOException { @SuppressWarnings("unchecked") V proxy = (V) Proxy.newProxyInstance(protocol.getClassLoader(), new Class[] { protocol }, new Invoker(addr, factory)); return proxy; }
From source file:org.jboss.bqt.core.BundleUtil.java
/** * Return the {@link BundleUtil} for the class. The bundle must be in the * same package or a parent package of the class. * /* w ww . j a va 2 s . c om*/ * @param clazz * @return BundleUtil */ public static BundleUtil getBundleUtil(Class<?> clazz) { String packageName = clazz.getPackage().getName(); while (true) { // scan up packages until found String bundleName = packageName + ".i18n"; //$NON-NLS-1$ try { ResourceBundle bundle = ResourceBundle.getBundle(bundleName, Locale.getDefault(), clazz.getClassLoader()); return new BundleUtil(packageName, bundleName, bundle); } catch (MissingResourceException e) { int index = packageName.lastIndexOf('.'); if (index < 0) { throw e; } packageName = packageName.substring(0, index); } } }
From source file:com.springframework.beans.BeanUtils.java
/** * Find a JavaBeans PropertyEditor following the 'Editor' suffix convention * (e.g. "mypackage.MyDomainClass" -> "mypackage.MyDomainClassEditor"). * <p>Compatible to the standard JavaBeans convention as implemented by * {@link java.beans.PropertyEditorManager} but isolated from the latter's * registered default editors for primitive types. * @param targetType the type to find an editor for * @return the corresponding editor, or {@code null} if none found *//* w w w . ja va 2 s . c om*/ public static PropertyEditor findEditorByConvention(Class<?> targetType) { if (targetType == null || targetType.isArray() || unknownEditorTypes.contains(targetType)) { return null; } ClassLoader cl = targetType.getClassLoader(); if (cl == null) { try { cl = ClassLoader.getSystemClassLoader(); if (cl == null) { return null; } } catch (Throwable ex) { // e.g. AccessControlException on Google App Engine if (logger.isDebugEnabled()) { logger.debug("Could not access system ClassLoader: " + ex); } return null; } } String editorName = targetType.getName() + "Editor"; try { Class<?> editorClass = cl.loadClass(editorName); if (!PropertyEditor.class.isAssignableFrom(editorClass)) { if (logger.isWarnEnabled()) { logger.warn("Editor class [" + editorName + "] does not implement [java.beans.PropertyEditor] interface"); } unknownEditorTypes.add(targetType); return null; } return (PropertyEditor) instantiateClass(editorClass); } catch (ClassNotFoundException ex) { if (logger.isDebugEnabled()) { logger.debug("No property editor [" + editorName + "] found for type " + targetType.getName() + " according to 'Editor' suffix convention"); } unknownEditorTypes.add(targetType); return null; } }
From source file:eu.mihosoft.vrl.fxscad.MainController.java
/** * Returns the location of the Jar archive or .class file the specified * class has been loaded from. <b>Note:</b> this only works if the class is * loaded from a jar archive or a .class file on the locale file system. * * @param cls class to locate// ww w.j a va2 s. c o m * @return the location of the Jar archive the specified class comes from */ public static File getClassLocation(Class<?> cls) { // VParamUtil.throwIfNull(cls); String className = cls.getName(); ClassLoader cl = cls.getClassLoader(); URL url = cl.getResource(className.replace(".", "/") + ".class"); String urlString = url.toString().replace("jar:", ""); if (!urlString.startsWith("file:")) { throw new IllegalArgumentException("The specified class\"" + cls.getName() + "\" has not been loaded from a location" + "on the local filesystem."); } urlString = urlString.replace("file:", ""); urlString = urlString.replace("%20", " "); int location = urlString.indexOf(".jar!"); if (location > 0) { urlString = urlString.substring(0, location) + ".jar"; } else { //System.err.println("No Jar File found: " + cls.getName()); } return new File(urlString); }
From source file:org.cloudata.core.common.ipc.CRPC.java
public static CVersionedProtocol getProxyWithoutVersionChecking(Class<?> protocol, long clientVersion, InetSocketAddress addr, CloudataConf conf) throws IOException { return (CVersionedProtocol) Proxy.newProxyInstance(protocol.getClassLoader(), new Class[] { protocol }, new Invoker(addr, conf, NetUtils.getDefaultSocketFactory(conf))); }
From source file:ClassLoaderUtils.java
/** * Load a class with a given name./*w ww .j ava 2 s . c o m*/ * <p/> * It will try to load the class in the following order: * <ul> * <li>From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()} * <li>Using the basic {@link Class#forName(java.lang.String) } * <li>From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()} * <li>From the {@link Class#getClassLoader() callingClass.getClassLoader() } * </ul> * * @param className The name of the class to load * @param callingClass The Class object of the calling object * @throws ClassNotFoundException If the class cannot be found anywhere. */ public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException { try { return Thread.currentThread().getContextClassLoader().loadClass(className); } catch (ClassNotFoundException e) { try { return Class.forName(className); } catch (ClassNotFoundException ex) { try { return ClassLoaderUtils.class.getClassLoader().loadClass(className); } catch (ClassNotFoundException exc) { return callingClass.getClassLoader().loadClass(className); } } } }