use Java 7 APIs to get parameter annotations

pull/94/head
Caglar Gonul 8 years ago committed by Mahmoud Ben Hassine
parent e8aa3626a9
commit 1d7cd95b4f

@ -29,7 +29,6 @@ import org.jeasy.rules.api.Facts;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;
@ -109,32 +108,42 @@ class RuleDefinitionValidator {
}
private boolean isConditionMethodWellDefined(final Method method) {
Parameter[] parameters = method.getParameters();
return Modifier.isPublic(method.getModifiers())
&& method.getReturnType().equals(Boolean.TYPE)
&& validParameters(parameters);
&& validParameters(method);
}
private boolean validParameters(final Method method) {
int notAnnotatedParameterCount = 0;
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for(Annotation[] anns : parameterAnnotations){
if(anns.length == 0){
notAnnotatedParameterCount += 1;
} else {
//Annotation types has to be Fact
for(Annotation ann : anns){
if(!ann.annotationType().equals(Fact.class)){
return false;
}
}
private boolean validParameters(Parameter[] parameters) {
List<Parameter> notAnnotatedParams = new ArrayList<>();
for (Parameter parameter : parameters) {
if (parameter.getAnnotation(Fact.class) == null) {
notAnnotatedParams.add(parameter);
}
}
if (notAnnotatedParams.size() > 1) {
if(notAnnotatedParameterCount > 1){
return false;
} else if (notAnnotatedParams.size() == 1) {
Parameter parameter = notAnnotatedParams.get(0);
return parameter.getType().isAssignableFrom(Facts.class);
}
Class<?>[] parameterTypes = method.getParameterTypes();
if(parameterTypes.length > 1){
return false;
}
if(parameterTypes.length == 1 && notAnnotatedParameterCount == 1){
return parameterTypes[0].equals(Facts.class);
}
return true;
}
private boolean isActionMethodWellDefined(final Method method) {
Parameter[] parameters = method.getParameters();
return Modifier.isPublic(method.getModifiers())
&& validParameters(parameters);
&& validParameters(method);
}
private boolean isPriorityMethodWellDefined(final Method method) {

@ -30,9 +30,9 @@ import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Priority;
import org.jeasy.rules.api.Rule;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Proxy;
import java.util.*;
@ -114,21 +114,21 @@ public class RuleProxy implements InvocationHandler {
}
private List<Object> getActualParameters(Method method, Facts facts) {
Parameter[] parameters = method.getParameters(); // validated upfront
List<Object> actualParameters = new ArrayList<>();
for (Parameter parameter : parameters) {
Fact annotation = parameter.getAnnotation(Fact.class); // validated upfront
if (annotation == null) { // validated upfront, there may be only one parameter not annotated and which is of type Facts.class
actualParameters.add(facts);
} else {
String factName = annotation.value();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for(Annotation[] ann : parameterAnnotations){
if(ann.length == 1){
String factName = ((Fact) (ann[0])).value(); //validated upfront.
Object fact = facts.get(factName);
if(fact == null){
throw new RuntimeException(String.format("No fact named %s found in known facts", factName));
}
actualParameters.add(fact);
} else {
actualParameters.add(facts); //validated upfront, there may be only one parameter not annotated and which is of type Facts.class
}
}
return actualParameters;
}

@ -26,7 +26,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<java.version>1.7</java.version>
<junit.version>4.12</junit.version>
<assertj.version>2.6.0</assertj.version>
<mockito.version>2.7.1</mockito.version>

Loading…
Cancel
Save