Skip to content

Commit 08d5f15

Browse files
cvictorychickenlj
authored andcommitted
Merge pull request #3639, Add equivalent annotation support for MethodConfig.
Fixes #2045
1 parent e4bfb3d commit 08d5f15

File tree

15 files changed

+320
-7
lines changed

15 files changed

+320
-7
lines changed

dubbo-config/dubbo-config-api/src/main/java/org/apache/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 org.apache.dubbo.config;
1818

19+
import org.apache.dubbo.config.annotation.Argument;
1920
import org.apache.dubbo.config.support.Parameter;
2021

2122
import java.io.Serializable;
@@ -44,6 +45,15 @@ public class ArgumentConfig implements Serializable {
4445
*/
4546
private Boolean callback;
4647

48+
public ArgumentConfig() {
49+
}
50+
51+
public ArgumentConfig(Argument argument) {
52+
this.index = argument.index();
53+
this.type = argument.type();
54+
this.callback = argument.callback();
55+
}
56+
4757
@Parameter(excluded = true)
4858
public Integer getIndex() {
4959
return index;
@@ -70,4 +80,4 @@ public Boolean isCallback() {
7080
return callback;
7181
}
7282

73-
}
83+
}

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MethodConfig.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
import org.apache.dubbo.common.Constants;
2020
import org.apache.dubbo.common.utils.StringUtils;
21+
import org.apache.dubbo.config.annotation.Method;
2122
import org.apache.dubbo.config.support.Parameter;
2223

24+
import java.util.ArrayList;
25+
import java.util.Collections;
2326
import java.util.List;
2427

2528
/**
@@ -117,6 +120,46 @@ public String getName() {
117120
return name;
118121
}
119122

123+
public MethodConfig() {
124+
}
125+
126+
public MethodConfig(Method method) {
127+
appendAnnotation(Method.class, method);
128+
129+
this.setReturn(method.isReturn());
130+
131+
if(!"".equals(method.oninvoke())){
132+
this.setOninvoke(method.oninvoke());
133+
}
134+
if(!"".equals(method.onreturn())){
135+
this.setOnreturn(method.onreturn());
136+
}
137+
if(!"".equals(method.onthrow())){
138+
this.setOnthrow(method.onthrow());
139+
}
140+
141+
if (method.arguments() != null && method.arguments().length != 0) {
142+
List<ArgumentConfig> argumentConfigs = new ArrayList<ArgumentConfig>(method.arguments().length);
143+
this.setArguments(argumentConfigs);
144+
for (int i = 0; i < method.arguments().length; i++) {
145+
ArgumentConfig argumentConfig = new ArgumentConfig(method.arguments()[i]);
146+
argumentConfigs.add(argumentConfig);
147+
}
148+
}
149+
}
150+
151+
public static List<MethodConfig> constructMethodConfig(Method[] methods) {
152+
if (methods != null && methods.length != 0) {
153+
List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>(methods.length);
154+
for (int i = 0; i < methods.length; i++) {
155+
MethodConfig methodConfig = new MethodConfig(methods[i]);
156+
methodConfigs.add(methodConfig);
157+
}
158+
return methodConfigs;
159+
}
160+
return Collections.emptyList();
161+
}
162+
120163
public void setName(String name) {
121164
checkMethodName("name", name);
122165
this.name = name;
@@ -279,4 +322,4 @@ public String getPrefix() {
279322
+ (StringUtils.isEmpty(serviceId) ? "" : ("." + serviceId))
280323
+ "." + getName();
281324
}
282-
}
325+
}

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java

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

179179
public ReferenceConfig(Reference reference) {
180180
appendAnnotation(Reference.class, reference);
181+
setMethods(MethodConfig.constructMethodConfig(reference.methods()));
181182
}
182183

183184
public URL toUrl() {

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java

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

170170
public ServiceConfig(Service service) {
171171
appendAnnotation(Service.class, service);
172+
setMethods(MethodConfig.constructMethodConfig(service.methods()));
172173
}
173174

174175
@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 org.apache.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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 org.apache.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 Method {
36+
String name();
37+
38+
int timeout() default -1;
39+
40+
int retries() default -1;
41+
42+
String loadbalance() default "";
43+
44+
boolean async() default false;
45+
46+
boolean sent() default true;
47+
48+
int actives() default 0;
49+
50+
int executes() default 0;
51+
52+
boolean deprecated() default false;
53+
54+
boolean sticky() default false;
55+
56+
boolean isReturn() default true;
57+
58+
String oninvoke() default "";
59+
60+
String onreturn() default "";
61+
62+
String onthrow() default "";
63+
64+
String cache() default "";
65+
66+
String validation() default "";
67+
68+
Argument[] arguments() default {};
69+
}

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Reference.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,10 @@
256256
* Protocol spring bean names
257257
*/
258258
String protocol() default "";
259+
260+
/**
261+
* methods support
262+
* @return
263+
*/
264+
Method[] methods() default {};
259265
}

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Service.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,10 @@
268268
* Service tag name
269269
*/
270270
String tag() default "";
271+
272+
/**
273+
* methods support
274+
* @return
275+
*/
276+
Method[] methods() default {};
271277
}

dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractConfigTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,12 @@ public void tetMetaData() {
467467
Assertions.assertNull(metaData.get("key2"));
468468
}
469469

