Skip to content

Commit

Permalink
perf: 优化Dinger定义
Browse files Browse the repository at this point in the history
  • Loading branch information
AnswerAIL committed Dec 27, 2020
1 parent 55c61a5 commit 2839e4d
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.github.jaemon.dinger.constant.DingerConstant;
import com.github.jaemon.dinger.core.annatations.DingerScan;
import com.github.jaemon.dinger.core.annatations.Parameter;
import com.github.jaemon.dinger.core.entity.enums.DingerDefinitionType;
import com.github.jaemon.dinger.core.entity.enums.DingerType;
import com.github.jaemon.dinger.core.entity.enums.ExceptionEnum;
Expand All @@ -29,6 +30,8 @@
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;
import java.util.List;

Expand Down Expand Up @@ -180,11 +183,14 @@ protected List<Class<?>> dingerAnnotationResolver() throws Exception {
* dingerDefinitionKey
* @param dingerConfiguration
* Dinger层配置DingerConfig
* @param methodParams
* 方法参数信息
*/
void registerDingerDefinition(
String dingerName, Object source,
String dingerDefinitionKey,
DingerConfig dingerConfiguration
DingerConfig dingerConfiguration,
String[] methodParams
) {
boolean debugEnabled = log.isDebugEnabled();
for (DingerType dingerType : enabledDingerTypes) {
Expand Down Expand Up @@ -219,6 +225,7 @@ void registerDingerDefinition(
if (INSTANCE.contains(keyName)) {
throw new DingerException(DINGER_REPEATED_EXCEPTION, keyName);
}
dingerDefinition.setMethodParams(methodParams);

// DingerConfig Priority: `@DingerText | @DingerMarkdown | XML` > `@DingerConfiguration` > `***.yml | ***.properties`
dingerDefinition.dingerConfig()
Expand All @@ -232,6 +239,52 @@ void registerDingerDefinition(
}
}

/**
* 获取当前Dinger接口层方法的所有参数信息
*
* @param dingerClass
* Dinger接口层类
* @return
* 当前Dinger接口定义的方法的参数信息
*/
protected Map<String, String[]> dingerClassMethods(Class<?> dingerClass) {
Method[] declaredMethods = dingerClass.getDeclaredMethods();
Map<String, String[]> dingerMethodParams = new HashMap<>();
for (Method declaredMethod : declaredMethods) {
String methodName = declaredMethod.getName();
String[] methodParams = methodParams(declaredMethod);
dingerMethodParams.put(methodName, methodParams);
}
return dingerMethodParams;
}

/**
* 获取当前方法的所有参数信息
*
* @param method
* Dinger接口层方法
* @return
* 当前方法的参数信息
*/
protected String[] methodParams(Method method) {
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
java.lang.reflect.Parameter[] parameters = method.getParameters();
String[] params = new String[parameterAnnotations.length];

for (int i = 0; i < parameterAnnotations.length; i++) {
Annotation[] parameterAnnotation = parameterAnnotations[i];
params[i] = parameters[i].getName();
for (Annotation annotation : parameterAnnotation) {
if (Parameter.class.isInstance(annotation)) {
Parameter dingerParam = (Parameter) annotation;
params[i] = dingerParam.value();
break;
}
}
}
return params;
}

/**
* Container for DingerDefinition
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class DefaultDingerDefinition implements DingerDefinition {
private DingerType dingerType;
private MessageMainType messageMainType;
private MessageSubType messageSubType;
private String[] methodParams;

@Override
public String dingerName() {
Expand Down Expand Up @@ -106,4 +107,14 @@ public MessageSubType messageSubType() {
public void setMessageSubType(MessageSubType messageSubType) {
this.messageSubType = messageSubType;
}

@Override
public String[] methodParams() {
return methodParams == null ? new String[0] : methodParams;
}

@Override
public void setMethodParams(String[] methodParams) {
this.methodParams = methodParams;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/github/jaemon/dinger/core/DingerDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,20 @@ public interface DingerDefinition {
*/
void setMessageSubType(MessageSubType messageSubType);

/**
* methodParams
*
* @return
* array
*/
String[] methodParams();

/**
* setMethodParams
*
* @param methodParams
* methodParams
*/
void setMethodParams(String[] methodParams);

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void analysisDingerXml(Resource[] resources) throws Exception {
}
continue;
}
String xml = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));
String xml = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()), "UTF-8");
xml = transferXml(xml);
BeanTag dingerBean = XmlUtils.xmlToJavaBean(xml, BeanTag.class);
if (dingerBean == null) {
Expand All @@ -104,12 +104,19 @@ void analysisDingerXml(Resource[] resources) throws Exception {
if (dingerClass == null) {
throw new DingerException(DINER_XML_NAMESPACE_INVALID, namespace);
}
Map<String, String[]> dingerClassMethods = dingerClassMethods(dingerClass);

DingerConfig dingerConfiguration = dingerConfiguration(dingerClass);

List<MessageTag> messages = dingerBean.getMessages();
for (MessageTag message : messages) {
String dingerName = namespace + SPOT_SEPERATOR + message.getIdentityId();
String methodName = message.getIdentityId();
if (!dingerClassMethods.containsKey(methodName)) {
log.warn("namespace={}, messageId={} undefined in dingerClass.",
namespace, methodName);
continue;
}
String dingerName = namespace + SPOT_SEPERATOR + methodName;
String messageSubType = message.getDingerType();
if (!MessageSubType.contains(messageSubType)) {
throw new DingerException(DINER_XML_MSGTYPE_INVALID, dingerName, messageSubType);
Expand All @@ -119,7 +126,8 @@ void analysisDingerXml(Resource[] resources) throws Exception {
registerDingerDefinition(
dingerName, message,
dingerDefinitionKey,
dingerConfiguration
dingerConfiguration,
dingerClassMethods.get(methodName)
);
}
}
Expand All @@ -143,13 +151,15 @@ void analysisDingerAnnotation(List<Class<?>> dingerClasses) throws Exception {
registerDingerDefinition(
dingerName, method.getAnnotation(DingerText.class),
dingerDefinitionKey + MessageSubType.TEXT,
dingerConfiguration
dingerConfiguration,
methodParams(method)
);
} else if (method.isAnnotationPresent(DingerMarkdown.class)) {
registerDingerDefinition(
dingerName, method.getAnnotation(DingerMarkdown.class),
dingerDefinitionKey + MessageSubType.MARKDOWN,
dingerConfiguration
dingerConfiguration,
methodParams(method)
);
} else {
if (log.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
}

try {
// method params map
Map<String, Object> params = paramsHandler(method.getParameters(), args);

DingerType useDinger = dingerType(method);
DingerDefinition dingerDefinition = dingerDefinition(
useDinger, dingerClassName, keyName
Expand All @@ -77,6 +74,9 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return null;
}

// method params map
Map<String, Object> params = paramsHandler(method, dingerDefinition.methodParams(), args);

MsgType message = transfer(dingerDefinition, params);

DingerResponse dingerResponse = dingerRobot.send(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,21 @@ public class DingerMessageHandler
protected DingerProperties dingerProperties;

@Override
public Map<String, Object> paramsHandler(Parameter[] parameters, Object[] values) {
public Map<String, Object> paramsHandler(Method method, String[] keys, Object[] values) {
Map<String, Object> params = new HashMap<>();
if (parameters.length == 0) {
int valueLength = values.length;
if (valueLength == 0) {
return params;
}
int keyLength = keys.length;
if (keyLength == valueLength) {
for (int i = 0; i < valueLength; i++) {
params.put(keys[i], values[i]);
}
return params;
}

Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
String paramName = parameter.getName();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/github/jaemon/dinger/core/ParamHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.github.jaemon.dinger.core;

import java.lang.reflect.Parameter;
import java.lang.reflect.Method;
import java.util.Map;

/**
Expand All @@ -29,13 +29,15 @@ public interface ParamHandler {
/**
* Dinger方法参数处理
*
* @param method
* 执行方法
* @param parameters
* Dinger方法形参集
* @param values
* Dinger方法实参
* @return
* 形参和实参的映射关系
*/
Map<String, Object> paramsHandler(Parameter[] parameters, Object[] values);
Map<String, Object> paramsHandler(Method method, String[] parameters, Object[] values);

}

0 comments on commit 2839e4d

Please sign in to comment.