List of usage examples for java.lang Class getClassLoader
@CallerSensitive
@ForceInline
public ClassLoader getClassLoader()
From source file:org.bonitasoft.engine.io.IOUtil.java
public static byte[] getClassData(final Class<?> clazz) throws IOException { if (clazz == null) { final String message = "Class is null"; throw new IOException(message); }/*from w w w.ja va 2 s . c o m*/ final String resource = clazz.getName().replace('.', '/') + ".class"; byte[] data; try (InputStream inputStream = clazz.getClassLoader().getResourceAsStream(resource)) { if (inputStream == null) { throw new IOException( "Impossible to get stream from class: " + clazz.getName() + ", className= " + resource); } data = IOUtil.getAllContentFrom(inputStream); } return data; }
From source file:biz.bokhorst.xprivacy.Util.java
public static void logStack(XHook hook, int priority, boolean cl) { StringBuilder trace = new StringBuilder(); ClassLoader loader = Thread.currentThread().getContextClassLoader(); for (StackTraceElement ste : Thread.currentThread().getStackTrace()) { trace.append(ste.toString());// www .j a va 2 s . c om if (cl) try { Class<?> clazz = Class.forName(ste.getClassName(), false, loader); trace.append(" ["); trace.append(clazz.getClassLoader().toString()); trace.append("]"); } catch (ClassNotFoundException ignored) { } trace.append("\n"); } log(hook, priority, trace.toString()); }
From source file:org.apache.jxtadoop.ipc.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, PeerGroup pg, JxtaSocketAddress jsocka, UserGroupInformation ticket, Configuration conf, SocketFactory factory) throws IOException { VersionedProtocol proxy = (VersionedProtocol) Proxy.newProxyInstance(protocol.getClassLoader(), new Class[] { protocol }, new Invoker(pg, jsocka, ticket, conf, factory)); long serverVersion = proxy.getProtocolVersion(protocol.getName(), clientVersion); if (serverVersion == clientVersion) { return proxy; } else {//from w w w . ja va2s . c o m throw new VersionMismatch(protocol.getName(), clientVersion, serverVersion); } }
From source file:org.hibernate.internal.util.ReflectHelper.java
/** * Perform resolution of a class name.//w w w. ja v a 2 s . co m * <p/> * Here we first check the context classloader, if one, before delegating to * {@link Class#forName(String, boolean, ClassLoader)} using the caller's classloader * * @param name The class name * @param caller The class from which this call originated (in order to access that class's loader). * @return The class reference. * @throws ClassNotFoundException From {@link Class#forName(String, boolean, ClassLoader)}. */ public static Class classForName(String name, Class caller) throws ClassNotFoundException { try { ClassLoader classLoader = ClassLoaderHelper.getContextClassLoader(); if (classLoader != null) { return classLoader.loadClass(name); } } catch (Throwable ignore) { } return Class.forName(name, true, caller.getClassLoader()); }
From source file:org.eclipse.che.vfs.impl.fs.LocalFileSystemTest.java
private static void enableAssertion(Class<?> clazz) { clazz.getClassLoader().setPackageAssertionStatus(clazz.getPackage().getName(), true); }
From source file:Loader.java
public static ResourceBundle getResourceBundle(Class loadClass, String name, boolean checkParents, Locale locale) throws MissingResourceException { MissingResourceException ex = null; ResourceBundle bundle = null; ClassLoader loader = Thread.currentThread().getContextClassLoader(); while (bundle == null && loader != null) { try {/*from w w w. j av a 2s.c o m*/ bundle = ResourceBundle.getBundle(name, locale, loader); } catch (MissingResourceException e) { if (ex == null) ex = e; } loader = (bundle == null && checkParents) ? loader.getParent() : null; } loader = loadClass == null ? null : loadClass.getClassLoader(); while (bundle == null && loader != null) { try { bundle = ResourceBundle.getBundle(name, locale, loader); } catch (MissingResourceException e) { if (ex == null) ex = e; } loader = (bundle == null && checkParents) ? loader.getParent() : null; } if (bundle == null) { try { bundle = ResourceBundle.getBundle(name, locale); } catch (MissingResourceException e) { if (ex == null) ex = e; } } if (bundle != null) return bundle; throw ex; }
From source file:gdt.data.grain.Support.java
/** * Get class resource as input stream. /*ww w . j av a 2 s .c o m*/ * @param handler the class * @param resource$ the resource name. * @return input stream. */ public static InputStream getClassResource(Class<?> handler, String resource$) { try { InputStream is = handler.getResourceAsStream(resource$); if (is != null) { //System.out.println("Support:getClassResource:resource stream="+is.toString()); return is; } else { // System.out.println("Support:getClassResource:cannot get embedded resource stream for handler="+handler.getName()); ClassLoader classLoader = handler.getClassLoader(); //if(classLoader!=null) // System.out.println("Support:getClassResource:class loader="+classLoader.toString()); is = classLoader.getResourceAsStream(resource$); //if(is!=null) // System.out.println("Support:getClassResource:resourse stream="+is.toString()); //else // System.out.println("Support:getClassResource:cannot get resource stream"); String handler$ = handler.getName(); //System.out.println("Support:getClassResource:class="+handler$); String handlerName$ = handler.getSimpleName(); //System.out.println("Support:getClassResource:class name="+handlerName$); String handlerPath$ = handler$.replace(".", "/"); //System.out.println("Support:getClassResource:class path="+handlerPath$); String resourcePath$ = "src/" + handlerPath$.replace(handlerName$, resource$); //System.out.println("Support:getClassResource:resource path="+resourcePath$); ClassLoader classloader = Thread.currentThread().getContextClassLoader(); URL resourceUrl = classloader.getResource(resourcePath$); if (resourceUrl != null) { //System.out.println("Support:getClassResource:resource URL="+resourceUrl.toString()); return resourceUrl.openStream(); } else { //System.out.println("Support:getClassResource:cannot get resource URL"); } } } catch (Exception e) { Logger.getLogger(gdt.data.grain.Support.class.getName()).severe(e.toString()); } return null; }
From source file:org.ebayopensource.turmeric.plugins.maven.AbstractTurmericMojo.java
public static String getJarLocationOfClass(Class<?> clazz) { try {//from w ww .j a v a 2 s . co m String classpath = clazz.getName().replace('.', '/') + ".class"; URL resource = clazz.getClassLoader().getResource(classpath); if (resource != null) { String uri = resource.toExternalForm(); int idx = uri.lastIndexOf("!"); if (idx > 0) { uri = uri.substring(0, idx); return uri; } else if (uri.endsWith(classpath)) { return uri.substring(0, uri.length() - classpath.length()); } else { return uri; } } } catch (Exception ignore) { /* ignore */ } return "<unknown>"; }
From source file:com.neuronrobotics.bowlerstudio.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//from www. jav a2s. c om * class to locate * @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:lapin.load.Loader.java
static private Subr _toSubr(Symbol name, Class clazz, Env env) throws InstantiationException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (Logger.debuglevelp(env)) { Logger.debug("[toSubr] name : ~S", Lists.list(name), env); Logger.debug("[toSubr] class : ~S", Lists.list(clazz), env); Logger.debug("[toSubr] loader: ~S", Lists.list(clazz.getClassLoader()), env); }//from w w w .j ava 2s. c o m Constructor c = clazz.getConstructor(CONSTRUCTOR_PARAM_TYPES); return Data.subr(c.newInstance(new Object[] { env })); }