470+
@Retention(RetentionPolicy.RUNTIME)
471+
@Target({ElementType.ANNOTATION_TYPE})
472+
public @interface ConfigField {
473+
String value() default "";
474+
}
475+
470476
@Retention(RetentionPolicy.RUNTIME)
471477
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
472478
public @interface Config {
@@ -479,6 +485,10 @@ public void tetMetaData() {
479485
String[] listener() default {};
480486

481487
String[] parameters() default {};
488+
489+
ConfigField[] configFields() default {};
490+
491+
ConfigField configField() default @ConfigField;
482492
}
483493

484494
private static class OverrideConfig extends AbstractInterfaceConfig {
@@ -735,6 +745,7 @@ private static class AnnotationConfig extends AbstractConfig {
735745
private String filter;
736746
private String listener;
737747
private Map<String, String> parameters;
748+
private String[] configFields;
738749

739750
public Class getInterface() {
740751
return interfaceClass;
@@ -767,5 +778,13 @@ public Map<String, String> getParameters() {
767778
public void setParameters(Map<String, String> parameters) {
768779
this.parameters = parameters;
769780
}
781+
782+
public String[] getConfigFields() {
783+
return configFields;
784+
}
785+
786+
public void setConfigFields(String[] configFields) {
787+
this.configFields = configFields;
788+
}
770789
}
771790
}

dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MethodConfigTest.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,75 @@
1818
package org.apache.dubbo.config;
1919

2020
import org.apache.dubbo.common.Constants;
21+
import org.apache.dubbo.config.annotation.Argument;
22+
import org.apache.dubbo.config.annotation.Method;
23+
import org.apache.dubbo.config.annotation.Reference;
2124

2225
import org.hamcrest.Matchers;
2326
import org.junit.jupiter.api.Test;
2427

2528
import java.util.Collections;
2629
import java.util.HashMap;
30+
import java.util.List;
2731
import java.util.Map;
2832

33+
import static org.hamcrest.MatcherAssert.assertThat;
2934
import static org.hamcrest.Matchers.contains;
3035
import static org.hamcrest.Matchers.equalTo;
3136
import static org.hamcrest.Matchers.hasEntry;
3237
import static org.hamcrest.Matchers.hasKey;
3338
import static org.hamcrest.Matchers.is;
3439
import static org.hamcrest.Matchers.not;
35-
import static org.hamcrest.MatcherAssert.assertThat;
3640

3741
public class MethodConfigTest {
42+
private static final String METHOD_NAME = "sayHello";
43+
private static final int TIMEOUT = 1300;
44+
private static final int RETRIES = 4;
45+
private static final String LOADBALANCE = "random";
46+
private static final boolean ASYNC = true;
47+
private static final int ACTIVES = 3;
48+
private static final int EXECUTES = 5;
49+
private static final boolean DEPERECATED = true;
50+
private static final boolean STICKY = true;
51+
private static final String ONINVOKE = "i";
52+
private static final String ONTHROW = "t";
53+
private static final String ONRETURN = "r";
54+
private static final String CACHE = "c";
55+
private static final String VALIDATION = "v";
56+
private static final int ARGUMENTS_INDEX = 24;
57+
private static final boolean ARGUMENTS_CALLBACK = true;
58+
private static final String ARGUMENTS_TYPE = "sss";
59+
60+
@Reference(methods = {@Method(name = METHOD_NAME, timeout = TIMEOUT, retries = RETRIES, loadbalance = LOADBALANCE, async = ASYNC,
61+
actives = ACTIVES, executes = EXECUTES, deprecated = DEPERECATED, sticky = STICKY, oninvoke = ONINVOKE, onthrow = ONTHROW, onreturn = ONRETURN, cache = CACHE, validation = VALIDATION,
62+
arguments = {@Argument(index = ARGUMENTS_INDEX, callback = ARGUMENTS_CALLBACK, type = ARGUMENTS_TYPE)})})
63+
private String testField;
64+
65+
@Test
66+
public void testStaticConstructor() throws NoSuchFieldException {
67+
Method[] methods = this.getClass().getDeclaredField("testField").getAnnotation(Reference.class).methods();
68+
List<MethodConfig> methodConfigs = MethodConfig.constructMethodConfig(methods);
69+
MethodConfig methodConfig = methodConfigs.get(0);
70+
71+
assertThat(METHOD_NAME, equalTo(methodConfig.getName()));
72+
assertThat(TIMEOUT, equalTo(methodConfig.getTimeout().intValue()));
73+
assertThat(RETRIES, equalTo(methodConfig.getRetries().intValue()));
74+
assertThat(LOADBALANCE, equalTo(methodConfig.getLoadbalance()));
75+
assertThat(ASYNC, equalTo(methodConfig.isAsync()));
76+
assertThat(ACTIVES, equalTo(methodConfig.getActives().intValue()));
77+
assertThat(EXECUTES, equalTo(methodConfig.getExecutes().intValue()));
78+
assertThat(DEPERECATED, equalTo(methodConfig.getDeprecated()));
79+
assertThat(STICKY, equalTo(methodConfig.getSticky()));
80+
assertThat(ONINVOKE, equalTo(methodConfig.getOninvoke()));
81+
assertThat(ONTHROW, equalTo(methodConfig.getOnthrow()));
82+
assertThat(ONRETURN, equalTo(methodConfig.getOnreturn()));
83+
assertThat(CACHE, equalTo(methodConfig.getCache()));
84+
assertThat(VALIDATION, equalTo(methodConfig.getValidation()));
85+
assertThat(ARGUMENTS_INDEX, equalTo(methodConfig.getArguments().get(0).getIndex().intValue()));
86+
assertThat(ARGUMENTS_CALLBACK, equalTo(methodConfig.getArguments().get(0).isCallback()));
87+
assertThat(ARGUMENTS_TYPE, equalTo(methodConfig.getArguments().get(0).getType()));
88+
}
89+
3890
@Test
3991
public void testName() throws Exception {
4092
MethodConfig method = new MethodConfig();

0 commit comments

Comments
 (0)