diff --git a/src/main/java/com/github/jaemon/dinger/core/AbstractDingerDefinitionResolver.java b/src/main/java/com/github/jaemon/dinger/core/AbstractDingerDefinitionResolver.java index a0a728c..35844fd 100644 --- a/src/main/java/com/github/jaemon/dinger/core/AbstractDingerDefinitionResolver.java +++ b/src/main/java/com/github/jaemon/dinger/core/AbstractDingerDefinitionResolver.java @@ -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; @@ -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; @@ -180,11 +183,14 @@ protected List> 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) { @@ -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() @@ -232,6 +239,52 @@ void registerDingerDefinition( } } + /** + * 获取当前Dinger接口层方法的所有参数信息 + * + * @param dingerClass + * Dinger接口层类 + * @return + * 当前Dinger接口定义的方法的参数信息 + */ + protected Map dingerClassMethods(Class dingerClass) { + Method[] declaredMethods = dingerClass.getDeclaredMethods(); + Map 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 */ diff --git a/src/main/java/com/github/jaemon/dinger/core/DefaultDingerDefinition.java b/src/main/java/com/github/jaemon/dinger/core/DefaultDingerDefinition.java index 65fa430..b89bcb8 100644 --- a/src/main/java/com/github/jaemon/dinger/core/DefaultDingerDefinition.java +++ b/src/main/java/com/github/jaemon/dinger/core/DefaultDingerDefinition.java @@ -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() { @@ -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; + } } \ No newline at end of file diff --git a/src/main/java/com/github/jaemon/dinger/core/DingerDefinition.java b/src/main/java/com/github/jaemon/dinger/core/DingerDefinition.java index 62237ec..5845559 100644 --- a/src/main/java/com/github/jaemon/dinger/core/DingerDefinition.java +++ b/src/main/java/com/github/jaemon/dinger/core/DingerDefinition.java @@ -130,4 +130,20 @@ public interface DingerDefinition { */ void setMessageSubType(MessageSubType messageSubType); + /** + * methodParams + * + * @return + * array + */ + String[] methodParams(); + + /** + * setMethodParams + * + * @param methodParams + * methodParams + */ + void setMethodParams(String[] methodParams); + } \ No newline at end of file diff --git a/src/main/java/com/github/jaemon/dinger/core/DingerDefinitionResolver.java b/src/main/java/com/github/jaemon/dinger/core/DingerDefinitionResolver.java index c824ea3..174cfbd 100644 --- a/src/main/java/com/github/jaemon/dinger/core/DingerDefinitionResolver.java +++ b/src/main/java/com/github/jaemon/dinger/core/DingerDefinitionResolver.java @@ -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) { @@ -104,12 +104,19 @@ void analysisDingerXml(Resource[] resources) throws Exception { if (dingerClass == null) { throw new DingerException(DINER_XML_NAMESPACE_INVALID, namespace); } + Map dingerClassMethods = dingerClassMethods(dingerClass); DingerConfig dingerConfiguration = dingerConfiguration(dingerClass); List 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); @@ -119,7 +126,8 @@ void analysisDingerXml(Resource[] resources) throws Exception { registerDingerDefinition( dingerName, message, dingerDefinitionKey, - dingerConfiguration + dingerConfiguration, + dingerClassMethods.get(methodName) ); } } @@ -143,13 +151,15 @@ void analysisDingerAnnotation(List> 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()) { diff --git a/src/main/java/com/github/jaemon/dinger/core/DingerHandleProxy.java b/src/main/java/com/github/jaemon/dinger/core/DingerHandleProxy.java index 12f140e..5ebb524 100644 --- a/src/main/java/com/github/jaemon/dinger/core/DingerHandleProxy.java +++ b/src/main/java/com/github/jaemon/dinger/core/DingerHandleProxy.java @@ -66,9 +66,6 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl } try { - // method params map - Map params = paramsHandler(method.getParameters(), args); - DingerType useDinger = dingerType(method); DingerDefinition dingerDefinition = dingerDefinition( useDinger, dingerClassName, keyName @@ -77,6 +74,9 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl return null; } + // method params map + Map params = paramsHandler(method, dingerDefinition.methodParams(), args); + MsgType message = transfer(dingerDefinition, params); DingerResponse dingerResponse = dingerRobot.send(message); diff --git a/src/main/java/com/github/jaemon/dinger/core/DingerMessageHandler.java b/src/main/java/com/github/jaemon/dinger/core/DingerMessageHandler.java index 625c5a4..eacf25e 100644 --- a/src/main/java/com/github/jaemon/dinger/core/DingerMessageHandler.java +++ b/src/main/java/com/github/jaemon/dinger/core/DingerMessageHandler.java @@ -54,12 +54,21 @@ public class DingerMessageHandler protected DingerProperties dingerProperties; @Override - public Map paramsHandler(Parameter[] parameters, Object[] values) { + public Map paramsHandler(Method method, String[] keys, Object[] values) { Map 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(); diff --git a/src/main/java/com/github/jaemon/dinger/core/ParamHandler.java b/src/main/java/com/github/jaemon/dinger/core/ParamHandler.java index 2858534..4a3a313 100644 --- a/src/main/java/com/github/jaemon/dinger/core/ParamHandler.java +++ b/src/main/java/com/github/jaemon/dinger/core/ParamHandler.java @@ -15,7 +15,7 @@ */ package com.github.jaemon.dinger.core; -import java.lang.reflect.Parameter; +import java.lang.reflect.Method; import java.util.Map; /** @@ -29,6 +29,8 @@ public interface ParamHandler { /** * Dinger方法参数处理 * + * @param method + * 执行方法 * @param parameters * Dinger方法形参集 * @param values @@ -36,6 +38,6 @@ public interface ParamHandler { * @return * 形参和实参的映射关系 */ - Map paramsHandler(Parameter[] parameters, Object[] values); + Map paramsHandler(Method method, String[] parameters, Object[] values); } \ No newline at end of file