dubbo 关于启动时MapTypeBuilder写死判断参数长度为2的问题

sd2nnvve  于 2022-11-02  发布在  其他
关注(0)|答案(2)|浏览(175)
  • I have searched the issues of this repository and believe that this is not a duplicate.
  • I have checked the FAQ of this repository and believe that this is not a duplicate.

Environment

  • Dubbo version: 2.7.3
  • Operating System version: linux
  • Java version: 1.8

Steps to reproduce this issue

  1. MapTypeBuilder 中写死map泛型参数数目必须为2,导致在使用外部开源的原生类型的map判断报错。
@Override
  public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinition> typeCache) {
    if (!(type instanceof ParameterizedType)) {
      return new TypeDefinition(clazz.getName());
    }

    ParameterizedType parameterizedType = (ParameterizedType) type;
    Type[] actualTypeArgs = parameterizedType.getActualTypeArguments();
    if (actualTypeArgs == null || actualTypeArgs.length != 2) {
      throw new IllegalArgumentException(MessageFormat.format(
          "[ServiceDefinitionBuilder] Map type [{0}] with unexpected amount of arguments [{1}]." + actualTypeArgs,
          new Object[] {type, actualTypeArgs}));
    }

    for (Type actualType : actualTypeArgs) {
      if (actualType instanceof ParameterizedType) {
        // Nested collection or map.
        Class<?> rawType = (Class<?>) ((ParameterizedType) actualType).getRawType();
        TypeDefinitionBuilder.build(actualType, rawType, typeCache);
      } else if (actualType instanceof Class<?>) {
        Class<?> actualClass = (Class<?>) actualType;
        if (actualClass.isArray() || actualClass.isEnum()) {
          TypeDefinitionBuilder.build(null, actualClass, typeCache);
        } else {
          DefaultTypeBuilder.build(actualClass, typeCache);
        }
      }
    }

如下所示,参数长度为1。

package it.unimi.dsi.fastutil.ints;
public interface Int2ObjectMap<V> extends Int2ObjectFunction<V>, Map<Integer, V> {}

该类来自于

<dependency>
    <groupId>it.unimi.dsi</groupId>
    <artifactId>fastutil</artifactId>
</dependency>

Expected Result

是否可以判断他的上层实现来进行判断

能否定义一个order接口,然后sort一下,这样外部拓展的时候可以手动调准新增的builder的顺序

kkbh8khc

kkbh8khc1#

遇到同样的问题
public class WebResult extends HashMap<String, Object> implements Serializable {
.........
}

daupos2t

daupos2t2#

1.创建自定义TypeBuilder

package com.xxx.dubbo.TreeBuilder;

public class TreeBuilder implements TypeBuilder , Prioritized {

    @Override
    public boolean accept(Type type, Class<?> clazz) {
        if (clazz == null) {
            return false;
        }
        return Tree.class.isAssignableFrom(clazz);
    }

    @Override
    public int getPriority() {
        return -1;
    }

    @Override
    public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinition> typeCache) {
        // balabala...
    }
}
  1. META-INF 下创建 dubbo/internal/org.apache.dubbo.metadata.definition.builder.TypeBuilder 文件
tree=com.xxx.dubbo.TreeBuilder

#6306

相关问题