List of usage examples for java.lang Class getClassLoader
@CallerSensitive
@ForceInline
public ClassLoader getClassLoader()
From source file:Main.java
/** * Loads an image from within a jar or from the file system. * //from w w w .j a va 2s .c o m * @param fileName the image path. * @param packageClass a class within the package where the image can be found * @return an initialised image-icon or null, when no image could be found. */ public static Image loadIcon(String fileName, Class packageClass) { try { ClassLoader classLoader = packageClass.getClassLoader(); InputStream is = classLoader.getResourceAsStream(fileName); if (is == null) { File file = new File(fileName); if (file.exists()) { is = new FileInputStream(file); } else { return null; } } return ImageIO.read(is); } catch (IOException e) { System.err.println("Unable to load image [" + fileName + "]"); e.printStackTrace(); return null; } }
From source file:Main.java
private static Class<?> loadClassViaClasses(String fqcn, Class<?>[] classes) { if (classes != null) { for (Class<?> c : classes) { ClassLoader loader = c.getClassLoader(); if (loader != null) { try { return loader.loadClass(fqcn); } catch (ClassNotFoundException e) { // move on to try the next class loader }/*from ww w . ja va2 s . c o m*/ } } } return null; }
From source file:com.wavemaker.commons.util.WMUtils.java
public static boolean isSharedLibSetup() { if (isSharedLibSetup == null) { Class<Assert> klass = Assert.class; if (klass.getClassLoader() == WMUtils.class.getClassLoader()) { isSharedLibSetup = false;/*ww w .j av a2s . c om*/ logger.info("Using classes from the webapp class loader {}", WMUtils.class.getClassLoader()); } else { isSharedLibSetup = true; logger.info("Using classes from the jars of the shared library"); } } return isSharedLibSetup; }
From source file:de.uni.bremen.monty.moco.CompileFilesBaseTest.java
protected static Collection<File> getFiles4Filter(String folderPath, String[] filter) throws URISyntaxException { Class aClass = CompileTestProgramsTest.class; ClassLoader classLoader = aClass.getClassLoader(); File testprogramFolder = new File(classLoader.getResource(folderPath).toURI()); return FileUtils.listFiles(testprogramFolder, filter, true); }
From source file:com.betfair.cougar.codegen.FileUtil.java
/** * Copy the given resource to the given file. * * @param resourceName name of resource to copy * @param destination file/*from w ww .j a v a 2 s.c o m*/ */ public static void resourceToFile(String resourceName, File dest, Class src) { InputStream is = null; OutputStream os = null; try { is = src.getClassLoader().getResourceAsStream(resourceName); if (is == null) { throw new RuntimeException("Could not load resource: " + resourceName); } dest.getParentFile().mkdirs(); os = new FileOutputStream(dest); IOUtils.copy(is, os); } catch (Exception e) { throw new RuntimeException( "Error copying resource '" + resourceName + "' to file '" + dest.getPath() + "': " + e, e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
From source file:com.cloudera.sqoop.util.Jars.java
/** * Return the jar file path that contains a particular class. * Method mostly cloned from o.a.h.mapred.JobConf.findContainingJar(). *///from w ww . j a v a2 s. c o m public static String getJarPathForClass(Class<? extends Object> classObj) { ClassLoader loader = classObj.getClassLoader(); String classFile = classObj.getName().replaceAll("\\.", "/") + ".class"; try { for (Enumeration<URL> itr = loader.getResources(classFile); itr.hasMoreElements();) { URL url = (URL) itr.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } // URLDecoder is a misnamed class, since it actually decodes // x-www-form-urlencoded MIME type rather than actual // URL encoding (which the file path has). Therefore it would // decode +s to ' 's which is incorrect (spaces are actually // either unencoded or encoded as "%20"). Replace +s first, so // that they are kept sacred during the decoding process. toReturn = toReturn.replaceAll("\\+", "%2B"); toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:Main.java
/** * Compute the root directory of this maven project. This will result in the * same directory no matter if executed from Eclipse, this maven project root or * any parent maven pom directory. //from w w w .ja v a 2 s. c o m * * @param anyTestClass Any test class *local* to the maven project, i.e that * only exist in this maven project. * * @param child The file that should be * @return The root directory of this maven project. */ public static File computeMavenProjectRoot(Class<?> anyTestClass) { final String clsUri = anyTestClass.getName().replace('.', '/') + ".class"; final URL url = anyTestClass.getClassLoader().getResource(clsUri); final String clsPath = url.getPath(); // located in ./target/test-classes or ./eclipse-out/target final File target_test_classes = new File(clsPath.substring(0, clsPath.length() - clsUri.length())); // get parent's parent return target_test_classes.getParentFile().getParentFile(); }
From source file:org.jtheque.i18n.I18NResourceFactory.java
/** * Construct a I18NResource from the path using the class (with the class loader) to load it. * * @param classz The class to get the class loader. * @param path The resource path./* w w w . j ava2 s . c o m*/ * * @return The I18NResource corresponding to the given resource. */ public static I18NResource fromResource(Class<?> classz, String path) { Resource resource = new InputStreamResource(classz.getClassLoader().getResourceAsStream(path)); return new I18NResourceImpl(path.substring(path.lastIndexOf('/')), resource); }
From source file:com.palantir.leader.proxy.ToggleableExceptionProxy.java
@SuppressWarnings("unchecked") public static <T> T newProxyInstance(Class<T> interfaceClass, T delegate, AtomicBoolean throwException, Exception exception) {/*from ww w.j a v a2 s . c o m*/ return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] { interfaceClass }, new ToggleableExceptionProxy(delegate, throwException, exception)); }
From source file:io.lavagna.common.QueryFactory.java
@SuppressWarnings("unchecked") private static <T> T from(final Class<T> clazz, final String activeDb, final NamedParameterJdbcTemplate jdbc) { return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, new InvocationHandler() { @Override/*from ww w . ja va2 s . c o m*/ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { QueryTypeAndQuery qs = extractQueryAnnotation(clazz, activeDb, method); return qs.type.apply(qs.query, jdbc, method, args); } }); }