69 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
package net.tomatentum.cutin.method;
 | 
						|
 | 
						|
import java.lang.reflect.InvocationTargetException;
 | 
						|
import java.lang.reflect.Method;
 | 
						|
import java.security.InvalidParameterException;
 | 
						|
import java.util.ArrayList;
 | 
						|
import java.util.Arrays;
 | 
						|
import java.util.List;
 | 
						|
 | 
						|
import org.slf4j.Logger;
 | 
						|
import org.slf4j.LoggerFactory;
 | 
						|
 | 
						|
import net.tomatentum.cutin.util.ReflectionUtil;
 | 
						|
 | 
						|
public abstract class ReflectedMethod<I extends Object, C extends Object> {
 | 
						|
 | 
						|
    private Logger logger = LoggerFactory.getLogger(getClass());
 | 
						|
 | 
						|
    protected Method method;
 | 
						|
    protected Object containingObject;
 | 
						|
 | 
						|
    protected ReflectedMethod(Method method, Object containingObject) {
 | 
						|
        if (!Arrays.asList(containingObject.getClass().getDeclaredMethods()).contains(method))
 | 
						|
            throw new InvalidParameterException("Method does not apply to specified handler");
 | 
						|
        this.method = method;
 | 
						|
        this.containingObject = containingObject;
 | 
						|
    }
 | 
						|
 | 
						|
    public abstract Object getParameter(C context, int index);
 | 
						|
 | 
						|
    public abstract I identifier();
 | 
						|
 | 
						|
    public Object run(C context) {
 | 
						|
        method.setAccessible(true);
 | 
						|
        try {
 | 
						|
            return method.invoke(containingObject, getParameters(context));
 | 
						|
        }catch (IllegalAccessException | InvocationTargetException ex) {
 | 
						|
            logger.error("ReflectedMethod failed to run", ex);
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public Method method() {
 | 
						|
        return this.method;
 | 
						|
    }
 | 
						|
 | 
						|
    public Object containingObject() {
 | 
						|
        return this.containingObject;
 | 
						|
    }
 | 
						|
 | 
						|
    @Override
 | 
						|
    public String toString() {
 | 
						|
        return identifier().toString();
 | 
						|
    }
 | 
						|
 | 
						|
    private Object[] getParameters(C context) {
 | 
						|
        int parameterCount = method.getParameterCount();
 | 
						|
        List<Object> parameters = new ArrayList<>();
 | 
						|
        
 | 
						|
        for (int i = 0; i < parameterCount; i++) {
 | 
						|
            Object parameter = getParameter(context, i);
 | 
						|
            logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method));
 | 
						|
            parameters.add(parameter);   
 | 
						|
        }
 | 
						|
        return parameters.toArray();
 | 
						|
    }
 | 
						|
    
 | 
						|
}
 |