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

[ISSUE-2045] Add annotation support of MethodConfig #3639

Merged
merged 9 commits into from
Mar 14, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.config;

import org.apache.dubbo.config.annotation.Argument;
import org.apache.dubbo.config.support.Parameter;

import java.io.Serializable;
Expand Down Expand Up @@ -44,6 +45,15 @@ public class ArgumentConfig implements Serializable {
*/
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 @@ -70,4 +80,4 @@ public Boolean isCallback() {
return callback;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.support.Parameter;

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

/**
Expand Down Expand Up @@ -117,6 +120,46 @@ public String getName() {
return name;
}

public MethodConfig() {
}

public MethodConfig(Method method) {
appendAnnotation(Method.class, method);

this.setReturn(method.isReturn());

if(!"".equals(method.oninvoke())){
this.setOninvoke(method.oninvoke());
}
if(!"".equals(method.onreturn())){
this.setOnreturn(method.onreturn());
}
if(!"".equals(method.onthrow())){
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();
}

public void setName(String name) {
checkMethodName("name", name);
this.name = name;
Expand Down Expand Up @@ -279,4 +322,4 @@ public String getPrefix() {
+ (StringUtils.isEmpty(serviceId) ? "" : ("." + serviceId))
+ "." + getName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public ReferenceConfig() {

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

public URL toUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,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 org.apache.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,69 @@
/*
* 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 org.apache.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 @@ -256,4 +256,10 @@
* Protocol spring bean names
*/
String protocol() default "";

/**
* methods support
* @return
*/
Method[] methods() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,10 @@
* Service tag name
*/
String tag() default "";

/**
* methods support
* @return
*/
Method[] methods() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ public void tetMetaData() {
Assertions.assertNull(metaData.get("key2"));
}

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

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface Config {
Expand All @@ -479,6 +485,10 @@ public void tetMetaData() {
String[] listener() default {};

String[] parameters() default {};

ConfigField[] configFields() default {};

ConfigField configField() default @ConfigField;
}

private static class OverrideConfig extends AbstractInterfaceConfig {
Expand Down Expand Up @@ -735,6 +745,7 @@ private static class AnnotationConfig extends AbstractConfig {
private String filter;
private String listener;
private Map<String, String> parameters;
private String[] configFields;

public Class getInterface() {
return interfaceClass;
Expand Down Expand Up @@ -767,5 +778,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 @@ -18,23 +18,75 @@
package org.apache.dubbo.config;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.config.annotation.Argument;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Reference;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

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

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.MatcherAssert.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);

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

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