Search in sources :

Example 1 with TypeDefinition

use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project incubator-dubbo-ops by apache.

the class ServiceTestV3Util method generateEnclosedType.

private static void generateEnclosedType(Map<String, Object> holder, String key, ServiceDefinition sd, String type) {
    if (isPrimitiveType(type)) {
        holder.put(key, generateType(sd, type));
    } else {
        TypeDefinition td = findTypeDefinition(sd, type);
        if (td.getProperties() == null || td.getProperties().size() == 0) {
            holder.put(key, generateType(sd, td));
        } else {
            Map<String, Object> enclosedMap = new HashMap<>();
            holder.put(key, enclosedMap);
            generateComplexType(sd, td, enclosedMap);
        }
    }
}
Also used : HashMap(java.util.HashMap) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition)

Example 2 with TypeDefinition

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

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

the class GeneralTypeDefinitionBuilder method buildProperties.

protected void buildProperties(ProcessingEnvironment processingEnv, TypeElement type, TypeDefinition definition) {
    getNonStaticFields(type).forEach(field -> {
        String fieldName = field.getSimpleName().toString();
        TypeDefinition propertyType = TypeDefinitionBuilder.build(processingEnv, field);
        if (propertyType != null) {
            definition.getProperties().put(fieldName, propertyType);
        }
    });
}
Also used : TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition)

Example 4 with TypeDefinition

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

the class TypeDefinitionBuilder method build.

/**
 * Build the instance of {@link TypeDefinition} from the specified {@link TypeMirror type}
 *
 * @param processingEnv {@link ProcessingEnvironment}
 * @param type          {@link TypeMirror type}
 * @return non-null
 */
static TypeDefinition build(ProcessingEnvironment processingEnv, TypeMirror type) {
    String typeName = type.toString();
    TypeDefinition typeDefinition = new TypeDefinition(typeName);
    // Build by all instances of TypeDefinitionBuilder that were loaded By Java SPI
    getExtensionLoader(TypeDefinitionBuilder.class).getSupportedExtensionInstances().stream().filter(builder -> builder.accept(processingEnv, type)).findFirst().ifPresent(builder -> {
        builder.build(processingEnv, type, typeDefinition);
    // typeDefinition.setTypeBuilderName(builder.getClass().getName());
    });
    return typeDefinition;
}
Also used : TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition)

Example 5 with TypeDefinition

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

the class MetadataTest method testRawMap.

/**
 */
@Test
public void testRawMap() {
    TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
    TypeDefinition td = builder.build(ResultWithRawCollections.class, ResultWithRawCollections.class);
    System.out.println(">> testRawMap: " + new Gson().toJson(td));
    Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.ResultWithRawCollections", td.getType());
    Assertions.assertEquals(2, td.getProperties().size());
    Assertions.assertEquals("java.util.Map", td.getProperties().get("map").getType());
    Assertions.assertEquals(MapTypeBuilder.class.getName(), td.getProperties().get("map").getTypeBuilderName());
    Assertions.assertEquals("java.util.List", td.getProperties().get("list").getType());
    Assertions.assertEquals(CollectionTypeBuilder.class.getName(), td.getProperties().get("list").getTypeBuilderName());
    ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class);
    System.out.println(">> testRawMap: " + new Gson().toJson(sd));
    Assertions.assertEquals(TestService.class.getName(), sd.getCanonicalName());
    Assertions.assertEquals(TestService.class.getMethods().length, sd.getMethods().size());
    boolean containsType = false;
    for (TypeDefinition type : sd.getTypes()) {
        if (type.getType().equals("org.apache.dubbo.metadata.definition.common.ResultWithRawCollections")) {
            containsType = true;
            break;
        }
    }
    Assertions.assertTrue(containsType);
}
Also used : TestService(org.apache.dubbo.metadata.definition.common.TestService) CollectionTypeBuilder(org.apache.dubbo.metadata.definition.builder.CollectionTypeBuilder) Gson(com.google.gson.Gson) MapTypeBuilder(org.apache.dubbo.metadata.definition.builder.MapTypeBuilder) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition) Test(org.junit.jupiter.api.Test)

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