List of usage examples for java.lang Class getClassLoader
@CallerSensitive
@ForceInline
public ClassLoader getClassLoader()
From source file:main.java.refinement_class.Useful.java
static public byte[] readFileAsBytes(Class c, String fileName) throws IOException { InputStream inStream = new java.io.BufferedInputStream(c.getClassLoader().getResourceAsStream(fileName)); ByteArrayOutputStream out = new ByteArrayOutputStream(); int nbytes = 0; byte[] buffer = new byte[100000]; try {/*from w w w. j a v a 2 s . com*/ while ((nbytes = inStream.read(buffer)) != -1) { out.write(buffer, 0, nbytes); } return out.toByteArray(); } finally { if (inStream != null) { inStream.close(); } if (out != null) { out.close(); } } }
From source file:api_proto3.TestElf.java
public static void setSlf4jTargetStream(Class<?> clazz, PrintStream stream) { try {//from w w w.ja v a 2 s . c o m //Log4jLogger log4Jlogger = (Log4jLogger) LoggerFactory.getLogger(clazz); Log4JLogger log4Jlogger = (Log4JLogger) LoggerFactory.getLogger(clazz); //Field field = clazz.getClassLoader().loadClass("org.apache.logging.slf4j.Log4jLogger").getDeclaredField("logger"); Field field = clazz.getClassLoader().loadClass("org.apache.commons.logging.impl.Log4JLogger") .getDeclaredField("logger"); field.setAccessible(true); Logger logger = (Logger) field.get(log4Jlogger); if (logger.getAppenders().containsKey("string")) { Appender appender = logger.getAppenders().get("string"); logger.removeAppender(appender); } logger.addAppender(new StringAppender("string", stream)); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.palantir.leader.proxy.AwaitingLeadershipProxy.java
@SuppressWarnings("unchecked") public static <T> T newProxyInstance(Class<T> interfaceClass, Supplier<T> delegateSupplier, LeaderElectionService leaderElectionService) { AwaitingLeadershipProxy proxy = new AwaitingLeadershipProxy(delegateSupplier, leaderElectionService, interfaceClass);//from w ww. j a va2 s .c om proxy.tryToGainLeadership(); return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] { interfaceClass, Closeable.class }, proxy); }
From source file:com.tesora.dve.common.PEFileUtils.java
/** * Helper function to load a file from the classpath relative to the root * classpath and return it as an <code>InputStream</code>. * //from w w w. java 2 s. c o m * @param testClass * <code>Class</code> of caller * @param fileName * name of file to load * @return <code>InputStream</code> of file * @throws PEException * if file cannot be located */ public static InputStream getClasspathResourceStream(Class<?> testClass, String fileName) throws PEException { InputStream is = testClass.getClassLoader().getResourceAsStream(fileName); validateFileResource(fileName, is); return is; }
From source file:com.mani.cucumber.ReflectionUtils.java
public static <T> Class<T> loadClass(Class<?> base, String name) { return loadClass(base.getClassLoader(), name); }
From source file:org.apache.hama.bsp.BSPJob.java
private static String findContainingJar(Class<?> my_class) { ClassLoader loader = my_class.getClassLoader(); String class_file = my_class.getName().replaceAll("\\.", "/") + ".class"; try {/*from w w w . j a v a 2 s . c o m*/ for (Enumeration<URL> itr = loader.getResources(class_file); itr.hasMoreElements();) { URL url = itr.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:org.apache.hive.hcatalog.templeton.tool.TempletonUtils.java
/** * Find a jar that contains a class of the same name and which * file name matches the given pattern.//from w w w .j ava2 s . co m * * @param clazz the class to find. * @param fileNamePattern regex pattern that must match the jar full path * @return a jar file that contains the class, or null */ public static String findContainingJar(Class<?> clazz, String fileNamePattern) { ClassLoader loader = clazz.getClassLoader(); String classFile = clazz.getName().replaceAll("\\.", "/") + ".class"; try { for (final Enumeration<URL> itr = loader.getResources(classFile); itr.hasMoreElements();) { final URL url = itr.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (fileNamePattern == null || toReturn.matches(fileNamePattern)) { toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:org.blocks4j.reconf.client.proxy.ConfigurationRepositoryFactory.java
private static synchronized <T> T newInstance(Class<T> arg, ConfigurationRepositoryElement repo) { ConfigurationRepositoryFactory factory = new ConfigurationRepositoryFactory(); ConfigurationRepositoryUpdater thread = new ConfigurationRepositoryUpdater(repo, ServiceLocator.defaultImplementation, factory); Environment.addThreadToCheck(thread); thread.start();//www .ja va 2 s .co m return (T) Proxy.newProxyInstance(arg.getClassLoader(), new Class<?>[] { arg }, factory); }
From source file:com.tesora.dve.common.PEFileUtils.java
public static URL getClasspathURL(final Class<?> testClass, final String fileName) throws PEException { final URL resource = testClass.getClassLoader().getResource(fileName); validateFileResource(fileName, resource); return resource; }
From source file:fm.last.commons.io.LastFileUtils.java
/** * Searches for a file on local filesytem, classpath etc. * //w w w . j a v a 2 s . c om * @param fileName Name of file to find. * @param classToLoadFrom Class to use as a base for finding the file via it's classloader, if necessary. * @return The file if found on the file system. * @throws FileNotFoundException If the File could not be found. */ public static File getFile(String fileName, Class<?> classToLoadFrom) throws FileNotFoundException { File file = new File(fileName); // first try the path directly if (!file.exists()) { URL fileURL = classToLoadFrom.getResource(fileName);// next try the class's classpath if (fileURL == null) { fileURL = classToLoadFrom.getClassLoader().getResource(fileName);// next try the class' classloader's classpath if (fileURL == null) { fileURL = ClassLoader.getSystemClassLoader().getResource(fileName); // finally try the system classloader's // classpath if (fileURL == null) { throw new FileNotFoundException( "Could not find " + fileName + " on path, classpath " + "or system classpath"); } } } file = new File(fileURL.getFile()); } log.debug("Path to file located is " + file.getAbsolutePath()); return file; }