Loading classes from a set of files - java

I have a task to check a set of conditions for unknown set of classes from classpath. I want to scan it for classes, load each of them and perform my checks. Now I have a set of urls to class files and I try to use URLClassLoader. But to load a class I need to specify a fully qualified class name, but I don't have them (I have only file path). I don't think that building class name from class file path is relieble, is it a better way to do it?
Thanks!

I'd just parse the beginning of the class file, looking for "package" keyword and first occurrence of "class" keyword. Then, when you combine those two (packageName + "." + className), it should result in a proper class name.

I started a project once to automatically test classes found on the class path for run time exceptions, by calling constructors and methods reflectively with dodgy arguments like null, 0, 1, -1, "" etc.
That project has a class called Finder wich does roughly what you need:
static List<Class<?>> findClassesForPackage(String packagename, Report report) throws ClassNotFoundException {
// 'classes' will hold a list of directories matching the package name.
// There may be more than one if a package is split over multiple
// jars/paths
List<Class<?>> classes = new ArrayList<Class<?>>();
List<File> directories = new ArrayList<File>();
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
throw new ClassNotFoundException("Can't get class loader.");
}
// Ask for all resources for the path
String path = packagename.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
while (resources.hasMoreElements()) {
URL res = resources.nextElement();
if (res.getProtocol().equalsIgnoreCase("jar")) {
JarURLConnection conn = (JarURLConnection) res.openConnection();
JarFile jar = conn.getJarFile();
for (JarEntry entry : Collections.list(jar.entries())) {
if (entry.getName().startsWith(path) && entry.getName().endsWith(".class")
&& !entry.getName().contains("$")) {
String className = entry.getName().replace("/", ".").substring(0,
entry.getName().length() - 6);
LOG.debug("Adding JAR className " + className);
try {
Class<?> clazz = Class.forName(className);
classes.add(clazz);
report.addClass(className);
} catch (Throwable throwable) {
ParamSet params = new ParamSet();
params.addParamValue(new ParamValue(className, "fully qualified classname"));
report.addError(className, new Error("Class.forName()", params, throwable));
}
}
}
} else
directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8")));
}
} catch (NullPointerException e) {
throw new ClassNotFoundException(String.format("%s does not appear to be a valid package", packagename), e);
} catch (UnsupportedEncodingException e) {
throw new ClassNotFoundException(String.format("%s does not appear to be a valid package", packagename), e);
} catch (IOException e) {
throw new ClassNotFoundException(String.format("Could not get all resources for %s", packagename), e);
}
List<String> subPackages = new ArrayList<String>();
// For every directory identified capture all the .class files
for (File directory : directories) {
if (directory.exists()) {
// Get the list of the files contained in the package
File[] files = directory.listFiles();
for (File file : files) {
// add .class files to results
String fileName = file.getName();
if (file.isFile() && fileName.endsWith(".class")) {
// removes the .class extension
String className = packagename + '.' + fileName.substring(0, fileName.length() - 6);
LOG.debug("Adding FILE className " + className);
try {
Class<?> clazz = Class.forName(className);
classes.add(clazz);
report.addClass(className);
} catch (Throwable throwable) {
ParamSet params = new ParamSet();
params.addParamValue(new ParamValue(className, "fully qualified classname"));
report.addError(className, new Error("Class.forName()", params, throwable));
}
}
// keep track of subdirectories
if (file.isDirectory()) {
subPackages.add(packagename + "." + fileName);
}
}
} else {
throw new ClassNotFoundException(String.format("%s (%s) does not appear to be a valid package",
packagename, directory.getPath()));
}
}
// check all potential subpackages
for (String subPackage : subPackages) {
classes.addAll(findClassesForPackage(subPackage, report));
}
return classes;
}
You probably have to strip some code that does reporting etc.

Related

How can I get all Class files in a specific package in Java?

