Search in sources :

Example 1 with MethodDefinition

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

the class AbstractServiceRestMetadataResolver method resolveRestMethodMetadata.

protected Optional<RestMethodMetadata> resolveRestMethodMetadata(ProcessingEnvironment processingEnv, TypeElement serviceType, TypeElement serviceInterfaceType, ExecutableElement serviceMethod) {
    ExecutableElement restCapableMethod = findRestCapableMethod(processingEnv, serviceType, serviceInterfaceType, serviceMethod);
    if (restCapableMethod == null) {
        // if can't be found
        return empty();
    }
    // requestPath is required
    String requestPath = resolveRequestPath(processingEnv, serviceType, restCapableMethod);
    if (requestPath == null) {
        return empty();
    }
    // requestMethod is required
    String requestMethod = resolveRequestMethod(processingEnv, serviceType, restCapableMethod);
    if (requestMethod == null) {
        return empty();
    }
    RestMethodMetadata metadata = new RestMethodMetadata();
    MethodDefinition methodDefinition = resolveMethodDefinition(processingEnv, serviceType, restCapableMethod);
    // Set MethodDefinition
    metadata.setMethod(methodDefinition);
    // process the annotated method parameters
    processAnnotatedMethodParameters(restCapableMethod, serviceType, metadata);
    // process produces
    Set<String> produces = new LinkedHashSet<>();
    processProduces(processingEnv, serviceType, restCapableMethod, produces);
    // process consumes
    Set<String> consumes = new LinkedHashSet<>();
    processConsumes(processingEnv, serviceType, restCapableMethod, consumes);
    // Initialize RequestMetadata
    RequestMetadata request = metadata.getRequest();
    request.setPath(requestPath);
    request.setMethod(requestMethod);
    request.setProduces(produces);
    request.setConsumes(consumes);
    // Post-Process
    postProcessRestMethodMetadata(processingEnv, serviceType, serviceMethod, metadata);
    return of(metadata);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition) ExecutableElement(javax.lang.model.element.ExecutableElement) RestMethodMetadata(org.apache.dubbo.metadata.rest.RestMethodMetadata) RequestMetadata(org.apache.dubbo.metadata.rest.RequestMetadata)

Example 2 with MethodDefinition

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

the class ProtobufTypeBuilderTest method testProtobufBuilder.

@Test
public void testProtobufBuilder() {
    // TEST Pb Service metaData builder
    FullServiceDefinition serviceDefinition = ServiceDefinitionBuilder.buildFullDefinition(ServiceInterface.class);
    MethodDefinition methodDefinition = serviceDefinition.getMethods().get(0);
    String parameterName = methodDefinition.getParameterTypes()[0];
    TypeDefinition typeDefinition = null;
    for (TypeDefinition type : serviceDefinition.getTypes()) {
        if (parameterName.equals(type.getType())) {
            typeDefinition = type;
            break;
        }
    }
    Map<String, TypeDefinition> propertiesMap = typeDefinition.getProperties();
    assertThat(propertiesMap.size(), is(11));
    assertThat(propertiesMap.containsKey("money"), is(true));
    assertThat(propertiesMap.get("money").getType(), equalTo("double"));
    assertThat(propertiesMap.containsKey("cash"), is(true));
    assertThat(propertiesMap.get("cash").getType(), equalTo("float"));
    assertThat(propertiesMap.containsKey("age"), is(true));
    assertThat(propertiesMap.get("age").getType(), equalTo("int"));
    assertThat(propertiesMap.containsKey("num"), is(true));
    assertThat(propertiesMap.get("num").getType(), equalTo("long"));
    assertThat(propertiesMap.containsKey("sex"), is(true));
    assertThat(propertiesMap.get("sex").getType(), equalTo("boolean"));
    assertThat(propertiesMap.containsKey("name"), is(true));
    assertThat(propertiesMap.get("name").getType(), equalTo("java.lang.String"));
    assertThat(propertiesMap.containsKey("msg"), is(true));
    assertThat(propertiesMap.get("msg").getType(), equalTo("com.google.protobuf.ByteString"));
    assertThat(propertiesMap.containsKey("phone"), is(true));
    assertThat(propertiesMap.get("phone").getType(), equalTo("java.util.List<org.apache.dubbo.metadata.definition.protobuf.model.GooglePB$PhoneNumber>"));
    assertThat(propertiesMap.containsKey("doubleMap"), is(true));
    assertThat(propertiesMap.get("doubleMap").getType(), equalTo("java.util.Map<java.lang.String,org.apache.dubbo.metadata.definition.protobuf.model.GooglePB$PhoneNumber>"));
    assertThat(propertiesMap.get("bytesList").getType(), equalTo("java.util.List<com.google.protobuf.ByteString>"));
    assertThat(propertiesMap.get("bytesMap").getType(), equalTo("java.util.Map<java.lang.String,com.google.protobuf.ByteString>"));
}
Also used : FullServiceDefinition(org.apache.dubbo.metadata.definition.model.FullServiceDefinition) MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with MethodDefinition

