Java URL Connection findClasses(URL resource, String packageName, boolean annotated)

Here you can find the source of findClasses(URL resource, String packageName, boolean annotated)

Description

Recursive method used to find all classes in a given directory and subdirs.

License

Apache License

Parameter

Parameter Description
directory The base directory
packageName The package name for classes found inside the base directory
annotated a parameter

Exception

Parameter Description
ClassNotFoundException an exception
IOException an exception

Return

The classes

Declaration

private static List<Class<?>> findClasses(URL resource, String packageName, boolean annotated)
        throws ClassNotFoundException, IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    /**//from ww  w  .  ja va2 s. c  o  m
     * Recursive method used to find all classes in a given directory and
     * subdirs.
     * 
     * @param directory
     *            The base directory
     * @param packageName
     *            The package name for classes found inside the base directory
     * @param annotated
     * @return The classes
     * @throws ClassNotFoundException
     * @throws IOException
     */
    private static List<Class<?>> findClasses(URL resource, String packageName, boolean annotated)
            throws ClassNotFoundException, IOException {
        List<Class<?>> classes = new ArrayList<Class<?>>();
        File directory = new File(resource.getFile());
        if (directory.exists()) {
            findClassInDir(directory, packageName, classes, annotated);
        } else {
            JarURLConnection conn = (JarURLConnection) resource.openConnection();
            JarFile jarFile = conn.getJarFile();
            Enumeration<JarEntry> e = jarFile.entries();
            String prefix = packageName.replace('.', '/');
            while (e.hasMoreElements()) {
                JarEntry entry = e.nextElement();
                String entryname = entry.getName();
                if (entryname.startsWith(prefix) && !entry.isDirectory() && entryname.endsWith(".class")) {
                    String classname = entryname.substring(0, entryname.length() - 6);
                    Class<?> clazz = Class.forName(classname.replace('/', '.'));
                    if (!annotated || clazz.getAnnotations().length > 0) {
                        classes.add(clazz);
                    }
                }
            }
        }
        return classes;
    }

    private static void findClassInDir(File directory, String packageName, List<Class<?>> classes,
            boolean annotated) throws ClassNotFoundException {
        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                assert !file.getName().contains(".");
                findClassInDir(file, packageName + "." + file.getName(), classes, annotated);
            } else if (file.getName().endsWith(".class")) {
                Class<?> clazz = Class
                        .forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6));
                if (!annotated || clazz.getAnnotations().length > 0) {
                    classes.add(clazz);
                }
            }
        }
    }
}

Related

  1. createWebStartDirectory(String name, String jarUrl)
  2. doGet(String targetURL)
  3. doGet(String url, String charset)
  4. doPost(final URL url, final Map parameters, final boolean encode)
  5. doPost(String url, String params, String charset)
  6. findClasspathUrls(ClassLoader classLoader)
  7. findResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List resources)
  8. getAsStream(URL url)
  9. getBaseAuthInputStreamFromURL(String query, String basicAuthString)