Java Utililty Methods Class Find

List of utility methods to do Class Find

Description

The list of methods to do Class Find are organized into topic(s).

Method

ListfindAllClasses(String packageName)
find All Classes
List<Class> classes = new ArrayList<Class>();
File directory = null;
try {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader == null)
        throw new ClassNotFoundException("Can't get class loader.");
    String path = '/' + packageName.replace('.', '/');
    URL resource = classLoader.getResource(path);
...
ListfindClass(String packageName, Class[] superClass)
find Class
List<Class> classes = new ArrayList<Class>();
if (packageName == null || packageName.trim().length() == 0 || superClass == null
        || superClass.length == 0) {
    return classes;
List<String> files = new ArrayList<String>();
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
try {
...
URLfindClassBase(Class clazz)
Find the classpath for the particular class
String resource = clazz.getName().replace('.', '/') + ".class";
return findResourceBase(resource, clazz.getClassLoader());
TreeSetfindClasses(String directory, String packageName)
Recursive method used to find all classes in a given directory and subdirs.
TreeSet<String> classes = new TreeSet<String>();
if (directory.startsWith("file:") && directory.contains("!")) {
    String[] split = directory.split("!");
    URL jar = new URL(split[0]);
    ZipInputStream zip = new ZipInputStream(jar.openStream());
    ZipEntry entry = null;
    while ((entry = zip.getNextEntry()) != null) {
        if (entry.getName().endsWith(".class")) {
...
TreeSetfindClasses(String path, String packageName)
find Classes
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")) {
...
List>findClassesAnnotatedWith(String packageName, Class a)
find Classes Annotated With
List<Class<?>> classes = new ArrayList<Class<?>>();
recurse(classes, packageName, a);
return classes;
Set>findClassesAssignableFrom(String packageName, Class assignableFrom)
find Classes Assignable From
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Set<Class<T>> classes = new HashSet<Class<T>>();
String path = packageName.replace('.', '/');
URL resource = loader.getResource(path);
if (resource != null) {
    String filePath = resource.getFile();
    if (filePath != null && new File(filePath).isDirectory()) {
        for (String file : new File(filePath).list()) {
...
StringfindClassLocation(Class context)
find Class Location
String jarRelatedPath = "/" + context.getName().replace(".", "/") + ".class";
URL location = context.getResource(jarRelatedPath);
String jarPath = "";
try {
    jarPath = URLDecoder.decode(location.getPath(), "UTF-8");
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
return jarPath.substring(0, jarPath.lastIndexOf("/") + 1);
FilefindClassOriginFile(Class cls)
find Class Origin File
try {
    final URL url = cls.getClassLoader().getResource(cls.getName().replace('.', '/') + ".class");
    final File file = new File(url.getFile()); 
    if (file.exists())
        return file;
} catch (Exception ex) {
try {
...
List>getAllAssignedClass(Class cls)
get All Assigned Class
List<Class<?>> classes = new ArrayList<Class<?>>();
for (Class<?> c : getClasses(cls)) {
    if (cls.isAssignableFrom(c) && !cls.equals(c)) {
        classes.add(c);
return classes;