是否有可能找到给定包中的所有类或接口?(快速看了一下e.g. Package,似乎没有。)


当前回答

你可以使用这个使用ClassLoader的method1。

/**
 * Scans all classes accessible from the context class loader which belong to the given package and subpackages.
 *
 * @param packageName The base package
 * @return The classes
 * @throws ClassNotFoundException
 * @throws IOException
 */
private static Class[] getClasses(String packageName)
        throws ClassNotFoundException, IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    assert classLoader != null;
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = classLoader.getResources(path);
    List<File> dirs = new ArrayList<File>();
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        dirs.add(new File(resource.getFile()));
    }
    ArrayList<Class> classes = new ArrayList<Class>();
    for (File directory : dirs) {
        classes.addAll(findClasses(directory, packageName));
    }
    return classes.toArray(new Class[classes.size()]);
}

/**
 * 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
 * @return The classes
 * @throws ClassNotFoundException
 */
private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException {
    List<Class> classes = new ArrayList<Class>();
    if (!directory.exists()) {
        return classes;
    }
    File[] files = directory.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            assert !file.getName().contains(".");
            classes.addAll(findClasses(file, packageName + "." + file.getName()));
        } else if (file.getName().endsWith(".class")) {
            classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
        }
    }
    return classes;
}

__________ 1 .该方法最初取自http://snippets.dzone.com/posts/show/4831,由互联网档案馆存档,如现在所示。该代码片段也可以在https://dzone.com/articles/get-all-classes-within-package上获得。

其他回答

这将扫描类加载器和所有父加载器,以查找jar文件和目录。 jar文件和由jar的类路径引用的目录也会被加载。

this code is testet with Java 8,11,18. on 8 everything works perfectly using the URLClassLoader and the getURLs() method. on 11 it works fine using reflections, but the JVM prints a warning on the stderr stream (not redirectible with System.setErr() with my JVM) on 18 the reflections are useless (throws NoSuchMethod/Field), and the only thing (where I know that it works) is to use the getResource() method. When the class loader loades the resources of the given package from the file system a simple path url is returned. When the class loader loades the resources from a jar a url like 'jar:file:[jar-path]!/[in-jar-path]' is returned.

我使用了答案https://stackoverflow.com/a/1157352/18252455(来自一个重复的问题),并添加了读取类路径和搜索目录url的功能。

/**
 * orig description:<br>
 * Scans all classloaders for the current thread for loaded jars, and then scans
 * each jar for the package name in question, listing all classes directly under
 * the package name in question. Assumes directory structure in jar file and class
 * package naming follow java conventions (i.e. com.example.test.MyTest would be in
 * /com/example/test/MyTest.class)
 * <p>
 * in addition this method also scans for directories, where also is assumed, that the classes are
 * placed followed by the java conventions. (i.e. <code>com.example.test.MyTest</code> would be in
 * <code>directory/com/example/test/MyTest.class</code>)
 * <p>
 * this method also reads the jars Class-Path for other jars and directories. for the jars and
 * directories referred in the jars are scanned with the same rules as defined here.<br>
 * it is ensured that no jar/directory is scanned exactly one time.
 * <p>
 * if {@code bailError} is <code>true</code> all errors will be wrapped in a
 * {@link RuntimeException}
 * and then thrown.<br>
 * a {@link RuntimeException} will also be thrown if something unexpected happens.<br>
 * 
 * @param packageName
 *            the name of the package for which the classes should be searched
 * @param allowSubPackages
 *            <code>true</code> is also classes in sub packages should be found
 * @param loader
 *            the {@link ClassLoader} which should be used to find the URLs and to load classes
 * @param bailError
 *            if all {@link Exception} should be re-thrown wrapped in {@link RuntimeException} and
 *            if a {@link RuntimeException} should be thrown, when something is not as expected.
 * @see https://stackoverflow.com/questions/1156552/java-package-introspection
 * @see https://stackoverflow.com/a/1157352/18252455
 * @see https://creativecommons.org/licenses/by-sa/2.5/
 * @see https://creativecommons.org/licenses/by-sa/2.5/legalcode
 */