As the description says: how to get a list of all Java class files for a given package name.
I've seen many questions and ways here on SO and other sites to find all classes in a specific Java package. Most of the solutions didn't work for me. Sometimes they worked on Jar files but not on "plain" Java projects in a folder (like the way an IDE does it) or the other way around. So I put all those code snippets together and formed a solution which will work (for me) out of the box regardless if the code is inside a Jar file or in a plain folder structure.
It's really simple: you give the method getClassesInPackage the name of the package to inspect and you will get a list of all classes in this package. Currently no exception is "consumed" orderly.
Have fun with it! Here is the code:
public static final List<Class<?>> getClassesInPackage(String packageName) {
String path = packageName.replaceAll("\\.", File.separator);
List<Class<?>> classes = new ArrayList<>();
String[] classPathEntries = System.getProperty("java.class.path").split(
System.getProperty("path.separator")
);
String name;
for (String classpathEntry : classPathEntries) {
if (classpathEntry.endsWith(".jar")) {
File jar = new File(classpathEntry);
try {
JarInputStream is = new JarInputStream(new FileInputStream(jar));
JarEntry entry;
while((entry = is.getNextJarEntry()) != null) {
name = entry.getName();
if (name.endsWith(".class")) {
if (name.contains(path) && name.endsWith(".class")) {
String classPath = name.substring(0, entry.getName().length() - 6);
classPath = classPath.replaceAll("[\\|/]", ".");
classes.add(Class.forName(classPath));
}
}
}
} catch (Exception ex) {
// Silence is gold
}
} else {
try {
File base = new File(classpathEntry + File.separatorChar + path);
for (File file : base.listFiles()) {
name = file.getName();
if (name.endsWith(".class")) {
name = name.substring(0, name.length() - 6);
classes.add(Class.forName(packageName + "." + name));
}
}
} catch (Exception ex) {
// Silence is gold
}
}
}
return classes;
}
Here is #mythbu 's answer in Kotlin:
#Throws(Exception::class)
fun getClassesInPackage(packageName: String) = with(packageName.replace(".", File.separator)) {
System.getProperty("java.class.path")
.split(System.getProperty("path.separator").toRegex())
.flatMap { classpathEntry ->
if (classpathEntry.endsWith(".jar")) {
JarInputStream(FileInputStream(File(classpathEntry))).use { s ->
generateSequence { s.nextJarEntry }
.map { it.name }
.filter { this in it && it.endsWith(".class") }
.map { it.substring(0, it.length - 6) }
.map { it.replace('|', '.').replace('/', '.') }
.map { Class.forName(it) }
.toList()
}
} else {
File(classpathEntry, this).list()
?.asSequence()
?.filter { it.endsWith(".class") }
?.map { it.substring(0, it.length - 6) }
?.map { Class.forName("$packageName.$it") }
?.toList() ?: emptyList()
}
}
}

return class names not working in Jar

I have used this code to get a list of class names from a package:
private List<String> getClasses()
{
List<String> classes = new ArrayList<String>();
String packageName = "algorithm/impl";
URL directoryUrl = Thread.currentThread().getContextClassLoader().
getResource(packageName);
File directory = new File(directoryUrl.getFile());
if(directory.exists())
{
String [] files = directory.list();
for(String filename : files)
{
classes.add(filename.substring(0, filename.lastIndexOf(".")));
}
}
return classes;
}
but this does not work when the app is packaged as an executable jar file. Why?
You can make use of this class JarFile.
JarFile file = new JarFile("YourFileName.jar");
for (Enumeration<JarEntry> enum = file.entries(); enum.hasMoreElements();) {
JarEntry entry = enum.next();
System.out.println(entry.getName());
}
Or if you want to search for particular class inside your jar you can use ZipFile class.
JarFile jar = new JarFile(YourJarFile);
ZipEntry e = jar.getEntry(CLASS_FILE_TO_FIND);
if (e == null) {
e = jar.getJarEntry(CLASS_FILE_TO_FIND);
if (e != null) {
foundIn.add(f.getPath());
}
} else {
foundIn.add(f.getPath());
}

is it possible to scan the android classpath for annotations?

I want to scan the classpath for certain annotations in Android.
I have only found one solution to this problem: http://mindtherobot.com/blog/737/android-hacks-scan-android-classpath/
And as the author writes this solution works, but has several limitations. Are there any future-proof ways of doing this in android? Any libraries providing this functionality?
This works for me using android 3.0
public static <T extends Annotation> List<Class> getClassesAnnotatedWith(Class<T> theAnnotation){
// In theory, the class loader is not required to be a PathClassLoader
PathClassLoader classLoader = (PathClassLoader) Thread.currentThread().getContextClassLoader();
Field field = null;
ArrayList<Class> candidates = new ArrayList<Class>();
try {
field = PathClassLoader.class.getDeclaredField("mDexs");
field.setAccessible(true);
} catch (Exception e) {
// nobody promised that this field will always be there
Log.e(TAG, "Failed to get mDexs field", e);
}
DexFile[] dexFile = null;
try {
dexFile = (DexFile[]) field.get(classLoader);
} catch (Exception e) {
Log.e(TAG, "Failed to get DexFile", e);
}
for (DexFile dex : dexFile) {
Enumeration<String> entries = dex.entries();
while (entries.hasMoreElements()) {
// Each entry is a class name, like "foo.bar.MyClass"
String entry = entries.nextElement();
// Load the class
Class<?> entryClass = dex.loadClass(entry, classLoader);
if (entryClass != null && entryClass.getAnnotation(theAnnotation) != null) {
Log.d(TAG, "Found: " + entryClass.getName());
candidates.add(entryClass);
}
}
}
return candidates;
}
I also created one to determin if a class was derived from X
public static List<Class> getClassesSuperclassedOf(Class theClass){
// In theory, the class loader is not required to be a PathClassLoader
PathClassLoader classLoader = (PathClassLoader) Thread.currentThread().getContextClassLoader();
Field field = null;
ArrayList<Class> candidates = new ArrayList<Class>();
try {
field = PathClassLoader.class.getDeclaredField("mDexs");
field.setAccessible(true);
} catch (Exception e) {
// nobody promised that this field will always be there
Log.e(TAG, "Failed to get mDexs field", e);
}
DexFile[] dexFile = null;
try {
dexFile = (DexFile[]) field.get(classLoader);
} catch (Exception e) {
Log.e(TAG, "Failed to get DexFile", e);
}
for (DexFile dex : dexFile) {
Enumeration<String> entries = dex.entries();
while (entries.hasMoreElements()) {
// Each entry is a class name, like "foo.bar.MyClass"
String entry = entries.nextElement();
// Load the class
Class<?> entryClass = dex.loadClass(entry, classLoader);
if (entryClass != null && entryClass.getSuperclass() == theClass) {
Log.d(TAG, "Found: " + entryClass.getName());
candidates.add(entryClass);
}
}
}
return candidates;
}
enjoy - B

How to load a resource bundle from a file resource in Java?

