Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dubbo-2298] Add Annotation-Driven for MethodConfig and ArgumentConfig #2603

Merged
merged 14 commits into from Feb 25, 2019
Expand Up @@ -423,7 +423,7 @@ protected void appendAnnotation(Class<?> annotationClass, Object annotation) {
}
String setter = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
Object value = method.invoke(annotation);
if (value != null && !value.equals(method.getDefaultValue())) {
if (!isAnnotationArray(method.getReturnType()) && value != null && !value.equals(method.getDefaultValue())) {
Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType());
if ("filter".equals(property) || "listener".equals(property)) {
parameterType = String.class;
Expand All @@ -446,6 +446,13 @@ protected void appendAnnotation(Class<?> annotationClass, Object annotation) {
}
}

boolean isAnnotationArray(Class target) {
if (target.isArray() && target.getComponentType().isAnnotation()) {
return true;
}
return false;
}

@Override
public String toString() {
try {
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/
package com.alibaba.dubbo.config;

import com.alibaba.dubbo.config.annotation.Argument;
import com.alibaba.dubbo.config.support.Parameter;

import java.io.Serializable;
Expand All @@ -36,6 +37,15 @@ public class ArgumentConfig implements Serializable {
//callback interface
private Boolean callback;

public ArgumentConfig() {
}

public ArgumentConfig(Argument argument) {
this.index = argument.index();
this.type = argument.type();
this.callback = argument.callback();
}

@Parameter(excluded = true)
public Integer getIndex() {
return index;
Expand All @@ -62,4 +72,4 @@ public Boolean isCallback() {
return callback;
}

}
}
Expand Up @@ -17,8 +17,11 @@
package com.alibaba.dubbo.config;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.config.annotation.Method;
import com.alibaba.dubbo.config.support.Parameter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -74,6 +77,37 @@ public class MethodConfig extends AbstractMethodConfig {

private List<ArgumentConfig> arguments;

public MethodConfig() {
}

public MethodConfig(Method method) {
appendAnnotation(Method.class, method);
this.setReturn(method.isReturn());
this.setOninvoke(method.oninvoke());
this.setOnreturn(method.onreturn());
this.setOnthrow(method.onthrow());
if (method.arguments() != null && method.arguments().length != 0) {
List<ArgumentConfig> argumentConfigs = new ArrayList<ArgumentConfig>(method.arguments().length);
this.setArguments(argumentConfigs);
for (int i = 0; i < method.arguments().length; i++) {
ArgumentConfig argumentConfig = new ArgumentConfig(method.arguments()[i]);
argumentConfigs.add(argumentConfig);
}
}
}

public static List<MethodConfig> constructMethodConfig(Method[] methods) {
if (methods != null && methods.length != 0) {
List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>(methods.length);
for (int i = 0; i < methods.length; i++) {
MethodConfig methodConfig = new MethodConfig(methods[i]);
methodConfigs.add(methodConfig);
}
return methodConfigs;
}
return Collections.emptyList();
}

@Parameter(excluded = true)
public String getName() {
return name;
Expand Down Expand Up @@ -211,4 +245,4 @@ public void setReturn(Boolean isReturn) {
this.isReturn = isReturn;
}

}
}
Expand Up @@ -112,6 +112,7 @@ public ReferenceConfig() {

public ReferenceConfig(Reference reference) {
appendAnnotation(Reference.class, reference);
setMethods(MethodConfig.constructMethodConfig(reference.methods()));
}

private static void checkAndConvertImplicitConfig(MethodConfig method, Map<String, String> map, Map<Object, Object> attributes) {
Expand Down
Expand Up @@ -101,6 +101,7 @@ public ServiceConfig() {

public ServiceConfig(Service service) {
appendAnnotation(Service.class, service);
setMethods(MethodConfig.constructMethodConfig(service.methods()));
}

@Deprecated
Expand Down
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.config.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @since 2.6.5
*
* 2018/9/29
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE})
@Inherited
public @interface Argument {
//argument: index -1 represents not set
int index() default -1;

//argument type
String type() default "";

//callback interface
boolean callback() default false;
}
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.config.annotation;


import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @since 2.6.5
* *
* * 2018/9/29
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE})
@Inherited
public @interface Method {
String name();

int timeout() default -1;

int retries() default -1;

String loadbalance() default "";

boolean async() default false;

boolean sent() default true;

int actives() default 0;

int executes() default 0;

boolean deprecated() default false;

boolean sticky() default false;

boolean isReturn() default true;

String oninvoke() default "";

String onreturn() default "";

String onthrow() default "";

String cache() default "";

String validation() default "";

Argument[] arguments() default {};
}
Expand Up @@ -118,6 +118,8 @@
* @return the default value is ""
* @since 2.6.6
*/
String protocol() default "";
String protocol() default "";

Method[] methods() default {};

}
}