Skip to content

Commit

Permalink
init method parameters list when build ServiceDefinition,add typeBuil…
Browse files Browse the repository at this point in the history
…derName in TypeDefinition.
  • Loading branch information
vio-lin committed Jun 24, 2019
1 parent 041a6ad commit 216ee79
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -73,11 +74,14 @@ public static <T extends ServiceDefinition> void build(T sd, final Class<?> inte
Type[] genericParamTypes = method.getGenericParameterTypes();

String[] parameterTypes = new String[paramTypes.length];
List<TypeDefinition> parameters = new ArrayList<>();
for (int i = 0; i < paramTypes.length; i++) {
TypeDefinition td = builder.build(genericParamTypes[i], paramTypes[i]);
parameterTypes[i] = td.getType();
parameters.add(td);
}
md.setParameterTypes(parameterTypes);
md.setParameters(parameters);

// Process return type.
TypeDefinition td = builder.build(method.getGenericReturnType(), method.getReturnType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
public class TypeDefinitionBuilder {
private static final List<TypeBuilder> BUILDERS;

static{
static {
List<TypeBuilder> builders = new ArrayList<>();
ExtensionLoader<TypeBuilder> extensionLoader = ExtensionLoader.getExtensionLoader(TypeBuilder.class);
for(String extensionName : extensionLoader.getSupportedExtensions()){
for (String extensionName : extensionLoader.getSupportedExtensions()) {
builders.add(extensionLoader.getExtension(extensionName));
}
BUILDERS = builders;
Expand All @@ -47,8 +47,10 @@ public static TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, Type
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class TypeDefinition {
private List<String> enums;
private String $ref;
private Map<String, TypeDefinition> properties;
private String typeBuilderName;

public TypeDefinition(String type) {
this.type = type;
Expand Down Expand Up @@ -75,6 +76,10 @@ public String getType() {
return type;
}

public String getTypeBuilderName() {
return typeBuilderName;
}

public void set$ref(String $ref) {
this.$ref = $ref;
}
Expand All @@ -99,6 +104,10 @@ public void setType(String type) {
this.type = type;
}

public void setTypeBuilderName(String typeBuilderName) {
this.typeBuilderName = typeBuilderName;
}

@Override
public String toString() {
return "TypeDefinition [id=" + id + ", type=" + type + ", properties=" + properties + ", $ref=" + $ref + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package org.apache.dubbo.metadata.definition;

import org.apache.dubbo.metadata.definition.builder.CollectionTypeBuilder;
import org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder;
import org.apache.dubbo.metadata.definition.builder.EnumTypeBuilder;
import org.apache.dubbo.metadata.definition.builder.MapTypeBuilder;
import org.apache.dubbo.metadata.definition.common.ClassExtendsMap;
import org.apache.dubbo.metadata.definition.common.ColorEnum;
import org.apache.dubbo.metadata.definition.common.OuterClass;
Expand Down Expand Up @@ -46,7 +50,7 @@ public void testInnerClassType() {
Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.OuterClass$InnerClass", td.getType());
Assertions.assertEquals(1, td.getProperties().size());
Assertions.assertNotNull(td.getProperties().get("name"));

Assertions.assertEquals(DefaultTypeBuilder.class.getName(), td.getTypeBuilderName());
ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class);
System.out.println(">> testInnerClassType: " + new Gson().toJson(sd));

Expand All @@ -73,7 +77,9 @@ public void testRawMap() {
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));
Expand All @@ -97,6 +103,7 @@ public void testEnum() {
System.out.println(">> testEnum: " + new Gson().toJson(td));

Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.ColorEnum", td.getType());
Assertions.assertEquals(EnumTypeBuilder.class.getName(), td.getTypeBuilderName());
Assertions.assertEquals(3, td.getEnums().size());
Assertions.assertTrue(td.getEnums().contains("RED"));
Assertions.assertTrue(td.getEnums().contains("YELLOW"));
Expand Down Expand Up @@ -124,6 +131,7 @@ public void testExtendsMap() {
System.out.println(">> testExtendsMap: " + new Gson().toJson(td));

Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.ClassExtendsMap", td.getType());
Assertions.assertEquals(MapTypeBuilder.class.getName(), td.getTypeBuilderName());
Assertions.assertEquals(0, td.getProperties().size());

ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ public class ServiceDefinitionBuildderTest {
public void testBuilderComplextObject() {
FullServiceDefinition fullServiceDefinition = ServiceDefinitionBuilder.buildFullDefinition(DemoService.class);
checkComplextObjectAsParam(fullServiceDefinition);
checkMethodParameterTypeDefinition(fullServiceDefinition);
}

private void checkMethodParameterTypeDefinition(FullServiceDefinition fullServiceDefinition) {
List<MethodDefinition> methodDefinitions = fullServiceDefinition.getMethods();
MethodDefinition complexComputeMethod = methodDefinitions.stream().filter(
a-> a.getName().equals("complexCompute")).findFirst().get();
Assertions.assertEquals(2, complexComputeMethod.getParameters().size());
Assertions.assertEquals(String.class.getName(), complexComputeMethod.getParameters().get(0).getType());
Assertions.assertEquals(ComplexObject.class.getName(), complexComputeMethod.getParameters().get(1).getType());

MethodDefinition findComplexObjectMethodDefintion = methodDefinitions.stream().filter(
a-> a.getName().equals("findComplexObject")).findFirst().get();
Assertions.assertEquals(6, findComplexObjectMethodDefintion.getParameters().size());
Assertions.assertEquals(String.class.getName(), findComplexObjectMethodDefintion.getParameters().get(0).getType());
Assertions.assertEquals("int", findComplexObjectMethodDefintion.getParameters().get(1).getType());
Assertions.assertEquals("long", findComplexObjectMethodDefintion.getParameters().get(2).getType());
Assertions.assertEquals("java.lang.String[]", findComplexObjectMethodDefintion.getParameters().get(3).getType());
Assertions.assertEquals("java.util.List<java.lang.Integer>", findComplexObjectMethodDefintion.getParameters().get(4).getType());
Assertions.assertEquals("org.apache.dubbo.metadata.definition.service.ComplexObject.TestEnum", findComplexObjectMethodDefintion.getParameters().get(5).getType());
}


Expand Down

0 comments on commit 216ee79

Please sign in to comment.