Search in sources :

Example 6 with TypeDefinition

use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.

the class ServiceDefinitionBuilderTest method checkComplextObjectAsParam.

void checkComplextObjectAsParam(FullServiceDefinition fullServiceDefinition) {
    List<MethodDefinition> methodDefinitions = fullServiceDefinition.getMethods();
    MethodDefinition complexCompute = null;
    MethodDefinition findComplexObject = null;
    for (MethodDefinition methodDefinition : methodDefinitions) {
        if ("complexCompute".equals(methodDefinition.getName())) {
            complexCompute = methodDefinition;
        } else if ("findComplexObject".equals(methodDefinition.getName())) {
            findComplexObject = methodDefinition;
        }
    }
    Assertions.assertTrue(Arrays.equals(complexCompute.getParameterTypes(), new String[] { String.class.getName(), ComplexObject.class.getName() }));
    Assertions.assertEquals(complexCompute.getReturnType(), String.class.getName());
    Assertions.assertTrue(Arrays.equals(findComplexObject.getParameterTypes(), new String[] { String.class.getName(), "int", "long", String[].class.getCanonicalName(), "java.util.List<java.lang.Integer>", ComplexObject.TestEnum.class.getCanonicalName() }));
    Assertions.assertEquals(findComplexObject.getReturnType(), ComplexObject.class.getCanonicalName());
    List<TypeDefinition> typeDefinitions = fullServiceDefinition.getTypes();
    TypeDefinition topTypeDefinition = null;
    TypeDefinition innerTypeDefinition = null;
    TypeDefinition inner2TypeDefinition = null;
    TypeDefinition inner3TypeDefinition = null;
    for (TypeDefinition typeDefinition : typeDefinitions) {
        if (typeDefinition.getType().equals(ComplexObject.class.getName())) {
            topTypeDefinition = typeDefinition;
        } else if (typeDefinition.getType().equals(ComplexObject.InnerObject.class.getName())) {
            innerTypeDefinition = typeDefinition;
        } else if (typeDefinition.getType().contains(ComplexObject.InnerObject2.class.getName())) {
            inner2TypeDefinition = typeDefinition;
        } else if (typeDefinition.getType().equals(ComplexObject.InnerObject3.class.getName())) {
            inner3TypeDefinition = typeDefinition;
        }
    }
    Assertions.assertEquals(topTypeDefinition.getProperties().get("v").getType(), "long");
    Assertions.assertEquals(topTypeDefinition.getProperties().get("maps").getType(), "java.util.Map<java.lang.String,java.lang.String>");
    Assertions.assertEquals(topTypeDefinition.getProperties().get("innerObject").getType(), ComplexObject.InnerObject.class.getName());
    Assertions.assertEquals(topTypeDefinition.getProperties().get("intList").getType(), "java.util.List<java.lang.Integer>");
    Assertions.assertEquals(topTypeDefinition.getProperties().get("strArrays").getType(), "java.lang.String[]");
    Assertions.assertEquals(topTypeDefinition.getProperties().get("innerObject3").getType(), "org.apache.dubbo.metadata.definition.service.ComplexObject.InnerObject3[]");
    Assertions.assertEquals(topTypeDefinition.getProperties().get("testEnum").getType(), "org.apache.dubbo.metadata.definition.service.ComplexObject.TestEnum");
    Assertions.assertEquals(topTypeDefinition.getProperties().get("innerObject2").getType(), "java.util.Set<org.apache.dubbo.metadata.definition.service.ComplexObject$InnerObject2>");
    Assertions.assertSame(innerTypeDefinition.getProperties().get("innerA").getType(), "java.lang.String");
    Assertions.assertSame(innerTypeDefinition.getProperties().get("innerB").getType(), "int");
    Assertions.assertSame(inner2TypeDefinition.getProperties().get("innerA2").getType(), "java.lang.String");
    Assertions.assertSame(inner2TypeDefinition.getProperties().get("innerB2").getType(), "int");
    Assertions.assertSame(inner3TypeDefinition.getProperties().get("innerA3").getType(), "java.lang.String");
}
Also used : MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition) ComplexObject(org.apache.dubbo.metadata.definition.service.ComplexObject) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition)

Example 7 with TypeDefinition

use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.

the class ProtobufTypeBuilder method buildProtobufTypeDefinition.

