📄 defaultlogicmethod.java
字号:
package xyz.frame.component;import java.lang.annotation.Annotation;import java.lang.reflect.Method;import org.apache.log4j.Logger;import xyz.frame.LogicException;import xyz.frame.LogicRequest;import xyz.frame.annotations.Read;import xyz.frame.annotations.Viewless;import xyz.frame.util.JavaMethod;import xyz.frame.util.MethodInvocationException;import xyz.frame.util.ReflectionUtil;import xyz.frame.validator.BasicValidationErrors;import xyz.frame.validator.UnstableValidationException;import xyz.frame.validator.ValidationErrors;/** * Represents a logic method. * * @author Guilherme Silveira */public class DefaultLogicMethod implements LogicMethod { private static final Logger logger = Logger .getLogger(DefaultLogicMethod.class); private String name; private JavaMethod method; private boolean shouldRedirect; private JavaMethod validateMethod; /** * Constructs it based on the logics name and method * * @param method * method */ public DefaultLogicMethod(String name, Method method, Method validateMethod) { this.name = name; this.method = new JavaMethod(method); this.shouldRedirect = !this.method.containsAnnotation(Viewless.class); // reads all method parameters Annotation[][] annotations = method.getParameterAnnotations(); Class<?>[] parameters = method.getParameterTypes(); for (int i = 0; i < parameters.length; i++) { Class<?> parameter = parameters[i]; // for each parameter, creates a ParameterRead Read annotation = ReflectionUtil.findAnnotation(annotations[i], Read.class); // ParameterReadParameter read = new ParameterReadParameter(); } if (validateMethod != null) { this.validateMethod = new JavaMethod(validateMethod); } logger.debug("Logic method " + method.getName() + " was read with validation " + this.validateMethod); } public String getName() { return this.name; } public String execute(Object component, LogicRequest context) throws LogicException { Object result; try { result = method.invoke(component); } catch (MethodInvocationException e) { logger.error("Unable to execute the action " + this.name, e .getCause()); throw new LogicException(e.getCause().getMessage(), e.getCause()); } return result == null ? "ok" : result.toString(); } public ValidationErrors validate(Object component, LogicRequest context) throws UnstableValidationException { ValidationErrors errors = new BasicValidationErrors(); if (validateMethod != null) { try { validateMethod.invoke(component, errors); } catch (MethodInvocationException e) { logger.error( "Nasty validation method has thrown an exception!", e); throw new UnstableValidationException( "Error during validation process", e); } } return errors; } public boolean shouldRedirect() { return this.shouldRedirect; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -