Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
njzhuzhiwei opened this issue Sep 27, 2019 · 2 comments
Open
2 tasks done
Labels
type/need-triage Need maintainers to triage

Comments

@njzhuzhiwei
Copy link

njzhuzhiwei commented Sep 27, 2019

  • 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的顺序

@htynkn htynkn added the type/need-triage Need maintainers to triage label Oct 5, 2019
@ben0x007
Copy link

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

@xul0038
Copy link

xul0038 commented Jul 29, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/need-triage Need maintainers to triage
Projects
None yet
Development

No branches or pull requests

4 participants