private TypeDefinition buildProtobufTypeDefinition(Class<?> clazz, GeneratedMessageV3.Builder builder, Map<Class<?>, TypeDefinition> typeCache) {
    TypeDefinition typeDefinition = new TypeDefinition(clazz.getName());
    if (builder == null) {
        return typeDefinition;
    }
    Map<String, TypeDefinition> properties = new HashMap<>();
    Method[] methods = builder.getClass().getDeclaredMethods();
    for (Method method : methods) {
        String methodName = method.getName();
        if (isSimplePropertySettingMethod(method)) {
            // property of custom type or primitive type
            properties.put(generateSimpleFiledName(methodName), TypeDefinitionBuilder.build(method.getGenericParameterTypes()[0], method.getParameterTypes()[0], typeCache));
        } else if (isMapPropertySettingMethod(method)) {
            // property of map
            Type type = method.getGenericParameterTypes()[0];
            String fieldName = generateMapFieldName(methodName);
            validateMapType(fieldName, type.toString());
            properties.put(fieldName, TypeDefinitionBuilder.build(type, method.getParameterTypes()[0], typeCache));
        } else if (isListPropertyGettingMethod(method)) {
            // property of list
            Type type = method.getGenericReturnType();
            String fieldName = generateListFieldName(methodName);
            TypeDefinition td;
            if (ProtocolStringList.class.isAssignableFrom(method.getReturnType())) {
                // property defined as "repeated string" transform to ProtocolStringList,
                // should be build as List<String>.
                td = TypeDefinitionBuilder.build(STRING_LIST_TYPE, List.class, typeCache);
            } else {
                // property without generic type should not be build ex method return List
                if (!LIST_PATTERN.matcher(type.toString()).matches()) {
                    continue;
                }
                td = TypeDefinitionBuilder.build(type, method.getReturnType(), typeCache);
            }
            properties.put(fieldName, td);
        }
    }
    typeDefinition.setProperties(properties);
    typeCache.put(clazz, typeDefinition);
    return typeDefinition;
}
Also used : Type(java.lang.reflect.Type) HashMap(java.util.HashMap) ProtocolStringList(com.google.protobuf.ProtocolStringList) List(java.util.List) ByteString(com.google.protobuf.ByteString) Method(java.lang.reflect.Method) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition)

Example 8 with TypeDefinition

use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.

the class ServiceDefinitionBuilder method build.

public static <T extends ServiceDefinition> void build(T sd, final Class<?> interfaceClass) {
    sd.setCanonicalName(interfaceClass.getCanonicalName());
    sd.setCodeSource(ClassUtils.getCodeSource(interfaceClass));
    Annotation[] classAnnotations = interfaceClass.getAnnotations();
    sd.setAnnotations(annotationToStringList(classAnnotations));
    TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
    List<Method> methods = ClassUtils.getPublicNonStaticMethods(interfaceClass);
    for (Method method : methods) {
        MethodDefinition md = new MethodDefinition();
        md.setName(method.getName());
        Annotation[] methodAnnotations = method.getAnnotations();
        md.setAnnotations(annotationToStringList(methodAnnotations));
        // Process parameter types.
        Class<?>[] paramTypes = method.getParameterTypes();
        Type[] genericParamTypes = method.getGenericParameterTypes();
        String[] parameterTypes = new String[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++) {
            TypeDefinition td = builder.build(genericParamTypes[i], paramTypes[i]);
            parameterTypes[i] = td.getType();
        }
        md.setParameterTypes(parameterTypes);
        // Process return type.
        TypeDefinition td = builder.build(method.getGenericReturnType(), method.getReturnType());
        md.setReturnType(td.getType());
        sd.getMethods().add(md);
    }
    sd.setTypes(builder.getTypeDefinitions());
}
Also used : Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition) Type(java.lang.reflect.Type) MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition)

Example 9 with TypeDefinition

use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.

the class TypeDefinitionBuilder method build.

public static TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinition> typeCache) {
    TypeBuilder builder = getGenericTypeBuilder(type, clazz);
    TypeDefinition td;
    if (builder != null) {
        td = builder.build(type, clazz, typeCache);
        td.setTypeBuilderName(builder.getClass().getName());
    } else {
        td = DefaultTypeBuilder.build(clazz, typeCache);
        td.setTypeBuilderName(DefaultTypeBuilder.class.getName());
    }
    return td;
}
Also used : DefaultTypeBuilder(org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder) TypeBuilder(org.apache.dubbo.metadata.definition.builder.TypeBuilder) DefaultTypeBuilder(org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition)

Example 10 with TypeDefinition

use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.

the class PrimitiveTypeDefinitionBuilderTest method buildAndAssertTypeDefinition.

static void buildAndAssertTypeDefinition(ProcessingEnvironment processingEnv, VariableElement field, TypeDefinitionBuilder builder) {
    TypeDefinition typeDefinition = TypeDefinitionBuilder.build(processingEnv, field);
    assertBasicTypeDefinition(typeDefinition, field.asType().toString(), builder);
// assertEquals(field.getSimpleName().toString(), typeDefinition.get$ref());
}
Also used : TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition)

Aggregations

TypeDefinition (org.apache.dubbo.metadata.definition.model.TypeDefinition)24 Type (java.lang.reflect.Type)7 Test (org.junit.jupiter.api.Test)6 MethodDefinition (org.apache.dubbo.metadata.definition.model.MethodDefinition)5 ServiceDefinition (org.apache.dubbo.metadata.definition.model.ServiceDefinition)5 Gson (com.google.gson.Gson)4 Method (java.lang.reflect.Method)4 TestService (org.apache.dubbo.metadata.definition.common.TestService)4 HashMap (java.util.HashMap)3 ParameterizedType (java.lang.reflect.ParameterizedType)2 ArrayList (java.util.ArrayList)2 DefaultTypeBuilder (org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder)2 MapTypeBuilder (org.apache.dubbo.metadata.definition.builder.MapTypeBuilder)2 ByteString (com.google.protobuf.ByteString)1 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)1 ProtocolStringList (com.google.protobuf.ProtocolStringList)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 List (java.util.List)1 Map (java.util.Map)1