Skip to content

Commit c528d56

Browse files
cvictorynzomkxia
authored andcommitted
[Dubbo-2298] Add Annotation-Driven for MethodConfig and ArgumentConfig (#2603)
* support methodConfig and argementConfig. fix #2298 * unit test * unit test * remove unused * remove author * add licence * format the code
1 parent fec3363 commit c528d56

File tree

18 files changed

+445
-135
lines changed

18 files changed

+445
-135
lines changed

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ protected void appendAnnotation(Class<?> annotationClass, Object annotation) {
423423
}
424424
String setter = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
425425
Object value = method.invoke(annotation);
426-
if (value != null && !value.equals(method.getDefaultValue())) {
426+
if (!isAnnotationArray(method.getReturnType()) && value != null && !value.equals(method.getDefaultValue())) {
427427
Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType());
428428
if ("filter".equals(property) || "listener".equals(property)) {
429429
parameterType = String.class;
@@ -446,6 +446,13 @@ protected void appendAnnotation(Class<?> annotationClass, Object annotation) {
446446
}
447447
}
448448

449+
boolean isAnnotationArray(Class target) {
450+
if (target.isArray() && target.getComponentType().isAnnotation()) {
451+
return true;
452+
}
453+
return false;
454+
}
455+
449456
@Override
450457
public String toString() {
451458
try {

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ArgumentConfig.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.alibaba.dubbo.config;
1818

19+
import com.alibaba.dubbo.config.annotation.Argument;
1920
import com.alibaba.dubbo.config.support.Parameter;
2021

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

40+
public ArgumentConfig() {
41+
}
42+
43+
public ArgumentConfig(Argument argument) {
44+
this.index = argument.index();
45+
this.type = argument.type();
46+
this.callback = argument.callback();
47+
}
48+
3949
@Parameter(excluded = true)
4050
public Integer getIndex() {
4151
return index;
@@ -62,4 +72,4 @@ public Boolean isCallback() {
6272
return callback;
6373
}
6474

65-
}
75+
}

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/MethodConfig.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package com.alibaba.dubbo.config;
1818

1919
import com.alibaba.dubbo.common.Constants;
20+
import com.alibaba.dubbo.config.annotation.Method;
2021
import com.alibaba.dubbo.config.support.Parameter;
2122

23+
import java.util.ArrayList;
24+
import java.util.Collections;
2225
import java.util.List;
2326

2427
/**
@@ -74,6 +77,37 @@ public class MethodConfig extends AbstractMethodConfig {
7477

7578
private List<ArgumentConfig> arguments;
7679

80+
public MethodConfig() {
81+
}
82+
83+
public MethodConfig(Method method) {
84+
appendAnnotation(Method.class, method);
85+
this.setReturn(method.isReturn());
86+
this.setOninvoke(method.oninvoke());
87+
this.setOnreturn(method.onreturn());
88+
this.setOnthrow(method.onthrow());
89+
if (method.arguments() != null && method.arguments().length != 0) {
90+
List<ArgumentConfig> argumentConfigs = new ArrayList<ArgumentConfig>(method.arguments().length);
91+
this.setArguments(argumentConfigs);
92+
for (int i = 0; i < method.arguments().length; i++) {
93+
ArgumentConfig argumentConfig = new ArgumentConfig(method.arguments()[i]);
94+
argumentConfigs.add(argumentConfig);
95+
}
96+
}
97+
}
98+
99+
public static List<MethodConfig> constructMethodConfig(Method[] methods) {
100+
if (methods != null && methods.length != 0) {
101+
List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>(methods.length);
102+
for (int i = 0; i < methods.length; i++) {
103+
MethodConfig methodConfig = new MethodConfig(methods[i]);
104+
methodConfigs.add(methodConfig);
105+
}
106+
return methodConfigs;
107+
}
108+
return Collections.emptyList();
109+
}
110+
77111
@Parameter(excluded = true)
78112
public String getName() {
79113
return name;
@@ -211,4 +245,4 @@ public void setReturn(Boolean isReturn) {
211245
this.isReturn = isReturn;
212246
}
213247

214-
}
248+
}

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ReferenceConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public ReferenceConfig() {
112112

113113
public ReferenceConfig(Reference reference) {
114114
appendAnnotation(Reference.class, reference);
115+
setMethods(MethodConfig.constructMethodConfig(reference.methods()));
115116
}
116117

117118
private static void checkAndConvertImplicitConfig(MethodConfig method, Map<String, String> map, Map<Object, Object> attributes) {

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public ServiceConfig() {
101101

102102
public ServiceConfig(Service service) {
103103
appendAnnotation(Service.class, service);
104+
setMethods(MethodConfig.constructMethodConfig(service.methods()));
104105
}
105106

106107
@Deprecated
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.config.annotation;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
/**
27+
* @since 2.6.5
28+
*
29+
* 2018/9/29
30+
*/
31+
@Documented
32+
@Retention(RetentionPolicy.RUNTIME)
33+
@Target({ElementType.ANNOTATION_TYPE})
34+
@Inherited
35+
public @interface Argument {
36+
//argument: index -1 represents not set
37+
int index() default -1;
38+
39+
//argument type
40+
String type() default "";
41+
42+
//callback interface
43+
boolean callback() default false;
44+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.config.annotation;
18+
19+
20+
import java.lang.annotation.Documented;
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Inherited;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* @since 2.6.5
29+
* *
30+
* * 2018/9/29
31+
*/
32+
@Documented
33+
@Retention(RetentionPolicy.RUNTIME)
34+
@Target({ElementType.ANNOTATION_TYPE})
35+
@Inherited
36+
public @interface Method {
37+
String name();
38+
39+
int timeout() default -1;
40+
41+
int retries() default -1;
42+
43+
String loadbalance() default "";
44+
45+
boolean async() default false;
46+
47+
boolean sent() default true;
48+
49+
int actives() default 0;
50+
51+
int executes() default 0;
52+
53+
boolean deprecated() default false;
54+
55+
boolean sticky() default false;
56+
57+
boolean isReturn() default true;
58+
59+
String oninvoke() default "";
60+
61+
String onreturn() default "";
62+
63+
String onthrow() default "";
64+
65+
String cache() default "";
66+
67+
String validation() default "";
68+
69+
Argument[] arguments() default {};
70+
}

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118
* @return the default value is ""
119119
* @since 2.6.6
120120
*/
121-
String protocol() default "";
121+
String protocol() default "";
122+
123+
Method[] methods() default {};
122124

123-
}
125+
}

0 commit comments

Comments
 (0)