use of org.apache.dubbo.metadata.definition.model.MethodDefinition 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 4 with MethodDefinition

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

the class AbstractServiceRestMetadataResolver method processRestMethodMetadata.

/**
 * Process the single {@link RestMethodMetadata} by the specified {@link Consumer} if present
 *
 * @param serviceMethod         Dubbo Service method
 * @param serviceType           Dubbo Service interface or type
 * @param serviceInterfaceClass The type of Dubbo Service interface
 * @param metadataToProcess     {@link RestMethodMetadata} to process if present
 * @return if processed successfully, return <code>true</code>, or <code>false</code>
 */
protected boolean processRestMethodMetadata(Method serviceMethod, Class<?> serviceType, Class<?> serviceInterfaceClass, Consumer<RestMethodMetadata> metadataToProcess) {
    if (!isRestCapableMethod(serviceMethod, serviceType, serviceInterfaceClass)) {
        return false;
    }
    // requestPath is required
    String requestPath = resolveRequestPath(serviceMethod, serviceType, serviceInterfaceClass);
    if (requestPath == null) {
        return false;
    }
    // requestMethod is required
    String requestMethod = resolveRequestMethod(serviceMethod, serviceType, serviceInterfaceClass);
    if (requestMethod == null) {
        return false;
    }
    RestMethodMetadata metadata = new RestMethodMetadata();
    MethodDefinition methodDefinition = resolveMethodDefinition(serviceMethod, serviceType, serviceInterfaceClass);
    // Set MethodDefinition
    metadata.setMethod(methodDefinition);
    // process the annotated method parameters
    processAnnotatedMethodParameters(serviceMethod, serviceType, serviceInterfaceClass, metadata);
    // process produces
    Set<String> produces = new LinkedHashSet<>();
    processProduces(serviceMethod, serviceType, serviceInterfaceClass, produces);
    // process consumes
    Set<String> consumes = new LinkedHashSet<>();
    processConsumes(serviceMethod, serviceType, serviceInterfaceClass, consumes);
    // Initialize RequestMetadata
    RequestMetadata request = metadata.getRequest();
    request.setPath(requestPath);
    request.setMethod(requestMethod);
    request.setProduces(produces);
    request.setConsumes(consumes);
    // Post-Process
    postResolveRestMethodMetadata(serviceMethod, serviceType, serviceInterfaceClass, metadata);
    // Accept RestMethodMetadata
    metadataToProcess.accept(metadata);
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition)

Example 5 with MethodDefinition

use of org.apache.dubbo.metadata.definition.model.MethodDefinition 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)

Aggregations

MethodDefinition (org.apache.dubbo.metadata.definition.model.MethodDefinition)11 TypeDefinition (org.apache.dubbo.metadata.definition.model.TypeDefinition)5 FullServiceDefinition (org.apache.dubbo.metadata.definition.model.FullServiceDefinition)4 Type (java.lang.reflect.Type)3 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 MetadataIdentifier (org.apache.dubbo.metadata.report.identifier.MetadataIdentifier)2 Test (org.junit.jupiter.api.Test)2 Gson (com.google.gson.Gson)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 MethodMetadata (org.apache.dubbo.admin.model.domain.MethodMetadata)1 URL (org.apache.dubbo.common.URL)1 ServiceDefinition (org.apache.dubbo.metadata.definition.model.ServiceDefinition)1 ComplexObject (org.apache.dubbo.metadata.definition.service.ComplexObject)1 RequestMetadata (org.apache.dubbo.metadata.rest.RequestMetadata)1 RestMethodMetadata (org.apache.dubbo.metadata.rest.RestMethodMetadata)1