JAVA反射工具包

1、

  
            org.reflections
            reflections
            0.9.12
        

2、

package com.example.learn.demo.reflect;

import com.example.learn.demo.DemoApplication;
import org.reflections.Reflections;
import org.springframework.boot.SpringApplication;

import java.util.Set;

public class TestOne {

    public static void main(String[] args) {

        // 实例化Reflections,并指定要扫描的包名
        Reflections reflections = new Reflections("com.example.learn.demo");
// 获取某个类的所有子类
        Set> subTypes = reflections.getSubTypesOf(SomeType.class);
        subTypes.forEach(i-> System.out.println(i.getSimpleName()));
// 获取包含某个注解的所有类
        Set> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);
        annotated.forEach(i-> System.out.println(i.getSimpleName()));
    }
}

3、

package com.example.learn.demo.reflect;

public class SomeType {
}

4、

package com.example.learn.demo.reflect;
@SomeAnnotation
public class SomeTypeOne extends SomeType {
}

5、

package com.example.learn.demo.reflect;

public @interface SomeAnnotation {
}

 

你可能感兴趣的:(JAVA反射工具包)