1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
package com.praesentiae.tools;
import java.io.File;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public final class ClassTools
{
static private final Comparator<Class<?>> sortClassByClassName = new Comparator<Class<?>>() {
public int compare(Class<?> class1, Class<?> class2)
{
return ((Class<?>) class1).getName().compareTo(((Class<?>) class2).getName());
}
};
static public boolean areTheSame(Object obj1, Object obj2)
{
if (obj1 == null)
return (obj2 == null);
if (obj2 == null)
return false;
return getClassName(obj1).equals(getClassName(obj2));
}
static public String getClassName(Class<?> classname)
{
if (classname == null)
return "null";
String str = classname.getName();
return str.substring(str.lastIndexOf(".") + 1);
}
static public String getClassName(Object obj)
{
return (obj == null ? "null" : getClassName(obj.getClass()));
}
static public <T> List<Class<T>> searchAssignableFrom(Class<T> refClass)
{
URL url = (new ClassTools()).getClass().getResource("/");
List<Class<T>> classes = new ArrayList<Class<T>>(100);
recurseClassFinder("", classes, new File(url.getFile()));
for (int idx = classes.size() - 1; idx >= 0; idx--)
{
Class<T> theClass = classes.get(idx);
if (!refClass.isAssignableFrom(theClass))
classes.remove(idx);
}
Collections.sort(classes, sortClassByClassName);
return classes;
}
static public <T> List<T> searchFieldsByType(Class<?> classToSearch, Class<T> fieldToSearch) throws IllegalArgumentException, IllegalAccessException
{
List<T> list = new ArrayList<T>(20);
Field[] fields = classToSearch.getDeclaredFields();
Field field;
for (int idx = fields.length - 1; idx >= 0; idx--)
{
field = fields[idx];
if (fieldToSearch.isAssignableFrom(field.getType()))
{
field.setAccessible(true);
list.add((T) field.get(null));
}
}
return list;
}
static public <T> List<T> searchFieldsByType(Class<?> classToSearch, Class<T>[] fieldToSearch) throws IllegalArgumentException, IllegalAccessException
{
List<T> list = new ArrayList<T>(20);
for (int idx = 0; idx < fieldToSearch.length; idx++)
{
List<T> l = searchFieldsByType(classToSearch, fieldToSearch[idx]);
if (l != null && l.size() > 0)
list.addAll(l);
}
return list;
}
static private void recurseClassFinder(String prevPackageName, List classes, File directory)
{
File[] files = directory.listFiles();
String nextPackageNamePrefix = (StringTools.isEmpty(prevPackageName) ? "" : prevPackageName.trim() + ".");
for (int idx = files.length - 1; idx >= 0; idx--)
{
if (files[idx].isDirectory())
{
recurseClassFinder(nextPackageNamePrefix + files[idx].getName(), classes, files[idx]);
}
else if (files[idx].getName().toLowerCase().endsWith(".class"))
{
String filename = files[idx].getName();
try
{
Class theclass = Class.forName(nextPackageNamePrefix + filename.substring(0, filename.length() - 6));
classes.add(theclass);
}
catch (Exception ex)
{
}
}
}
}
} |
Partager