List of usage examples for java.lang Class getClassLoader
@CallerSensitive
@ForceInline
public ClassLoader getClassLoader()
From source file:org.eclipse.mylyn.commons.sdk.util.CommonTestUtil.java
public static File getFile(Object source, String filename) throws IOException { Class<?> clazz = (source instanceof Class<?>) ? (Class<?>) source : source.getClass(); if (Platform.isRunning()) { ClassLoader classLoader = clazz.getClassLoader(); try {// w ww .j a v a2 s . c om if (isOsgiVersion310orNewer(classLoader)) { return checkNotNull(getFileFromClassLoader4Luna(filename, classLoader)); } else { return checkNotNull(getFileFromClassLoaderBeforeLuna(filename, classLoader)); } } catch (Exception e) { AssertionFailedError exception = new AssertionFailedError( NLS.bind("Could not locate {0} using classloader for {1}", filename, clazz)); exception.initCause(e); throw exception; } } else { return getFileFromNotRunningPlatform(filename, clazz); } }
From source file:nl.nn.adapterframework.util.ClassUtils.java
static private URL getResourceURL(ClassLoader classLoader, Class klass, String resource) { resource = Misc.replace(resource, "%20", " "); URL url = null;// ww w . j av a 2s . c om if (classLoader == null) { if (klass == null) { klass = ClassUtils.class; } classLoader = klass.getClassLoader(); } // Remove slash like Class.getResource(String name) is doing before // delegation to ClassLoader if (resource.startsWith("/")) { resource = resource.substring(1); } // first try to get the resource as a resource url = classLoader.getResource(resource); // then try to get it as a URL if (url == null) { try { url = new URL(Misc.replace(resource, " ", "%20")); } catch (MalformedURLException e) { log.debug("Could not find resource as URL [" + resource + "]: " + e.getMessage()); } } if (url == null) { log.warn("cannot find URL for resource [" + resource + "]"); } else { // Spaces must be escaped to %20. But ClassLoader.getResource(String) // has a bug in Java 1.3 and 1.4 and doesn't do this escaping. // See also: // // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4778185 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4785848 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4273532 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4496398 // // Escaping spaces to %20 if spaces are found. String urlString = url.toString(); if (urlString.indexOf(' ') >= 0 && !urlString.startsWith("jar:")) { urlString = Misc.replace(urlString, " ", "%20"); try { URL escapedURL = new URL(urlString); log.debug("resolved resource-string [" + resource + "] to URL [" + escapedURL.toString() + "]"); return escapedURL; } catch (MalformedURLException e) { log.warn("Could not find URL from space-escaped url [" + urlString + "], will use unescaped original version [" + url.toString() + "] "); } } } return url; }
From source file:Loader.java
/** Load a class. * //from w w w . j a v a 2 s. co m * @param loadClass * @param name * @param checkParents If true, try loading directly from parent classloaders. * @return Class * @throws ClassNotFoundException */ public static Class loadClass(Class loadClass, String name, boolean checkParents) throws ClassNotFoundException { ClassNotFoundException ex = null; Class c = null; ClassLoader loader = Thread.currentThread().getContextClassLoader(); while (c == null && loader != null) { try { c = loader.loadClass(name); } catch (ClassNotFoundException e) { if (ex == null) ex = e; } loader = (c == null && checkParents) ? loader.getParent() : null; } loader = loadClass == null ? null : loadClass.getClassLoader(); while (c == null && loader != null) { try { c = loader.loadClass(name); } catch (ClassNotFoundException e) { if (ex == null) ex = e; } loader = (c == null && checkParents) ? loader.getParent() : null; } if (c == null) { try { c = Class.forName(name); } catch (ClassNotFoundException e) { if (ex == null) ex = e; } } if (c != null) return c; throw ex; }
From source file:org.beangle.model.persist.hibernate.internal.ClassUtils.java
/** * Returns the first matching class from the given array, that doens't * belong to common libraries such as the JDK or OSGi API. Useful for * filtering OSGi services by type to prevent class cast problems. * <p/>//from ww w . ja v a2 s . c o m * No sanity checks are done on the given array class. * * @param classes * array of classes * @return a 'particular' (non JDK/OSGi) class if one is found. Else the * first available entry is returned. */ public static Class<?> getParticularClass(Class<?>[] classes) { boolean hasSecurity = (System.getSecurityManager() != null); for (int i = 0; i < classes.length; i++) { final Class<?> clazz = classes[i]; ClassLoader loader = null; if (hasSecurity) { loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { public ClassLoader run() { return clazz.getClassLoader(); } }); } else { loader = clazz.getClassLoader(); } // quick boot/system check if (loader != null) { // consider known loaders if (!knownNonOsgiLoadersSet.contains(loader)) { return clazz; } } } return (ObjectUtils.isEmpty(classes) ? null : classes[0]); }
From source file:fr.inria.atlanmod.neoemf.core.PersistentEObjectAdapter.java
/** * Create an adapter for the given {@code object} in a specific {@code type}. * * @param adaptableObject the object to adapt * @param adapterType the class in which the object must be adapted * * @return an adapted object in the given {@code type} *///from w w w .ja v a 2s . c o m private static Object createAdapter(Object adaptableObject, Class<?> adapterType) { /* * Compute the interfaces that the proxy has to implement * These are the current interfaces + PersistentEObject */ List<Class<?>> interfaces = ClassUtils.getAllInterfaces(adaptableObject.getClass()); interfaces.add(PersistentEObject.class); // Create the proxy Enhancer proxy = new Enhancer(); /* * Use the ClassLoader of the type, otherwise it will cause OSGI troubles (like project trying to * create an PersistentEObject while it does not have a dependency to NeoEMF core) */ proxy.setClassLoader(adapterType.getClassLoader()); proxy.setSuperclass(adaptableObject.getClass()); proxy.setInterfaces(interfaces.toArray(new Class[interfaces.size()])); proxy.setCallback(new PersistentEObjectProxyHandler()); return proxy.create(); }
From source file:org.cloudata.core.common.ipc.CRPC.java
public static Object getOnewayProxy(Class<?> protocol, long clientVersion, InetSocketAddress addr, CloudataConf conf) throws IOException { SocketFactory factory = NetUtils.getDefaultSocketFactory(conf); getProxy(protocol, clientVersion, addr, conf, factory); CVersionedProtocol proxy = (CVersionedProtocol) Proxy.newProxyInstance(protocol.getClassLoader(), new Class[] { protocol }, new OnewayInvoker(addr, conf, factory)); return proxy; }
From source file:org.eclipse.wb.internal.core.utils.state.EditorState.java
/** * @return <code>true</code> if given {@link Class} was loaded using this {@link EditorState}. *///from w w w . j a va2 s.co m private static boolean isLoadedFrom(Class<?> clazz, ClassLoader loader) { // check for this ClassLoader if (clazz.getClassLoader() == loader) { return true; } // check parts of CompositeClassLoader if (loader instanceof CompositeClassLoader) { CompositeClassLoader compositeClassLoader = (CompositeClassLoader) loader; for (Iterator<?> I = compositeClassLoader.getClassLoaders().iterator(); I.hasNext();) { ClassLoader classLoader = (ClassLoader) I.next(); if (isLoadedFrom(clazz, classLoader)) { return true; } } } // generic case return false; }
From source file:org.apache.hadoop.ipc.chinamobile.RPC.java
/** Construct a client-side proxy object that implements the named protocol, * talking to a server at the named address. */ public static VersionedProtocol getProxy(Class<?> protocol, long clientVersion, InetSocketAddress addr, UserGroupInformation ticket, Configuration conf, SocketFactory factory) throws IOException { VersionedProtocol proxy = (VersionedProtocol) Proxy.newProxyInstance(protocol.getClassLoader(), new Class[] { protocol }, new Invoker(addr, ticket, conf, factory)); long serverVersion = proxy.getProtocolVersion(protocol.getName(), clientVersion); if (serverVersion == clientVersion) { return proxy; } else {//from ww w . j a v a 2s. c o m throw new VersionMismatch(protocol.getName(), clientVersion, serverVersion); } }
From source file:org.rosenvold.spring.convention.ConventionBeanFactory.java
/** * Resolve the given autowiring value against the given required type, * e.g. an {@link org.springframework.beans.factory.ObjectFactory} value to its actual object result. * * @param autowiringValue the value to resolve * @param requiredType the type to assign the result to * @return the resolved value/*from w ww. j a v a 2s . c o m*/ */ public static Object resolveAutowiringValue(Object autowiringValue, Class requiredType) { if (autowiringValue instanceof ObjectFactory && !requiredType.isInstance(autowiringValue)) { ObjectFactory factory = (ObjectFactory) autowiringValue; if (autowiringValue instanceof Serializable && requiredType.isInterface()) { autowiringValue = Proxy.newProxyInstance(requiredType.getClassLoader(), new Class[] { requiredType }, new ObjectFactoryDelegatingInvocationHandler(factory)); } else { return factory.getObject(); } } return autowiringValue; }
From source file:net.ymate.platform.commons.util.ResourceUtils.java
/** * //from www .j a va2s . c o m * @param className * @param callingClass * @return * @throws ClassNotFoundException */ public static Class<?> loadClass(String className, Class<?> callingClass) throws ClassNotFoundException { Class<?> _targetClass = null; try { _targetClass = Thread.currentThread().getContextClassLoader().loadClass(className); } catch (ClassNotFoundException e) { try { _targetClass = Class.forName(className); } catch (ClassNotFoundException ex) { try { _targetClass = ClassUtils.getDefaultClassLoader().loadClass(className); } catch (ClassNotFoundException exc) { _targetClass = callingClass.getClassLoader().loadClass(className); } } } return _targetClass; }