Search in sources :

Example 6 with MethodDefinition

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

the class MethodDefinitionBuilder method build.

/**
 * Build the instance of {@link MethodDefinition}
 *
 * @param method {@link Method}
 * @return non-null
 */
public MethodDefinition build(Method method) {
    MethodDefinition md = new MethodDefinition();
    md.setName(method.getName());
    // Process the parameters
    Class<?>[] paramTypes = method.getParameterTypes();
    Type[] genericParamTypes = method.getGenericParameterTypes();
    int paramSize = paramTypes.length;
    String[] parameterTypes = new String[paramSize];
    List<TypeDefinition> parameters = new ArrayList<>(paramSize);
    for (int i = 0; i < paramSize; i++) {
        TypeDefinition parameter = builder.build(genericParamTypes[i], paramTypes[i]);
        parameterTypes[i] = parameter.getType();
        parameters.add(parameter);
    }
    md.setParameterTypes(parameterTypes);
    md.setParameters(parameters);
    // Process return type.
    TypeDefinition td = builder.build(method.getGenericReturnType(), method.getReturnType());
    md.setReturnType(td.getType());
    return md;
}
Also used : Type(java.lang.reflect.Type) MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition) ArrayList(java.util.ArrayList) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition)

Example 7 with MethodDefinition

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

the class MethodDefinitionBuilder method build.

static MethodDefinition build(ProcessingEnvironment processingEnv, ExecutableElement method) {
    MethodDefinition methodDefinition = new MethodDefinition();
    methodDefinition.setName(getMethodName(method));
    methodDefinition.setReturnType(getReturnType(method));
    methodDefinition.setParameterTypes(getMethodParameterTypes(method));
    methodDefinition.setParameters(getMethodParameters(processingEnv, method));
    return methodDefinition;
}
Also used : MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition)

Example 8 with MethodDefinition

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

the class MetadataUtils method generateMetadata.

/**
 * com.taobao.hsf.metadata.store.MetadataInfoStoreServiceRedis.publishClassInfo(ServiceMetadata) 生成元数据的代码
 */
public static ServiceDefinition generateMetadata(Class<?> interfaceClass) {
    ServiceDefinition sd = new ServiceDefinition();
    sd.setCanonicalName(interfaceClass.getCanonicalName());
    sd.setCodeSource(ClassUtils.getCodeSource(interfaceClass));
    TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
    List<Method> methods = ClassUtils.getPublicNonStaticMethods(interfaceClass);
    for (Method method : methods) {
        MethodDefinition md = new MethodDefinition();
        md.setName(method.getName());
        Class<?>[] paramTypes = method.getParameterTypes();
        Type[] genericParamTypes = method.getGenericParameterTypes();
        String[] parameterTypes = new String[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++) {
            try {
                TypeDefinition td = builder.build(genericParamTypes[i], paramTypes[i]);
                parameterTypes[i] = td.getType();
            } catch (Exception e) {
                parameterTypes[i] = paramTypes[i].getName();
            }
        }
        md.setParameterTypes(parameterTypes);
        try {
            TypeDefinition td = builder.build(method.getGenericReturnType(), method.getReturnType());
            md.setReturnType(td.getType());
        } catch (Exception e) {
            md.setReturnType(method.getReturnType().getName());
        }
        sd.getMethods().add(md);
    }
    sd.setTypes(builder.getTypeDefinitions());
    return sd;
}
Also used : Method(java.lang.reflect.Method) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition) Type(java.lang.reflect.Type) MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition)

Example 9 with MethodDefinition

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

the class GenericServiceTest method testGenericComplexCompute4FullServiceMetadata.

@Test
public void testGenericComplexCompute4FullServiceMetadata() {
    DemoService server = new DemoServiceImpl();
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("dubbo://127.0.0.1:5342/" + DemoService.class.getName() + "?version=1.0.0&generic=true$timeout=3000");
    Exporter<DemoService> exporter = protocol.export(proxyFactory.getInvoker(server, DemoService.class, url));
    String var1 = "v1";
    int var2 = 234;
    long l = 555;
    String[] var3 = { "var31", "var32" };
    List<Integer> var4 = Arrays.asList(2, 4, 8);
    ComplexObject.TestEnum testEnum = ComplexObject.TestEnum.VALUE2;
    FullServiceDefinition fullServiceDefinition = ServiceDefinitionBuilder.buildFullDefinition(DemoService.class);
    MethodDefinition methodDefinition = getMethod("complexCompute", fullServiceDefinition.getMethods());
    Map mapObject = createComplexObject(fullServiceDefinition, var1, var2, l, var3, var4, testEnum);
    ComplexObject complexObject = map2bean(mapObject);
    Invoker<GenericService> invoker = protocol.refer(GenericService.class, url);
    GenericService client = proxyFactory.getProxy(invoker, true);
    Object result = client.$invoke(methodDefinition.getName(), methodDefinition.getParameterTypes(), new Object[] { "haha", mapObject });
    Assertions.assertEquals("haha###" + complexObject.toString(), result);
    Invoker<DemoService> invoker2 = protocol.refer(DemoService.class, url);
    GenericService client2 = (GenericService) proxyFactory.getProxy(invoker2, true);
    Object result2 = client2.$invoke("complexCompute", methodDefinition.getParameterTypes(), new Object[] { "haha2", mapObject });
    Assertions.assertEquals("haha2###" + complexObject.toString(), result2);
    invoker.destroy();
    exporter.unexport();
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) DemoService(org.apache.dubbo.service.DemoService) URL(org.apache.dubbo.common.URL) FullServiceDefinition(org.apache.dubbo.metadata.definition.model.FullServiceDefinition) ComplexObject(org.apache.dubbo.service.ComplexObject) MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition) ComplexObject(org.apache.dubbo.service.ComplexObject) Protocol(org.apache.dubbo.rpc.Protocol) HashMap(java.util.HashMap) Map(java.util.Map) DemoServiceImpl(org.apache.dubbo.service.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Example 10 with MethodDefinition

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

the class ServiceTestController method methodDetail.

@RequestMapping(value = "/method", method = RequestMethod.GET)
public MethodMetadata methodDetail(@PathVariable String env, @RequestParam String application, @RequestParam String service, @RequestParam String method) {
    Map<String, String> info = ConvertUtil.serviceName2Map(service);
    MetadataIdentifier identifier = new MetadataIdentifier(info.get(Constants.INTERFACE_KEY), info.get(Constants.VERSION_KEY), info.get(Constants.GROUP_KEY), Constants.PROVIDER_SIDE, application);
    String metadata = providerService.getProviderMetaData(identifier);
    MethodMetadata methodMetadata = null;
    if (metadata != null) {
        Gson gson = new Gson();
        String release = providerService.findVersionInApplication(application);
        if (release.startsWith("2.")) {
            org.apache.dubbo.admin.model.domain.FullServiceDefinition serviceDefinition = gson.fromJson(metadata, org.apache.dubbo.admin.model.domain.FullServiceDefinition.class);
            List<org.apache.dubbo.admin.model.domain.MethodDefinition> methods = serviceDefinition.getMethods();
            if (methods != null) {
                for (org.apache.dubbo.admin.model.domain.MethodDefinition m : methods) {
                    if (ServiceTestUtil.sameMethod(m, method)) {
                        methodMetadata = ServiceTestUtil.generateMethodMeta(serviceDefinition, m);
                        break;
                    }
                }
            }
        } else {
            FullServiceDefinition serviceDefinition = gson.fromJson(metadata, FullServiceDefinition.class);
            List<MethodDefinition> methods = serviceDefinition.getMethods();
            if (methods != null) {
                for (MethodDefinition m : methods) {
                    if (ServiceTestV3Util.sameMethod(m, method)) {
                        methodMetadata = ServiceTestV3Util.generateMethodMeta(serviceDefinition, m);
                        break;
                    }
                }
            }
        }
    }
    return methodMetadata;
}
Also used : Gson(com.google.gson.Gson) MetadataIdentifier(org.apache.dubbo.metadata.report.identifier.MetadataIdentifier) FullServiceDefinition(org.apache.dubbo.metadata.definition.model.FullServiceDefinition) MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition) MethodMetadata(org.apache.dubbo.admin.model.domain.MethodMetadata) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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