public static Set <Class <?>> tryGetClassesForPackage(String packageName, boolean allowSubPackages, ClassLoader loader, boolean bailError) {
    Set <URL> jarUrls = new HashSet <URL>();
    Set <Path> directorys = new HashSet <Path>();
    findClassPools(loader, jarUrls, directorys, bailError, packageName);
    Set <Class <?>> jarClasses = findJarClasses(allowSubPackages, packageName, jarUrls, directorys, loader, bailError);
    Set <Class <?>> dirClasses = findDirClasses(allowSubPackages, packageName, directorys, loader, bailError);
    jarClasses.addAll(dirClasses);
    return jarClasses;
}

private static Set <Class <?>> findDirClasses(boolean subPackages, String packageName, Set <Path> directorys, ClassLoader loader, boolean bailError) {
    Filter <Path> filter;
    Set <Class <?>> result = new HashSet <>();
    for (Path startPath : directorys) {
        String packagePath = packageName.replace(".", startPath.getFileSystem().getSeparator());
        final Path searchPath = startPath.resolve(packagePath).toAbsolutePath();
        if (subPackages) {
            filter = p -> {
                p = p.toAbsolutePath();
                Path other;
                if (p.getNameCount() >= searchPath.getNameCount()) {
                    other = searchPath;
                } else {
                    other = searchPath.subpath(0, p.getNameCount());
                }
                if (p.startsWith(other)) {
                    return true;
                } else {
                    return false;
                }
            };
        } else {
            filter = p -> {
                p = p.toAbsolutePath();
                if (p.getNameCount() > searchPath.getNameCount() + 1) {
                    return false;
                } else if (p.toAbsolutePath().startsWith(searchPath)) {
                    return true;
                } else {
                    return false;
                }
            };
        }
        if (Files.exists(searchPath)) {
            findDirClassFilesRecursive(filter, searchPath, startPath, result, loader, bailError);
        } // the package does not have to exist in every directory
    }
    return result;
}

private static void findDirClassFilesRecursive(Filter <Path> filter, Path path, Path start, Set <Class <?>> classes, ClassLoader loader, boolean bailError) {
    try (DirectoryStream <Path> dirStream = Files.newDirectoryStream(path, filter)) {
        for (Path p : dirStream) {
            if (Files.isDirectory(p)) {
                findDirClassFilesRecursive(filter, p, start, classes, loader, bailError);
            } else {
                Path subp = p.subpath(start.getNameCount(), p.getNameCount());
                String str = subp.toString();
                if (str.endsWith(".class")) {
                    str = str.substring(0, str.length() - 6);
                    String sep = p.getFileSystem().getSeparator();
                    if (str.startsWith(sep)) {
                        str = str.substring(sep.length());
                    }
                    if (str.endsWith(sep)) {
                        str = str.substring(0, str.length() - sep.length());
                    }
                    String fullClassName = str.replace(sep, ".");
                    try {
                        Class <?> cls = Class.forName(fullClassName, false, loader);
                        classes.add(cls);
                    } catch (ClassNotFoundException e) {
                        if (bailError) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        if (bailError) {
            throw new RuntimeException(e);
        }
    }
}

private static Set <Class <?>> findJarClasses(boolean subPackages, String packageName, Set <URL> nextJarUrls, Set <Path> directories, ClassLoader loader, boolean bailError) {
    String packagePath = packageName.replace('.', '/');
    Set <Class <?>> result = new HashSet <>();
    Set <URL> allJarUrls = new HashSet <>();
    while (true) {
        Set <URL> thisJarUrls = new HashSet <>(nextJarUrls);
        thisJarUrls.removeAll(allJarUrls);
        if (thisJarUrls.isEmpty()) {
            break;
        }
        allJarUrls.addAll(thisJarUrls);
        for (URL url : thisJarUrls) {
            try (JarInputStream stream = new JarInputStream(url.openStream())) {
                // may want better way to open url connections
                readJarClassPath(stream, nextJarUrls, directories, bailError);
                JarEntry entry = stream.getNextJarEntry();
                while (entry != null) {
                    String name = entry.getName();
                    int i = name.lastIndexOf("/");
                    
                    if (i > 0 && name.endsWith(".class")) {
                        try {
                            if (subPackages) {
                                if (name.substring(0, i).startsWith(packagePath)) {
                                    Class <?> cls = Class.forName(name.substring(0, name.length() - 6).replace("/", "."), false, loader);
                                    result.add(cls);
                                }
                            } else {
                                if (name.substring(0, i).equals(packagePath)) {
                                    Class <?> cls = Class.forName(name.substring(0, name.length() - 6).replace("/", "."), false, loader);
                                    result.add(cls);
                                }
                            }
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                    entry = stream.getNextJarEntry();
                }
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

private static void readJarClassPath(JarInputStream stream, Set <URL> jarUrls, Set <Path> directories, boolean bailError) {
    Object classPathObj = stream.getManifest().getMainAttributes().get(new Name("Class-Path"));
    if (classPathObj == null) {
        return;
    }
    if (classPathObj instanceof String) {
        String[] entries = ((String) classPathObj).split("\\s+");// should also work with a single space (" ")
        for (String entry : entries) {
            try {
                URL url = new URL(entry);
                addFromUrl(jarUrls, directories, url, bailError);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    } else if (bailError) {
        throw new RuntimeException("the Class-Path attribute is no String: " + classPathObj.getClass().getName() + " tos='" + classPathObj + "'");
    }
}

private static void findClassPools(ClassLoader classLoader, Set <URL> jarUrls, Set <Path> directoryPaths, boolean bailError, String packageName) {
    packageName = packageName.replace('.', '/');
    while (classLoader != null) {
        if (classLoader instanceof URLClassLoader) {
            for (URL url : ((URLClassLoader) classLoader).getURLs()) {
                addFromUrl(jarUrls, directoryPaths, url, bailError);
                System.out.println("rurl-class-loade.url[n]r->'" + url + "'");
            }
        } else {
            URL res = classLoader.getResource("");
            if (res != null) {
                addFromUrl(jarUrls, directoryPaths, res, bailError);
            }
            res = classLoader.getResource("/");
            if (res != null) {
                addFromUrl(jarUrls, directoryPaths, res, bailError);
            }
            res = classLoader.getResource("/" + packageName);
            if (res != null) {
                res = removePackageFromUrl(res, packageName, bailError);
                if (res != null) {
                    addFromUrl(jarUrls, directoryPaths, res, bailError);
                }
            }
            res = classLoader.getResource(packageName);
            if (res != null) {
                res = removePackageFromUrl(res, packageName, bailError);
                if (res != null) {
                    addFromUrl(jarUrls, directoryPaths, res, bailError);
                }
            }
            addFromUnknownClass(classLoader, jarUrls, directoryPaths, bailError, 8);
        }
        classLoader = classLoader.getParent();
    }
}

private static URL removePackageFromUrl(URL res, String packagePath, boolean bailError) {
    packagePath = "/" + packagePath;
    String urlStr = res.toString();
    if ( !urlStr.endsWith(packagePath)) {
        if (bailError) {
            throw new RuntimeException("the url string does not end with the packagepath! packagePath='" + packagePath + "' urlStr='" + urlStr + "'");
        } else {
            return null;
        }
    }
    urlStr = urlStr.substring(0, urlStr.length() - packagePath.length());
    if (urlStr.endsWith("!")) {
        urlStr = urlStr.substring(0, urlStr.length() - 1);
    }
    if (urlStr.startsWith("jar:")) {
        urlStr = urlStr.substring(4);
    }
    try {
        return new URL(urlStr);
    } catch (MalformedURLException e) {
        if (bailError) {
            throw new RuntimeException(e);
        } else {
            return null;
        }
    }
}

private static void addFromUnknownClass(Object instance, Set <URL> jarUrls, Set <Path> directoryPaths, boolean bailError, int maxDeep) {
    Class <?> cls = instance.getClass();
    while (cls != null) {
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            Class <?> type = field.getType();
            Object value;
            try {
                value = getValue(instance, field);
                if (value != null) {
                    addFromUnknownValue(value, jarUrls, directoryPaths, bailError, type, field.getName(), maxDeep - 1);
                }
            } catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
                if (bailError) {
                    final String version = System.getProperty("java.version");
                    String vers = version;
                    if (vers.startsWith("1.")) {
                        vers = vers.substring(2);
                    }
                    int dotindex = vers.indexOf('.');
                    if (dotindex != -1) {
                        vers = vers.substring(0, dotindex);
                    }
                    int versNum;
                    try {
                        versNum = Integer.parseInt(vers);
                    } catch (NumberFormatException e1) {
                        throw new RuntimeException("illegal version: '" + version + "' lastError: " + e.getMessage(), e);
                    }
                    if (versNum <= 11) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        cls = cls.getSuperclass();
    }
    
}

private static Object getValue(Object instance, Field field) throws IllegalArgumentException, IllegalAccessException, SecurityException {
    try {
        boolean flag = field.isAccessible();
        boolean newflag = flag;
        try {
            field.setAccessible(true);
            newflag = true;
        } catch (Exception e) {}
        try {
            return field.get(instance);
        } finally {
            if (flag != newflag) {
                field.setAccessible(flag);
            }
        }
    } catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
        try {
            Field override = AccessibleObject.class.getDeclaredField("override");
            boolean flag = override.isAccessible();
            boolean newFlag = flag;
            try {
                override.setAccessible(true);
                flag = true;
            } catch (Exception s) {}
            override.setBoolean(field, true);
            if (flag != newFlag) {
                override.setAccessible(flag);
            }
            return field.get(instance);
        } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e1) {
            e.addSuppressed(e1);
            throw e;
        }
    }
}

private static void addFromUnknownValue(Object value, Set <URL> jarUrls, Set <Path> directoryPaths, boolean bailError, Class <?> type, String fieldName, int maxDeep) {
    if (Collection.class.isAssignableFrom(type)) {
        for (Object obj : (Collection <?>) value) {
            URL url = null;
            try {
                if (obj instanceof URL) {
                    url = (URL) obj;
                } else if (obj instanceof Path) {
                    url = ((Path) obj).toUri().toURL();
                } else if (obj instanceof File) {
                    url = ((File) obj).toURI().toURL();
                }
            } catch (MalformedURLException e) {
                if (bailError) {
                    throw new RuntimeException(e);
                }
            }
            if (url != null) {
                addFromUrl(jarUrls, directoryPaths, url, bailError);
            }
        }
    } else if (URL[].class.isAssignableFrom(type)) {
        for (URL url : (URL[]) value) {
            addFromUrl(jarUrls, directoryPaths, url, bailError);
        }
    } else if (Path[].class.isAssignableFrom(type)) {
        for (Path path : (Path[]) value) {
            try {
                addFromUrl(jarUrls, directoryPaths, path.toUri().toURL(), bailError);
            } catch (MalformedURLException e) {
                if (bailError) {
                    throw new RuntimeException(e);
                }
            }
        }
    } else if (File[].class.isAssignableFrom(type)) {
        for (File file : (File[]) value) {
            try {
                addFromUrl(jarUrls, directoryPaths, file.toURI().toURL(), bailError);
            } catch (MalformedURLException e) {
                if (bailError) {
                    throw new RuntimeException(e);
                }
            }
        }
    } else if (maxDeep > 0) {
        addFromUnknownClass(value, jarUrls, directoryPaths, bailError, maxDeep - 1);
    }
}

private static void addFromUrl(Set <URL> jarUrls, Set <Path> directoryPaths, URL url, boolean bailError) {
    if (url.getFile().endsWith(".jar") || url.getFile().endsWith(".zip")) {
        // may want better way to detect jar files
        jarUrls.add(url);
    } else {
        try {
            Path path = Paths.get(url.toURI());
            if (Files.isDirectory(path)) {
                directoryPaths.add(path);
            } else if (bailError) {
                throw new RuntimeException("unknown url for class loading: " + url);
            }
        } catch (URISyntaxException e) {
            if (bailError) {
                throw new RuntimeException(e);
            }
        }
    }
}

进口:

import java.io.File;
import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.DirectoryStream;
import java.nio.file.DirectoryStream.Filter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.Attributes.Name;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

我是这样做的。我扫描所有的子文件夹(子包),我不尝试加载匿名类:

   /**
   * Attempts to list all the classes in the specified package as determined
   * by the context class loader, recursively, avoiding anonymous classes
   * 
   * @param pckgname
   *            the package name to search
   * @return a list of classes that exist within that package
   * @throws ClassNotFoundException
   *             if something went wrong
   */
  private static List<Class> getClassesForPackage(String pckgname) throws ClassNotFoundException {
      // This will hold a list of directories matching the pckgname. There may be more than one if a package is split over multiple jars/paths
      ArrayList<File> directories = new ArrayList<File>();
      String packageToPath = pckgname.replace('.', '/');
      try {
          ClassLoader cld = Thread.currentThread().getContextClassLoader();
          if (cld == null) {
              throw new ClassNotFoundException("Can't get class loader.");
          }

          // Ask for all resources for the packageToPath
          Enumeration<URL> resources = cld.getResources(packageToPath);
          while (resources.hasMoreElements()) {
              directories.add(new File(URLDecoder.decode(resources.nextElement().getPath(), "UTF-8")));
          }
      } catch (NullPointerException x) {
          throw new ClassNotFoundException(pckgname + " does not appear to be a valid package (Null pointer exception)");
      } catch (UnsupportedEncodingException encex) {
          throw new ClassNotFoundException(pckgname + " does not appear to be a valid package (Unsupported encoding)");
      } catch (IOException ioex) {
          throw new ClassNotFoundException("IOException was thrown when trying to get all resources for " + pckgname);
      }

      ArrayList<Class> classes = new ArrayList<Class>();
      // For every directoryFile identified capture all the .class files
      while (!directories.isEmpty()){
          File directoryFile  = directories.remove(0);             
          if (directoryFile.exists()) {
              // Get the list of the files contained in the package
              File[] files = directoryFile.listFiles();

              for (File file : files) {
                  // we are only interested in .class files
                  if ((file.getName().endsWith(".class")) && (!file.getName().contains("$"))) {
                      // removes the .class extension
                      int index = directoryFile.getPath().indexOf(packageToPath);
                      String packagePrefix = directoryFile.getPath().substring(index).replace('/', '.');;                          
                    try {                  
                      String className = packagePrefix + '.' + file.getName().substring(0, file.getName().length() - 6);                            
                      classes.add(Class.forName(className));                                
                    } catch (NoClassDefFoundError e)
                    {
                      // do nothing. this class hasn't been found by the loader, and we don't care.
                    }
                  } else if (file.isDirectory()){ // If we got to a subdirectory
                      directories.add(new File(file.getPath()));                          
                  }
              }
          } else {
              throw new ClassNotFoundException(pckgname + " (" + directoryFile.getPath() + ") does not appear to be a valid package");
          }
      }
      return classes;
  }  

这是不可能的,因为包中的所有类都可能不会被加载,而你总是知道一个类的包。

您可能应该看看开源的Reflections库。有了它,你可以很容易地实现你想要的。

首先,设置反射索引(这有点乱,因为搜索所有类默认是禁用的):

List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());

Reflections reflections = new Reflections(new ConfigurationBuilder()
    .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner())
    .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
    .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("org.your.package"))));

然后你可以查询给定包中的所有对象:

Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);

如果你在Spring-land,你可以使用PathMatchingResourcePatternResolver;

  PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  Resource[] resources = resolver.getResources("classpath*:some/package/name/*.class");

    Arrays.asList(resources).forEach(r->{
        ...
    });