java吧 关注:1,235,798贴子:12,706,145
  • 13回复贴,共1

请问ResolvableType的generic哪里来的啊

只看楼主收藏回复

请问 springboot2.18 底层 listener.starting()一直往下走。
eventType = ResolvableType.forClass(listenerType).as(ApplicationListener.class).getGeneric();
这行代码中getGeneric是直接取的对象的generic,这里很奇怪啊。
我断点看这个地方只运行了一次就直接从对象取值了,但是new 这个对象的时候和 as方法都没有地方看到有对这个赋值。

其中,这个地方突然就有值了,完全不明白。 有谁能解答一下吗?


IP属地:湖北1楼2024-08-01 16:33回复
    构造器赋值了吧


    IP属地:广东来自Android客户端3楼2024-08-01 18:35
    收起回复
      listenertype可能有类型信息?


      IP属地:重庆来自Android客户端4楼2024-08-02 09:53
      收起回复
        这是反射获取对象


        IP属地:海南来自Android客户端5楼2024-08-02 11:25
        回复
          static ResolvableType resolveDeclaredEventType(Class<?> listenerType) {
          ResolvableType eventType = eventTypeCache.get(listenerType);
          if (eventType == null) {
          eventType = ResolvableType.forClass(listenerType).as(ApplicationListener.class).getGeneric();
          eventTypeCache.put(listenerType, eventType);
          }
          return (eventType != ResolvableType.NONE ? eventType : null);
          }
          --------------------------------------------------------------------------------------------------------------------------------------------------
          ResolvableType.forClass(listenerType)这个时候创建的对象没有generic
          as方法后有了generic
          getGeneric直接从对象取的
          ---------------------------------------------------------------------------------------------------------------------------------------------------
          public ResolvableType as(Class<?> type) {
          if (this == NONE) {
          return NONE;
          }
          Class<?> resolved = resolve();
          if (resolved == null || resolved == type) {
          return this;
          }
          for (ResolvableType interfaceType : getInterfaces()) {
          ResolvableType interfaceAsType = interfaceType.as(type);
          if (interfaceAsType != NONE) {
          return interfaceAsType;
          }
          }
          return getSuperType().as(type);
          }
          这个递归里面getInterfaces最后得到的interfaceType 有 generic
          -------------------------------------------------------------------------------------------------------------------------------------------------
          public ResolvableType[] getInterfaces() {
          Class<?> resolved = resolve();
          if (resolved == null) {
          return EMPTY_TYPES_ARRAY;
          }
          ResolvableType[] interfaces = this.interfaces;
          if (interfaces == null) {
          Type[] genericIfcs = resolved.getGenericInterfaces();
          interfaces = new ResolvableType[genericIfcs.length];
          for (int i = 0; i < genericIfcs.length; i++) {
          interfaces[i] = forType(genericIfcs[i], this);
          }
          this.interfaces = interfaces;
          }
          return interfaces;
          }
          这里面重点是 forType方法
          ---------------------------------------------------------------------------------------------------------------------------------------------------
          static ResolvableType forType(
          @nullable Type type, @Nullable TypeProvider typeProvider, @Nullable VariableResolver variableResolver) {
          if (type == null && typeProvider != null) {
          type = SerializableTypeWrapper.forTypeProvider(typeProvider);
          }
          if (type == null) {
          return NONE;
          }
          // 重点是这里
          if (type instanceof Class) {
          return new ResolvableType(type, typeProvider, variableResolver, (ResolvableType) null);
          }
          // Purge empty entries on access since we don't have a clean-up thread or the like.
          cache.purgeUnreferencedEntries();
          // Check the cache - we may have a ResolvableType which has been resolved before...
          ResolvableType resultType = new ResolvableType(type, typeProvider, variableResolver);
          ResolvableType cachedType = cache.get(resultType);
          if (cachedType == null) {
          cachedType = new ResolvableType(type, typeProvider, variableResolver, resultType.hash);
          cache.put(cachedType, cachedType);
          }
          resultType.resolved = cachedType.resolved;
          return resultType;
          }


          IP属地:湖北6楼2024-08-02 14:28
          回复
            我只记得研究mvc的converter的时候看到resolvable之类的会直接获取泛型信息,但是好像涉及到native的方法,然后各种获取类的class信息


            IP属地:广东来自Android客户端7楼2024-08-03 00:47
            回复
              啊,我终于明白了,这个问题我搞明白了,这里把答案放在这里。
              idea的debug功能会在初始化对象的时候调用tostring方法,在这里tostring方法里面hasGenerics()时会对generic赋值


              IP属地:湖北8楼2024-11-20 17:21
              收起回复
                我真服了!你项目启动的时候需要对需要的类已经进行缓存了!你debug的时候!直接去缓存里面拿!什么idea的debug功能会在初始化对象的时候调用tostring方法!还问我底层,你把Spring搞懂再说吧!?真搞笑!


                IP属地:河南9楼2024-12-30 17:45
                回复