/** * See {@link Proxy#getProxyClass(ClassLoader, Class...)} */ Class<?> getProxyClass(final Class<?>... classObjs) { IllegalArgumentException ex = null; for (ClassLoader classLoader : this.getClassLoaders()) { try { return Proxy.getProxyClass(classLoader, classObjs); } catch (SecurityException sex) { // Continue to next classloader } catch (IllegalArgumentException iaex) { ex = iaex; // Continue to next classloader } } if (ex != null) { throw ex; } return null; }
@Override public String toString() { final StringBuilder sb = new StringBuilder(getClass().getName()); sb.append("@").append(System.identityHashCode(this)).append("{"); sb.append(", excludeTCCL=").append(this.excludeTCCL); sb.append(", classLoaders=["); sb.append(this.getClassLoaders().stream().map(ClassLoader::toString).collect(joining(", "))); sb.append("]}"); return sb.toString(); }
public Class<?> forName(final String name) throws ClassNotFoundException { final boolean isDebugEnabled = logger.isTraceEnabled(); if (isDebugEnabled) { logger.trace("forName({})", name); } for (ClassLoader classLoader : this.getClassLoaders()) { if (isDebugEnabled) { logger.trace("forName trying: {}", classLoader); } try { Class<?> clazz = Class.forName(name, true, classLoader); if (clazz != null) { if (isDebugEnabled) { logger.trace("forName found by: {}", classLoader); } return clazz; } } catch (SecurityException | ClassNotFoundException e) { // try next classLoader } } throw new ClassNotFoundException(name); }
public URL getResource(final String name) { final boolean isDebugEnabled = logger.isTraceEnabled(); if (isDebugEnabled) { logger.trace("getResource({})", name); } for (ClassLoader classLoader : getClassLoaders()) { if (isDebugEnabled) { logger.trace("getResource trying: {}", classLoader); } try { URL url = classLoader.getResource(name); if (url != null) { if (isDebugEnabled) { logger.trace("getResource found by: {}", classLoader); } return url; } } catch (SecurityException e) { // try next classLoader } } return null; }
/** * Finds all the resources with the given name. This method will first search the class loader of * the context class for the resource before searching all other {@link ClassLoader}s. * * @param contextClass The class whose class loader will first be searched * @param name The resource name * @return An enumeration of {@link java.net.URL <tt>URL</tt>} objects for the resource. If no * resources could be found, the enumeration will be empty. Resources that the class * loader doesn't have access to will not be in the enumeration. * @throws IOException If I/O errors occur * @see ClassLoader#getResources(String) */ private Enumeration<URL> getResources(final Class<?> contextClass, final String name) throws IOException { final LinkedHashSet<URL> urls = new LinkedHashSet<URL>(); if (contextClass != null) { CollectionUtils.addAll(urls, contextClass.getClassLoader().getResources(name)); } for (ClassLoader classLoader : getClassLoaders()) { Enumeration<URL> resources = classLoader.getResources(name); if (resources != null && resources.hasMoreElements()) { CollectionUtils.addAll(urls, resources); } } return Collections.enumeration(urls); }