List of usage examples for java.lang Class getResource
@CallerSensitive
public URL getResource(String name)
From source file:net.servicefixture.util.ReflectionUtils.java
/** * Finds all the concrete subclasses for given class in the the SAME JAR * file where the baseClass is loaded from. * // w ww . ja v a 2s. c o m * @param baseClass * the base class */ public static Class[] findSubClasses(Class baseClass) { String packagePath = "/" + baseClass.getPackage().getName().replace('.', '/'); URL url = baseClass.getResource(packagePath); if (url == null) { return new Class[0]; } List<Class> derivedClasses = new ArrayList<Class>(); try { URLConnection connection = url.openConnection(); if (connection instanceof JarURLConnection) { JarFile jarFile = ((JarURLConnection) connection).getJarFile(); Enumeration e = jarFile.entries(); while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry) e.nextElement(); String entryName = entry.getName(); if (entryName.endsWith(".class")) { String clazzName = entryName.substring(0, entryName.length() - 6); clazzName = clazzName.replace('/', '.'); try { Class clazz = Class.forName(clazzName); if (isConcreteSubclass(baseClass, clazz)) { derivedClasses.add(clazz); } } catch (Throwable ignoreIt) { } } } } else if (connection instanceof FileURLConnection) { File file = new File(url.getFile()); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { String filename = files[i].getName(); if (filename.endsWith(".class")) { filename = filename.substring(0, filename.length() - 6); String clazzname = baseClass.getPackage().getName() + "." + filename; try { Class clazz = Class.forName(clazzname); if (isConcreteSubclass(baseClass, clazz)) { derivedClasses.add(clazz); } } catch (Throwable ignoreIt) { } } } } } catch (IOException ignoreIt) { } return derivedClasses.toArray(new Class[derivedClasses.size()]); }
From source file:io.selendroid.builder.SelendroidServerBuilder.java
public static String getJarVersionNumber() { Class clazz = SelendroidStandaloneDriver.class; String className = clazz.getSimpleName() + ".class"; String classPath = clazz.getResource(className).toString(); if (!classPath.startsWith("jar")) { // Class not from JAR return "dev"; }//w w w. j av a2 s . co m String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF"; Manifest manifest = null; try { manifest = new Manifest(new URL(manifestPath).openStream()); } catch (Exception e) { return ""; } Attributes attr = manifest.getMainAttributes(); String value = attr.getValue("version"); return value; }
From source file:edu.stanford.epad.common.plugins.impl.ClassFinderTestUtils.java
/** * /* w w w . j av a 2 s. c om*/ * @param clazz Class * @return String */ public static String readJarManifestForClass(Class<?> clazz) { StringBuilder sb = new StringBuilder(); InputStream manifestInputStream = null; try { String className = clazz.getSimpleName() + ".class"; String classPath = clazz.getResource(className).toString(); if (!classPath.startsWith("jar")) { // Class not from JAR return "Class " + className + " not from jar file."; } String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF"; manifestInputStream = new URL(manifestPath).openStream(); Manifest manifest = new Manifest(manifestInputStream); Attributes attr = manifest.getMainAttributes(); // String value = attr.getValue("Manifest-Version"); Set<Object> keys = attr.keySet(); for (Object currKey : keys) { String currValue = attr.getValue(currKey.toString()); if (currValue != null) { sb.append(currKey.toString()).append(" : ").append(currValue).append("\n"); } else { sb.append(currKey.toString()).append(": Didn't have a value"); } } } catch (Exception e) { logger.warning("Failed to read manifest for " + clazz.getSimpleName(), e); } finally { IOUtils.closeQuietly(manifestInputStream); } return sb.toString(); }
From source file:com.liferay.sync.engine.SyncSystemTest.java
protected static Path getResourceFilePath(String name) throws Exception { Class<?> clazz = SyncSystemTest.class; URL url = clazz.getResource(name); return Paths.get(url.toURI()); }
From source file:com.tesora.dve.common.PEFileUtils.java
public static URL getResourceURL(final Class<?> testClass, final String fileName) throws PEException { final URL resource = testClass.getResource(fileName); validateFileResource(fileName, resource); return resource; }
From source file:com.googlecode.blaisemath.app.MenuConfig.java
/** * Reads menu components from file./*from w ww . j ava 2 s . c o m*/ * @param cls class with associated resource * @return map, where values are either nested maps, or lists of strings representing actions * @throws IOException if there's an error reading from the config file */ public static Map<String, Object> readConfig(Class cls) throws IOException { String path = "resources/" + cls.getSimpleName() + MENU_SUFFIX + ".yml"; URL rsc = cls.getResource(path); checkNotNull(rsc, "Failed to locate " + path); ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); return mapper.readValue(rsc, Map.class); }
From source file:eu.annocultor.common.Utils.java
private static InputStream readResourceFromPackage(Class theClass, String resource) throws IOException { if (theClass.getResource(resource) == null) throw new NullPointerException( "Failed to find resource for class " + theClass.getName() + " resource " + resource); String fileName = theClass.getResource(resource).getFile(); if (fileName == null) throw new NullPointerException("Failed to generate file for resource for class " + theClass.getName() + " resource " + resource); byte[] file = readResourceFile(new File(fileName)); return new ByteArrayInputStream(file); }
From source file:com.redhat.rhn.testing.TestUtils.java
/** * method to find a file relative to the calling class. primarily * useful when writing tests that need access to external data. * this lets you put the data relative to the test class file. * * @param path the path, relative to caller's location * @return URL a URL referencing the file * @throws ClassNotFoundException if the calling class can not be found * (i.e., should not happen)/* ww w . j a v a 2 s.co m*/ * @throws IOException if the specified file in an archive (eg. jar) and * it cannot be copied to a temporary location */ public static URL findTestData(String path) throws ClassNotFoundException, IOException { Throwable t = new Throwable(); StackTraceElement[] ste = t.getStackTrace(); String className = ste[1].getClassName(); Class clazz = Class.forName(className); URL ret = clazz.getResource(path); if (ret.toString().contains("!")) { // file is from an archive String tempPath = "/tmp/" + filePrefix + ret.hashCode(); InputStream input = clazz.getResourceAsStream(path); OutputStream output = new FileOutputStream(tempPath); IOUtils.copy(input, output); return new File(tempPath).toURI().toURL(); } return ret; }
From source file:PropertiesUtil.java
public static void store(Properties properties, Class relativeClass, String relativeFileName) throws IOException { if (relativeClass == null) throw new IllegalArgumentException("Relative Class is not set."); if (relativeFileName == null) { throw new IllegalArgumentException("Relative File Name is not set."); } else {/* w ww . j av a2s. c o m*/ String className = relativeClass.getName().substring(relativeClass.getName().lastIndexOf(".") + 1); URL url = relativeClass.getResource(String.valueOf(String.valueOf(className)).concat(".class")); String fileName = url.getFile(); fileName = String.valueOf(fileName.substring(1, fileName.length() - className.length() - 6)) + String.valueOf(relativeFileName); store(properties, fileName); return; } }
From source file:io.selendroid.standalone.builder.SelendroidServerBuilder.java
public static String getJarVersionNumber() { Class clazz = SelendroidStandaloneDriver.class; String className = clazz.getSimpleName() + ".class"; String classPath = clazz.getResource(className).toString(); String version = ""; if (!classPath.startsWith("jar")) { // Class not from JAR if (classPath.startsWith("file") && classPath.contains("target")) { try { version = getVersionFromPom(classPath.substring(5, classPath.lastIndexOf("target"))); } catch (Exception e) { e.printStackTrace();/*from w ww . j a va 2 s.c om*/ return ""; } } else return "dev"; } else { try { version = getVersionFromManifest(classPath); } catch (IOException e) { return ""; } } return version; }