⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 runtimemethod.java

📁 EclipseIM是一个Eclipse插件, 其可以与Java开发人员即时聊天, 当在聊天中, 可以发送Eclipse中的java源码.
💻 JAVA
字号:
/**
 *
 */
package com.asterix.validation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.MessageFormat;

import com.asterix.validation.annotations.Rule;

/**
 * @author Andrei A Adamian
 *
 */
public class RuntimeMethod {
    private Method method;
    private Class<IValidationRule> clazz;
    private IValidationRule target;

    public RuntimeMethod(Method method, Class<IValidationRule> clazz) {
	this.method = method;
	this.clazz = clazz;
    }

    public RuntimeMethod(Method method, IValidationRule target) {
	this.method = method;
	this.target = target;
    }

    public Method getMethod() {
	return method;
    }

    public void setMethod(Method method) {
	this.method = method;
    }

    public IValidationRule getTarget() {
	return target;
    }

    public void setTarget(IValidationRule target) {
	this.target = target;
    }

    public String getMessage() {
	Rule rule = method.getAnnotation(Rule.class);
	if (rule != null) {
	    return rule.message();
	}
	throw new IllegalStateException("Rule annotation not specified");
    }


    public Class<IValidationRule> getClazz() {
        return clazz;
    }

    public void setClazz(Class<IValidationRule> clazz) {
        this.clazz = clazz;
    }

    public String[] getFixesId() {
	Rule rule = method.getAnnotation(Rule.class);
	if (rule != null) {
	    return rule.fixes();
	}
	throw new IllegalStateException("Rule annotation not specified");
    }

    public IValidationError invoke(Object param) {
	    IValidationError error = null;
	    try {
		error = (IValidationError) method.invoke(target, param);
	    if (error == null) {
		return null;
	    }
	    String m = getMessage();
	    if (!"".equals(m)) {
		target.getContext().addMessageParameter(0, error.getMessage());
		error.setMessage(MessageFormat.format(m, target.getContext().getMessageParameters().toArray()));
	    }
	    if (getFixesId().length > 0) {
	    }
	    } catch (IllegalArgumentException e) {
		throw new IllegalArgumentException("Illegal argument type in rule "+getMethod().getName()+". Argument: "+param,e);
	    } catch (IllegalAccessException e) {
		e.printStackTrace();
	    } catch (InvocationTargetException e) {
		e.printStackTrace();
	    }
	    return error;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -