简体   繁体   English

获取模块 java.base 中所有类的 Class 对象

[英]Get the Class objects for all classes in the module java.base

How can I get the Class objects for all the classes in the module java.base programmatically?如何以编程方式获取模块java.base所有类的Class对象? I'm open to using any external libraries.我愿意使用任何外部库。 I have looked at the Reflections library but could not get a list of classes in the built-in Java Library, only classes on the classpath.我查看了Reflections库,但无法获取内置 Java 库中的类列表,只能获取类路径中的类。 I tried to manually do this by iterating through all the files in my file system, but (as far as I'm aware) I cannot get to the classes because they are inside a jar file.我试图通过遍历文件系统中的所有文件来手动执行此操作,但是(据我所知)我无法访问这些类,因为它们位于 jar 文件中。 What would the code look like for getting all the Class objects for the classes in java.base ?获取java.base类的所有Class对象的代码是什么样的?

Thanks.谢谢。

I was able to extract all of the classes from the src.zip file:我能够从 src.zip 文件中提取所有类:

public class TypeUtils {
    private static Set<Class<?>> classes;
    private static final Set<String> CONSIDERED_MODULES = Set.of("java.base");
    public static void main(String[] args) {
        try {
            findClasses();
            classes.forEach(System.out::println);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void findClasses() throws IOException {
        classes = new HashSet<>();
        File root = new File(System.getProperty("java.home"));
        File lib = new File(root, "lib");
        ZipFile srczip = new ZipFile(new File(lib, "src.zip"));
        Iterator<? extends ZipEntry> itr = srczip.entries().asIterator();
        while(itr.hasNext())
            extractFromEntry(itr.next());
    }
    
    private static void extractFromEntry(ZipEntry entry) {
        String name = entry.getName();
        if(name.endsWith(".java")) {
            int firstSlash = name.indexOf('/');
            String moduleName = name.substring(0, firstSlash);
            if(!CONSIDERED_MODULES.contains(moduleName)) return;
            String fullyQualifiedName = name.substring(firstSlash + 1, name.length() - 5).replace('/', '.');
            if(fullyQualifiedName.endsWith("-info")) return; //catches module-info or package-info files
            try {
                Class<?> clazz = Class.forName(fullyQualifiedName);
                classes.add(clazz);
            }
            catch (ClassNotFoundException e) {}
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用修饰符“public”访问类java.nio.DirectByteBuffer(在模块java.base中)的成员 - cannot access a member of class java.nio.DirectByteBuffer (in module java.base) with modifiers “public” 例外 - class [B 不能转换为 class [C([B 和 [C 在模块 java.base 的加载程序“引导程序”中) - Exception - class [B cannot be cast to class [C ([B and [C are in module java.base of loader 'bootstrap') 模块java.base不读取模块java.desktop - module java.base does not read module java.desktop X 类不能转换为布尔类(X 位于加载程序“app”的未命名模块中;布尔值位于加载程序“bootstrap”的模块 java.base 中 - Class X cannot be cast to class Boolean (X is in unnamed module of loader 'app'; Boolean is in module java.base of loader 'bootstrap 无法从java.base模块导出包 - Unable to export a package from java.base module 在将jnlp从openjdk 8迁移到openjdk 9时,JarIndexAccess无法访问类jdk.internal.util.jar.JarIndex(在java.base模块中) - JarIndexAccess cannot access class jdk.internal.util.jar.JarIndex (in module java.base) in migrating jnlp from openjdk 8 to openjdk 9 class java.math.BigInteger cannot be cast to class java.lang.Integer (java.math.BigInteger and java.lang.Integer are in module java.base of load - class java.math.BigInteger cannot be cast to class java.lang.Integer (java.math.BigInteger and java.lang.Integer are in module java.base of load java.lang.ClassCastException:类 java.lang.String 不能强制转换为类 [B(java.lang.String 和 [B 在加载程序&#39;bootstrap 的模块 java.base 中) - java.lang.ClassCastException: class java.lang.String cannot be cast to class [B (java.lang.String and [B are in module java.base of loader 'bootstrap class java.lang.String cannot be cast to class java.util.List (java.lang.String and java.util.List are in module java.base of loader 'bootstrap') - class java.lang.String cannot be cast to class java.util.List (java.lang.String and java.util.List are in module java.base of loader 'bootstrap') class java.util.ArrayList cannot be cast to class com.patient.entity.CommentEntity (java.util.ArrayList is in module java.base of loader 'bootstrap'; - class java.util.ArrayList cannot be cast to class com.patient.entity.CommentEntity (java.util.ArrayList is in module java.base of loader 'bootstrap';
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM