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);
}
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>"));
}
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");
}
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;
}
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());
}
Aggregations