Reflections could not get class type

前端 未结 3 1977
一个人的身影
一个人的身影 2021-01-11 10:01

So I am using the Java Reflections API to search another jar for Classes that extend Foo using the following code:

Reflections reflections =          


        
3条回答
  •  迷失自我
    2021-01-11 10:21

    Scanning for classes is not easy with pure Java.

    The spring framework offers a class called ClassPathScanningCandidateComponentProvider that can do what you need. The following example would find all subclasses of MyClass in the package org.example.package

    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
     provider.addIncludeFilter(new AssignableTypeFilter(MyClass.class));
    
    // scan in org.example.package
     Set components = provider.findCandidateComponents("org/example/package");
    for (BeanDefinition component : components)
    {
    

    This method has the additional benefit of using a bytecode analyzer to find the candidates which means it will not load all classes it scans. Class cls = Class.forName(component.getBeanClassName()); // use class cls found }

    Fore more info read the link

提交回复
热议问题