记录一些常用但记不起来的方法类

1.查找参数类里面是否包含某个注解

ProtocolType protocolType = AnnotationUtils.findAnnotation(clazz, ProtocolType.class);
这个就是判断clazz类里面是否包含 ProtocolType注解,有的话就返回注解本身,没有就返回null(一般用作自定义注解)

2.获取指定类型的JavaBean

Map<String, IProcessor> beansMap = ApplicationContextUtils.getApplicationContext().getBeansOfType(IProcessor.class);
该方法就是获取凡是继承IProcessor接口或者类的所有javaBean。

3. 获取由此对象表示的类或接口实现的接口

private class Food{
    String foodName;
}
private interface Eat<T>{
    void eat(T things);
}
private interface Run{
    void run();
}

private class Dog implements Eat<Food>,Run{
    @Override
    public void run() { }
    @Override
    public void eat(Food things) { }
}
private void main() {
    Class<?> clazz = Dog.class;
    Type[] genericInterfaces = clazz.getGenericInterfaces();
    Class<?>[] interfaces = clazz.getInterfaces();
}
运行结果
 可以看到:clazz.getGenericInterfaces();所生成的数组里有一个接口类型为:ParameterizedType 而ParameterizedType extends Type

ParameterizedType 和 Type 有什么区别呢?

Type代表 包括原始类型,参数化类型、数组类型、类型变量和原始类型,而ParameterizedType只代表参数化类型


译:ParameterizedType表示参数化类型,例如 Collection ParameterizedType相对于Type新增了三个接口方法:


Type[] getActualTypeArguments(); //返回参数化类型的实际类型的数组 例如:Food<Fruit,Vegetable>那么将返回 [Fruit,Vegetable]这两个类型的数组 下面两种方法就不用多说了,从字面意思就能看出来。

Type getRawType();//返回最外层<>前面那个类型,即Map<K ,V>的Map。

Type getOwnerType();//获得这个类型的所有者的类型,主要对嵌套定义的内部类而言。列如对java.util.Map.Node<K,V> 调用getOwnerType方法返回的是interface java.util.Map接口

对上面的内容进行一个总结:

getInterfaces:主要是 获取由此对象表示的类或接口实现的接口

getGenericInterfaces: 主要是 获取由此对象表示的类或接口直接实现的接口的Type。

 

 



posted @ 2022-10-11 09:58  红豆奶茶+红豆  阅读(40)  评论(0)    收藏  举报