Netty TypeParameterMatcher与java 泛型

本文深入探讨了Netty中SimpleChannelInboundHandler类如何通过泛型确定类型拦截器。通过跟踪TypeParameterMatcher.find方法,详细解释了如何从类型参数中找到匹配的泛型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



  1.  /** 
  2.      * Create a new instance which will try to detect the types to match out of the type parameter of the class. 
  3.      * 
  4.      * @param autoRelease   {@code true} if handled messages should be released automatically by pass them to 
  5.      *                      {@link ReferenceCountUtil#release(Object)}. 
  6.      */  
  7.     protected SimpleChannelInboundHandler(boolean autoRelease) {  
  8.         // 根据传入的泛型来确定类型拦截器  
  9.         matcher = TypeParameterMatcher.find(this, SimpleChannelInboundHandler.class"I");  
  10.         this.autoRelease = autoRelease;  
  11.     } 
第一次看到下面代码,看到“ 根据传入的泛型来确定类型拦截器  ”注解,想到难道 这里"I"可以直接转为“泛型”?

直觉又感觉不对,做了个代码试验确实不行,只是表示个字符串“I“,没有别的功能。

那代码中又如何实现来确定泛型类型的呢,进一步研究源代码发现其最终实现是在TypeParameterMatcher.find0

TypeParameterMatcher matcher = map.get(typeParamName);
            if (matcher == null) {
              
                matcher = get(find0(object, parameterizedSuperclass, typeParamName));
                map.put(typeParamName, matcher);
            }

private static Class<?> find0(
                final Object object, Class<?> parameterizedSuperclass, String typeParamName) {

   TypeVariable<?>[] typeParams = currentClass.getSuperclass().getTypeParameters();
                    System.out.println("find0 typeParams num:"+typeParams.length);
                    for (int i = 0; i < typeParams.length; i ++) {
                         System.out.println("find0 typeParamName:"+typeParamName+"typeParams["+i+"].getName():"+typeParams[i].getName());
                        if (typeParamName.equals(typeParams[i].getName())) {
                            typeParamIndex = i;
                            break;
                        }
                    }

                    if (typeParamIndex < 0) {
                        throw new IllegalStateException(
                                "unknown type parameter '" + typeParamName + "': " + parameterizedSuperclass);
                    }

                    Type genericSuperType = currentClass.getGenericSuperclass();
                    System.out.println("find0 genericSuperType:"+genericSuperType.getTypeName());
                    if (!(genericSuperType instanceof ParameterizedType)) {
                        return Object.class;
                    }

                    Type[] actualTypeParams = ((ParameterizedType) genericSuperType).getActualTypeArguments();

                    Type actualTypeParam = actualTypeParams[typeParamIndex];
                    System.out.println("find0 actualTypeParam:"+actualTypeParam.getTypeName());
                    if (actualTypeParam instanceof ParameterizedType) {
                        actualTypeParam = ((ParameterizedType) actualTypeParam).getRawType();
                    }
                    if (actualTypeParam instanceof Class) {
                        System.out.println("find0 actualTypeParam:"+actualTypeParam.getTypeName());
                        return (Class<?>) actualTypeParam;
                    }

}


相关阅读:

http://stackoverflow.com/questions/27083667/proguard-and-netty-5-on-android

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值