use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.
the class CollectionTypeBuilder method build.
@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 != 1) {
throw new IllegalArgumentException(MessageFormat.format("[ServiceDefinitionBuilder] Collection type [{0}] with unexpected amount of arguments [{1}]." + Arrays.toString(actualTypeArgs), type, actualTypeArgs));
}
Type actualType = actualTypeArgs[0];
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;
TypeDefinitionBuilder.build(null, actualClass, typeCache);
}
return new TypeDefinition(type.toString());
}
use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.
the class EnumTypeBuilder method build.
@Override
public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinition> typeCache) {
TypeDefinition td = new TypeDefinition(clazz.getCanonicalName());
try {
Method methodValues = clazz.getDeclaredMethod("values");
Object[] values = (Object[]) methodValues.invoke(clazz, new Object[0]);
int length = values.length;
for (int i = 0; i < length; i++) {
Object value = values[i];
td.getEnums().add(value.toString());
}
} catch (Throwable t) {
td.setId("-1");
}
typeCache.put(clazz, td);
return td;
}
use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.
the class MetadataTest method testExtendsMap.
@Test
public void testExtendsMap() {
TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
TypeDefinition td = builder.build(ClassExtendsMap.class, ClassExtendsMap.class);
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);
System.out.println(">> testExtendsMap: " + 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.ClassExtendsMap")) {
containsType = true;
break;
}
}
Assertions.assertFalse(containsType);
}
use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.
the class MetadataTest method testEnum.
@Test
public void testEnum() {
TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
TypeDefinition td = builder.build(ColorEnum.class, ColorEnum.class);
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"));
Assertions.assertTrue(td.getEnums().contains("BLUE"));
ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class);
System.out.println(">> testEnum: " + 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.ColorEnum")) {
containsType = true;
break;
}
}
Assertions.assertTrue(containsType);
}
use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.
the class MetadataTest method testInnerClassType.
/**
*/
@Test
public void testInnerClassType() {
TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
TypeDefinition td = builder.build(OuterClass.InnerClass.class, OuterClass.InnerClass.class);
System.out.println(">> testInnerClassType: " + new Gson().toJson(td));
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));
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.OuterClass$InnerClass")) {
containsType = true;
break;
}
}
Assertions.assertTrue(containsType);
}
Aggregations