java小技巧(二)--TypeResolver用法

1、List

1 TypeResolver typeResolver = new TypeResolver();
2 // listType => List<Object>
3 ResolvedType listType = typeResolver.resolve(List.class);

2、Map

1 class StringIntMap extends HashMap<String,Integer> { }
2 
3 TypeResolver typeResolver = new TypeResolver();
4 ResolvedType resolve = typeResolver.resolve(StringIntMap.class);
5 List<ResolvedType> resolvedTypes = resolve.typeParametersFor(Map.class);
6 System.out.println(resolvedTypes);

3、ArrayList<String>

1 TypeResolver typeResolver = new TypeResolver();
2 ResolvedType listType = typeResolver.resolve(List.class, String.class);
3 System.out.println(listType);

4、类成员函数和成员变量

 1 public class Student implements Serializable {
 2     private static final long serialVersionUID = -1L;
 3 
 4     private String name;
 5 
 6     private Integer age;
 7 
 8     private String classRoom;
 9     
10     ..............
11 }
12 
13 TypeResolver typeResolver = new TypeResolver();
14 ResolvedType resolve = typeResolver.resolve(Student.class);
15 MemberResolver memberResolver = new MemberResolver(typeResolver);
16 ResolvedTypeWithMembers arrayListTypeWithMembers = memberResolver.resolve(
17         resolve, null, null);
18 ResolvedMethod[] arrayListMethods = arrayListTypeWithMembers.getMemberMethods();
19 ResolvedField[] memberFields = arrayListTypeWithMembers.getMemberFields();

5、成员函数过滤

 1 TypeResolver typeResolver = new TypeResolver();
 2 ResolvedType arrayListType = typeResolver.resolve(ArrayList.class, String.class);
 3 MemberResolver memberResolver = new MemberResolver(typeResolver);
 4 memberResolver.setMethodFilter(new Filter<RawMethod>() {
 5     @Override
 6     public boolean include(RawMethod element) {
 7         return "size".equals(element.getName());
 8     }
 9 });
10 ResolvedTypeWithMembers arrayListTypeWithMembers = memberResolver.resolve(arrayListType, null, null);
11 ResolvedMethod sizeMethod = arrayListTypeWithMembers.getMemberMethods()[0];

6、成员变量过滤

TypeResolver typeResolver = new TypeResolver();
ResolvedType arrayListType = typeResolver.resolve(ArrayList.class, String.class);
MemberResolver memberResolver = new MemberResolver(typeResolver);
memberResolver.setFieldFilter(new Filter<RawField>() {
    @Override
    public boolean include(RawField element) {
        return "size".equals(element.getName());
    }
});
ResolvedTypeWithMembers arrayListTypeWithMembers = memberResolver.resolve(arrayListType, null, null);
ResolvedField sizeField = arrayListTypeWithMembers.getMemberFields()[0];

7、注解解析

 1 @Retention(RetentionPolicy.RUNTIME)
 2 @interface Marker { }
 3 
 4 @Retention(RetentionPolicy.RUNTIME)
 5 @Inherited
 6 @interface MarkerA { }
 7 
 8 class SomeClass {
 9     @Marker @MarkerA
10     public void someMethod() { }
11 }
12 class SomeSubclass extends SomeClass {
13     @Override
14     public void someMethod() { }
15 }
16 
17 
18 TypeResolver typeResolver = new TypeResolver();
19 ResolvedType someType = typeResolver.resolve(SomeClass.class);
20 MemberResolver memberResolver = new MemberResolver(typeResolver);
21 memberResolver.setMethodFilter(new Filter<RawMethod>() {
22     @Override public boolean include(RawMethod element) {
23         return "someMethod".equals(element.getName());
24     }
25 });
26 AnnotationConfiguration annConfig = new AnnotationConfiguration.StdConfiguration(AnnotationInclusion.INCLUDE_BUT_DONT_INHERIT);
27 ResolvedTypeWithMembers someTypeWithMembers = memberResolver.resolve(someType, annConfig, null);
28 ResolvedMethod someMethod = someTypeWithMembers.getMemberMethods()[0];
29 Marker marker = someMethod.get(Marker.class);  // marker != null
30 MarkerA markerA = someMethod.get(MarkerA.class); // markerA != null

 

posted on 2023-04-14 17:35  小夏coding  阅读(204)  评论(0编辑  收藏  举报

导航