I have a file called mybundle.txt in c:/temp -
c:/temp/mybundle.txt
How do I load this file into a java.util.ResourceBundle? The file is a valid resource bundle.
This does not seem to work:
java.net.URL resourceURL = null;
String path = "c:/temp/mybundle.txt";
java.io.File fl = new java.io.File(path);
try {
resourceURL = fl.toURI().toURL();
} catch (MalformedURLException e) {
}
URLClassLoader urlLoader = new URLClassLoader(new java.net.URL[]{resourceURL});
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle( path ,
java.util.Locale.getDefault(), urlLoader );
As long as you name your resource bundle files correctly (with a .properties extension), then this works:
File file = new File("C:\\temp");
URL[] urls = {file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle rb = ResourceBundle.getBundle("myResource", Locale.getDefault(), loader);
where "c:\temp" is the external folder (NOT on the classpath) holding the property files, and "myResource" relates to myResource.properties, myResource_fr_FR.properties, etc.
Credit to http://www.coderanch.com/t/432762/java/java/absolute-path-bundle-file
When you say it's "a valid resource bundle" - is it a property resource bundle? If so, the simplest way of loading it probably:
try (FileInputStream fis = new FileInputStream("c:/temp/mybundle.txt")) {
return new PropertyResourceBundle(fis);
}
1) Change the extension to properties (ex. mybundle.properties.)
2) Put your file into a jar and add it to your classpath.
3) Access the properties using this code:
ResourceBundle rb = ResourceBundle.getBundle("mybundle");
String propertyValue = rb.getString("key");
From the JavaDocs for ResourceBundle.getBundle(String baseName):
baseName - the base name of the
resource bundle, a fully qualified
class name
What this means in plain English is that the resource bundle must be on the classpath and that baseName should be the package containing the bundle plus the bundle name, mybundle in your case.
Leave off the extension and any locale that forms part of the bundle name, the JVM will sort that for you according to default locale - see the docs on java.util.ResourceBundle for more info.
For JSF Application
To get resource bundle prop files from a given file path to use them in a JSF app.
Set the bundle with URLClassLoader for a class that extends
ResourceBundle to load the bundle from the file path.
Specify the class at basename property of loadBundle tag.
<f:loadBundle basename="Message" var="msg" />
For basic implementation of extended RB please see the sample at Sample Customized Resource Bundle
/* Create this class to make it base class for Loading Bundle for JSF apps */
public class Message extends ResourceBundle {
public Messages (){
File file = new File("D:\\properties\\i18n");
ClassLoader loader=null;
try {
URL[] urls = {file.toURI().toURL()};
loader = new URLClassLoader(urls);
ResourceBundle bundle = getBundle("message", FacesContext.getCurrentInstance().getViewRoot().getLocale(), loader);
setParent(bundle);
} catch (MalformedURLException ex) { }
}
.
.
.
}
Otherwise, get the bundle from getBundle method but locale from others source like Locale.getDefault(), the new (RB)class may not require in this case.
If, like me, you actually wanted to load .properties files from your filesystem instead of the classpath, but otherwise keep all the smarts related to lookup, then do the following:
Create a subclass of java.util.ResourceBundle.Control
Override the newBundle() method
In this silly example, I assume you have a folder at C:\temp which contains a flat list of ".properties" files:
public class MyControl extends Control {
#Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
if (!format.equals("java.properties")) {
return null;
}
String bundleName = toBundleName(baseName, locale);
ResourceBundle bundle = null;
// A simple loading approach which ditches the package
// NOTE! This will require all your resource bundles to be uniquely named!
int lastPeriod = bundleName.lastIndexOf('.');
if (lastPeriod != -1) {
bundleName = bundleName.substring(lastPeriod + 1);
}
InputStreamReader reader = null;
FileInputStream fis = null;
try {
File file = new File("C:\\temp\\mybundles", bundleName);
if (file.isFile()) { // Also checks for existance
fis = new FileInputStream(file);
reader = new InputStreamReader(fis, Charset.forName("UTF-8"));
bundle = new PropertyResourceBundle(reader);
}
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(fis);
}
return bundle;
}
}
Note also that this supports UTF-8, which I believe isn't supported by default otherwise.
This worked for me very well. And it doesn't reload the bundle everytime. I tried to take some stats to load and reload the bundle from external file location.
File file = new File("C:\\temp");
URL[] urls = {file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle rb = ResourceBundle.getBundle("myResource", Locale.getDefault(), loader);
where "c:\temp" is the external folder (NOT on the classpath) holding the property files, and "myResource" relates to myResource.properties, myResource_fr_FR.properties, etc.
Note: If you have the same bundle name on your classpath then it will be picked up by default using this constructor of URLClassLoader.
Credit to http://www.coderanch.com/t/432762/java/java/absolute-path-bundle-file
Find some of the stats below, all time in ms.
I am not worried about the initial load time as that could be something with my workspace or code that I am trying to figure out but what I am trying to show is the reload took way lesser telling me its coming from memory.
Here some of the stats:
Initial Locale_1 load took 3486
Reload Locale_1 took 24
Reload Locale_1 took 23
Reload Locale_1 took 22
Reload Locale_1 took 15
Initial Locale_2 load took 870
Reload Locale_2 took 22
Reload Locale_2 took 18
Initial Locale_3 load took 2298
Reload Locale_3 took 8
Reload Locale_3 took 4
I would prefer to use the resourceboundle class to load the properties - just to get it done in one line instead of 5 lines code through stream, Properties class and load().
FYI ....
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
try {
/*** Type1 */
Properties props = new Properties();
String fileName = getServletContext().getRealPath("WEB-INF/classes/com/test/my.properties");
// stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
// stream = ClassLoader.getSystemResourceAsStream("WEB-INF/class/com/test/my.properties");
InputStream stream = getServletContext().getResourceAsStream("/WEB-INF/classes/com/test/my.properties");
// props.load(new FileInputStream(fileName));
props.load(stream);
stream.close();
Iterator keyIterator = props.keySet().iterator();
while(keyIterator.hasNext()) {
String key = (String) keyIterator.next();
String value = (String) props.getProperty(key);
System.out.println("key:" + key + " value: " + value);
}
/*** Type2: */
// Just get it done in one line by rb instead of 5 lines to load the properties
// WEB-INF/classes/com/test/my.properties file
// ResourceBundle rb = ResourceBundle.getBundle("com.test.my", Locale.ENGLISH, getClass().getClassLoader());
ResourceBundle rb = ResourceBundle.getBundle("com.ibm.multitool.customerlogs.ui.nl.redirect");
Enumeration<String> keys = rb.getKeys();
while(keys.hasMoreElements()) {
String key = keys.nextElement();
System.out.println(key + " - " + rb.getObject(key));
}
} catch (IOException e) {
e.printStackTrace();
throw new ServletException("Error loading config.", e);
} catch (Exception e) {
e.printStackTrace();
throw new ServletException("Error loading config.", e);
}
}
I think that you want the file's parent to be on the classpath, not the actual file itself.
Try this (may need some tweaking):
String path = "c:/temp/mybundle.txt";
java.io.File fl = new java.io.File(path);
try {
resourceURL = fl.getParentFile().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLClassLoader urlLoader = new URLClassLoader(new java.net.URL[]{resourceURL});
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("mybundle.txt",
java.util.Locale.getDefault(), urlLoader );
The file name should have .properties extension and the base directory should be in classpath. Otherwise it can also be in a jar which is in classpath
Relative to the directory in classpath the resource bundle can be specified with / or . separator. "." is preferred.
If you wanted to load message files for different languages, just use the
shared.loader=
of catalina.properties...
for more info, visit http://theswarmintelligence.blogspot.com/2012/08/use-resource-bundle-messages-files-out.html
This works for me:
File f = new File("some.properties");
Properties props = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
props.load(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
fis = null;
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
public class One {
private static One one = null;
Map<String, String> configParameter = Collections.synchronizedMap(new HashMap<String, String>());
private One() {
ResourceBundle rb = ResourceBundle.getBundle("System", Locale.getDefault());
Enumeration en = rb.getKeys();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String value = rb.getString(key);
configParameter.put(key, value);
}
}
public static One getInstance() {
if (one == null) {
one= new One();
}
return one;
}
public Map<String, String> getParameter() {
return configParameter;
}
public static void main(String[] args) {
String string = One.getInstance().getParameter().get("subin");
System.out.println(string);
}
}
ResourceBundle rb = ResourceBundle.getBundle("service"); //service.properties
System.out.println(rb.getString("server.dns")); //server.dns=http://....

How do I locate resources on the classpath in java? Specifically stuff that ends in .hbm.xml

How do I locate resources on the classpath in java?
Specifically stuff that ends in .hbm.xml.
My goal is to get a List of all resources on the classpath that end with ".hbm.xml".
You have to get a classloader, and test whether it's a URLClassLoader. If so, downcast and get its URLs. From there, open each as a JarFile and look at its entries. Apply a regex to each entry and see if it's one that interests you.
Clearly, this isn't fast. It's best to be given a name to be looked up in the classpath, perhaps listed in a standard file name in the META-INF directory of each classpath element, similar to the technique used by the ServiceProvider facility. Note that you can list all files with a given name on the classpath.
Method findClasses from our ClassLoaderUtil might be a good starting point to adapt to your needs.
public class ClassLoaderUtil {
/**
* Recursive method used to find all classes in a given path (directory or zip file url). Directories
* are searched recursively. (zip files are
* Adapted from http://snippets.dzone.com/posts/show/4831 and extended to support use of JAR files
*
* #param path The base directory or url from which to search.
* #param packageName The package name for classes found inside the base directory
* #param regex an optional class name pattern. e.g. .*Test
* #return The classes
*/
private static TreeSet<String> findClasses(String path, String packageName, Pattern regex) throws Exception {
TreeSet<String> classes = new TreeSet<String>();
if (path.startsWith("file:") && path.contains("!")) {
String[] split = path.split("!");
URL jar = new URL(split[0]);
ZipInputStream zip = new ZipInputStream(jar.openStream());
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
if (entry.getName().endsWith(".class")) {
String className = entry.getName().replaceAll("[$].*", "").replaceAll("[.]class", "").replace('/', '.');
if (className.startsWith(packageName) && (regex == null || regex.matcher(className).matches()))
classes.add(className);
}
}
}
File dir = new File(path);
if (!dir.exists()) {
return classes;
}
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
assert !file.getName().contains(".");
classes.addAll(findClasses(file.getAbsolutePath(), packageName + "." + file.getName(), regex));
} else if (file.getName().endsWith(".class")) {
String className = packageName + '.' + file.getName().substring(0, file.getName().length() - 6);
if (regex == null || regex.matcher(className).matches())
classes.add(className);
}
}
return classes;
}
public static <T> List<T> instances(Class<? extends T>[] classList) {
List<T> tList = new LinkedList<T>();
for(Class<? extends T> tClass : classList) {
try {
// Only try to instantiate real classes.
if(! Modifier.isAbstract(tClass.getModifiers()) && ! Modifier.isInterface(tClass.getModifiers())) {
tList.add(tClass.newInstance());
}
} catch (Throwable t) {
throw new RuntimeException(t.getMessage(), t);
}
}
return tList;
}
public static Class[] findByPackage(String packageName, Class isAssignableFrom) {
Class[] clazzes = getClassesInPackage(packageName, null);
if(isAssignableFrom == null) {
return clazzes;
} else {
List<Class> filteredList = new ArrayList<Class>();
for(Class clazz : clazzes) {
if(isAssignableFrom.isAssignableFrom(clazz))
filteredList.add(clazz);
}
return filteredList.toArray(new Class[0]);
}
}
/**
* Scans all classes accessible from the context class loader which belong to the given package and subpackages.
* Adapted from http://snippets.dzone.com/posts/show/4831 and extended to support use of JAR files
*
* #param packageName The base package
* #param regexFilter an optional class name pattern.
* #return The classes
*/
public static Class[] getClassesInPackage(String packageName, String regexFilter) {
Pattern regex = null;
if (regexFilter != null)
regex = Pattern.compile(regexFilter);
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
List<String> dirs = new ArrayList<String>();
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
dirs.add(resource.getFile());
}
TreeSet<String> classes = new TreeSet<String>();
for (String directory : dirs) {
classes.addAll(findClasses(directory, packageName, regex));
}
ArrayList<Class> classList = new ArrayList<Class>();
for (String clazz : classes) {
classList.add(Class.forName(clazz));
}
return classList.toArray(new Class[classes.size()]);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
MyClass.class.getClassLoader().getResourceAsStream("Person.hbm.xml") is one way to look for it.

Categories