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
Original file line number Diff line number Diff line change
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 @@ -445,6 +445,12 @@ protected void appendAnnotation(Class<?> annotationClass, Object annotation) {
}
}
}
boolean isAnnotationArray(Class target){
cvictory marked this conversation as resolved.
Show resolved Hide resolved
if(target.isArray() && target.getComponentType().isAnnotation()){
cvictory marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
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,14 @@ public class ArgumentConfig implements Serializable {
//callback interface
private Boolean callback;

public ArgumentConfig(){
cvictory marked this conversation as resolved.
Show resolved Hide resolved
}
public ArgumentConfig(Argument argument){
cvictory marked this conversation as resolved.
Show resolved Hide resolved
this.index = argument.index();
this.type = argument.type();
this.callback = argument.callback();
}

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

}
}
Original file line number Diff line number Diff line change
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;
}

}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public ServiceConfig() {

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

@Deprecated
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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 {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,6 @@

String[] registry() default {};

Method[] methods() default {};

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,6 @@

String[] registry() default {};

Method[] methods() default {};

}
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,24 @@ public void setFlag(byte flag) {
String[] listener() default {};

String[] parameters() default {};

ConfigField[] configFields() default {};

ConfigField configField() default @ConfigField;
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE})
public @interface ConfigField {
String value() default "";
}

private static class AnnotationConfig extends AbstractConfig {
private Class interfaceClass;
private String filter;
private String listener;
private Map<String, String> parameters;
private String[] configFields;

public Class getInterface() {
return interfaceClass;
Expand Down Expand Up @@ -513,5 +524,13 @@ public Map<String, String> getParameters() {
public void setParameters(Map<String, String> parameters) {
this.parameters = parameters;
}

public String[] getConfigFields() {
return configFields;
}

public void setConfigFields(String[] configFields) {
this.configFields = configFields;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.alibaba.dubbo.config;

import com.alibaba.dubbo.config.annotation.Argument;
import org.junit.Test;

import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
package com.alibaba.dubbo.config;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.config.annotation.Argument;
import com.alibaba.dubbo.config.annotation.Method;
import com.alibaba.dubbo.config.annotation.Reference;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.contains;
Expand All @@ -34,6 +39,54 @@
import static org.junit.Assert.assertThat;

public class MethodConfigTest {
private static final String METHOD_NAME = "sayHello";
private static final int TIMEOUT = 1300;
private static final int RETRIES = 4;
private static final String LOADBALANCE = "random";
private static final boolean ASYNC = true;
private static final int ACTIVES = 3;
private static final int EXECUTES = 5;
private static final boolean DEPERECATED = true;
private static final boolean STICKY = true;
private static final String ONINVOKE = "i";
private static final String ONTHROW = "t";
private static final String ONRETURN = "r";
private static final String CACHE = "c";
private static final String VALIDATION = "v";
private static final int ARGUMENTS_INDEX = 24;
private static final boolean ARGUMENTS_CALLBACK = true;
private static final String ARGUMENTS_TYPE = "sss";

@Reference(methods = {@Method(name = METHOD_NAME, timeout = TIMEOUT, retries = RETRIES, loadbalance = LOADBALANCE, async = ASYNC,
actives = ACTIVES, executes = EXECUTES, deprecated = DEPERECATED, sticky = STICKY, oninvoke = ONINVOKE, onthrow = ONTHROW, onreturn = ONRETURN, cache = CACHE, validation = VALIDATION,
arguments = {@Argument(index = ARGUMENTS_INDEX, callback = ARGUMENTS_CALLBACK, type = ARGUMENTS_TYPE)})})
private String testField;

@Test
public void testStaticConstructor() throws NoSuchFieldException {
Method[] methods = this.getClass().getDeclaredField("testField").getAnnotation(Reference.class).methods();
List<MethodConfig> methodConfigs = MethodConfig.constructMethodConfig(methods);
MethodConfig methodConfig = methodConfigs.get(0);

Assert.assertEquals(METHOD_NAME, methodConfig.getName());
Assert.assertEquals(methodConfig.getTimeout().intValue(), TIMEOUT);
Assert.assertEquals(RETRIES, methodConfig.getRetries().intValue());
Assert.assertEquals(LOADBALANCE, methodConfig.getLoadbalance());
Assert.assertEquals(ASYNC, methodConfig.isAsync());
Assert.assertEquals(ACTIVES, methodConfig.getActives().intValue());
Assert.assertEquals(EXECUTES, methodConfig.getExecutes().intValue());
Assert.assertEquals(DEPERECATED, methodConfig.getDeprecated());
Assert.assertEquals(STICKY, methodConfig.getSticky());
Assert.assertEquals(ONINVOKE, methodConfig.getOninvoke());
Assert.assertEquals(ONTHROW, methodConfig.getOnthrow());
Assert.assertEquals(ONRETURN, methodConfig.getOnreturn());
Assert.assertEquals(CACHE, methodConfig.getCache());
Assert.assertEquals(VALIDATION, methodConfig.getValidation());
Assert.assertEquals(ARGUMENTS_INDEX, methodConfig.getArguments().get(0).getIndex().intValue());
Assert.assertEquals(ARGUMENTS_CALLBACK, methodConfig.getArguments().get(0).isCallback());
Assert.assertEquals(ARGUMENTS_TYPE, methodConfig.getArguments().get(0).getType());
}

@Test
public void testName() throws Exception {
MethodConfig method = new MethodConfig();
Expand Down
Loading