ResolverUtil
前面我们讲到了ResolverUtil是用来找到符合条件的类。下面我们具体来讲讲怎么用的
只有3个方法
是不是比较简单?
我们再来看看Test类的定义
public interface Test {
/**
* Will be called repeatedly with candidate classes. Must return True if a class
* is to be included in the results, false otherwise.
* 根据传入的类型返回是否匹配
*/
boolean matches(Class<?> type);
}就一个方法。
再来看看Test的两个子类实现
public static class IsA implements Test {
private Class<?> parent;
public IsA(Class<?> parentType) {
this.parent = parentType;
}
@Override
public boolean matches(Class<?> type) {
return type != null && parent.isAssignableFrom(type);
}
}public static class AnnotatedWith implements Test {
private Class<? extends Annotation> annotation;
public AnnotatedWith(Class<? extends Annotation> annotation) {
this.annotation = annotation;
}
@Override
public boolean matches(Class<?> type) {
return type != null && type.isAnnotationPresent(annotation);
}
}是不是比较简单,所以,如果需要自定义实现规则的话,继承Test类,实现matches方法就行了,也比较简单。
关注我,带你由浅入深,走入源码世界。
| 留言与评论(共有 0 条评论) “” |