Search in sources :

Example 6 with MethodInstance

use of lucee.runtime.reflection.pairs.MethodInstance in project Lucee by lucee.

the class Reflector method getSetter.

/**
 * to invoke a setter Method of a Object
 * @param obj Object to invoke method from
 * @param prop Name of the Method without get
 * @param value Value to set to the Method
 * @return MethodInstance
 * @throws NoSuchMethodException
 * @throws PageException
 */
public static MethodInstance getSetter(Object obj, String prop, Object value) throws NoSuchMethodException {
    prop = "set" + StringUtil.ucFirst(prop);
    MethodInstance mi = getMethodInstance(obj, obj.getClass(), prop, new Object[] { value });
    Method m = mi.getMethod();
    if (m.getReturnType() != void.class)
        throw new NoSuchMethodException("invalid return Type, method [" + m.getName() + "] must have return type void, now [" + m.getReturnType().getName() + "]");
    return mi;
}
Also used : MethodInstance(lucee.runtime.reflection.pairs.MethodInstance) Method(java.lang.reflect.Method)

Example 7 with MethodInstance

use of lucee.runtime.reflection.pairs.MethodInstance in project Lucee by lucee.

the class Reflector method getMethodInstanceEL.

/**
 * gets the MethodInstance matching given Parameter
 * @param objMaybeNull maybe null
 * @param clazz Class Of the Method to get
 * @param methodName Name of the Method to get
 * @param args Arguments of the Method to get
 * @return return Matching Method
 * @throws
 */
public static MethodInstance getMethodInstanceEL(Object objMaybeNull, Class clazz, Collection.Key methodName, Object[] args) {
    checkAccessibility(objMaybeNull, clazz, methodName);
    args = cleanArgs(args);
    // getDeclaredMethods(clazz);
    Method[] methods = mStorage.getMethods(clazz, methodName, args.length);
    if (methods != null) {
        Class[] clazzArgs = getClasses(args);
        // print.e("exact:"+methodName);
        outer: for (int i = 0; i < methods.length; i++) {
            if (methods[i] != null) {
                Class[] parameterTypes = methods[i].getParameterTypes();
                for (int y = 0; y < parameterTypes.length; y++) {
                    if (toReferenceClass(parameterTypes[y]) != clazzArgs[y])
                        continue outer;
                }
                return new MethodInstance(methods[i], args);
            }
        }
        // print.e("like:"+methodName);
        outer: for (int i = 0; i < methods.length; i++) {
            if (methods[i] != null) {
                Class[] parameterTypes = methods[i].getParameterTypes();
                for (int y = 0; y < parameterTypes.length; y++) {
                    if (!like(clazzArgs[y], toReferenceClass(parameterTypes[y])))
                        continue outer;
                }
                return new MethodInstance(methods[i], args);
            }
        }
        // convert comparsion
        // print.e("convert:"+methodName);
        MethodInstance mi = null;
        int _rating = 0;
        outer: for (int i = 0; i < methods.length; i++) {
            if (methods[i] != null) {
                RefInteger rating = (methods.length > 1) ? new RefIntegerImpl(0) : null;
                Class[] parameterTypes = methods[i].getParameterTypes();
                Object[] newArgs = new Object[args.length];
                for (int y = 0; y < parameterTypes.length; y++) {
                    try {
                        newArgs[y] = convert(args[y], toReferenceClass(parameterTypes[y]), rating);
                    } catch (PageException e) {
                        continue outer;
                    }
                }
                if (mi == null || rating.toInt() > _rating) {
                    if (rating != null)
                        _rating = rating.toInt();
                    mi = new MethodInstance(methods[i], newArgs);
                }
            // return new MethodInstance(methods[i],newArgs);
            }
        }
        return mi;
    }
    return null;
}
Also used : PageException(lucee.runtime.exp.PageException) MethodInstance(lucee.runtime.reflection.pairs.MethodInstance) RefInteger(lucee.commons.lang.types.RefInteger) Method(java.lang.reflect.Method) RefIntegerImpl(lucee.commons.lang.types.RefIntegerImpl)

Example 8 with MethodInstance

use of lucee.runtime.reflection.pairs.MethodInstance in project Lucee by lucee.

the class JavaObject method setEL.

@Override
public Object setEL(PageContext pc, Collection.Key propertyName, Object value) {
    if (isInit) {
        return variableUtil.setEL(pc, object, propertyName, value);
    }
    // Field
    Field[] fields = Reflector.getFieldsIgnoreCase(clazz, propertyName.getString(), null);
    if (!ArrayUtil.isEmpty(fields) && Modifier.isStatic(fields[0].getModifiers())) {
        try {
            fields[0].set(null, value);
        } catch (Exception e) {
        }
        return value;
    }
    // Getter
    MethodInstance mi = Reflector.getSetter(clazz, propertyName.getString(), value, null);
    if (mi != null) {
        if (Modifier.isStatic(mi.getMethod().getModifiers())) {
            try {
                return mi.invoke(null);
            } catch (Exception e) {
            }
        }
    }
    try {
        return variableUtil.setEL(pc, init(), propertyName, value);
    } catch (PageException e1) {
        return value;
    }
}
Also used : Field(java.lang.reflect.Field) PageException(lucee.runtime.exp.PageException) MethodInstance(lucee.runtime.reflection.pairs.MethodInstance) PageException(lucee.runtime.exp.PageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 9 with MethodInstance

use of lucee.runtime.reflection.pairs.MethodInstance in project Lucee by lucee.

the class JavaObject method set.

@Override
public Object set(PageContext pc, Collection.Key propertyName, Object value) throws PageException {
    if (isInit) {
        return ((VariableUtilImpl) variableUtil).set(pc, object, propertyName, value);
    }
    // Field
    Field[] fields = Reflector.getFieldsIgnoreCase(clazz, propertyName.getString(), null);
    if (!ArrayUtil.isEmpty(fields) && Modifier.isStatic(fields[0].getModifiers())) {
        try {
            fields[0].set(null, value);
            return value;
        } catch (Exception e) {
            Caster.toPageException(e);
        }
    }
    // Getter
    MethodInstance mi = Reflector.getSetter(clazz, propertyName.getString(), value, null);
    if (mi != null) {
        if (Modifier.isStatic(mi.getMethod().getModifiers())) {
            try {
                return mi.invoke(null);
            } catch (IllegalAccessException e) {
                throw Caster.toPageException(e);
            } catch (InvocationTargetException e) {
                throw Caster.toPageException(e.getTargetException());
            }
        }
    }
    return ((VariableUtilImpl) variableUtil).set(pc, init(), propertyName, value);
}
Also used : Field(java.lang.reflect.Field) MethodInstance(lucee.runtime.reflection.pairs.MethodInstance) VariableUtilImpl(lucee.runtime.util.VariableUtilImpl) PageException(lucee.runtime.exp.PageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExpressionException(lucee.runtime.exp.ExpressionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with MethodInstance

use of lucee.runtime.reflection.pairs.MethodInstance in project Lucee by lucee.

the class JavaObject method get.

public Object get(PageContext pc, String propertyName) throws PageException {
    if (isInit) {
        return variableUtil.get(pc, object, propertyName);
    }
    // Check Field
    Field[] fields = Reflector.getFieldsIgnoreCase(clazz, propertyName, null);
    if (!ArrayUtil.isEmpty(fields) && Modifier.isStatic(fields[0].getModifiers())) {
        try {
            return fields[0].get(null);
        } catch (Exception e) {
            throw Caster.toPageException(e);
        }
    }
    // Getter
    MethodInstance mi = Reflector.getGetterEL(clazz, propertyName);
    if (mi != null) {
        if (Modifier.isStatic(mi.getMethod().getModifiers())) {
            try {
                return mi.invoke(null);
            } catch (IllegalAccessException e) {
                throw Caster.toPageException(e);
            } catch (InvocationTargetException e) {
                throw Caster.toPageException(e.getTargetException());
            }
        }
    }
    // male Instance
    return variableUtil.get(pc, init(), propertyName);
}
Also used : Field(java.lang.reflect.Field) MethodInstance(lucee.runtime.reflection.pairs.MethodInstance) PageException(lucee.runtime.exp.PageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExpressionException(lucee.runtime.exp.ExpressionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

MethodInstance (lucee.runtime.reflection.pairs.MethodInstance)10 ExpressionException (lucee.runtime.exp.ExpressionException)5 PageException (lucee.runtime.exp.PageException)5 Field (java.lang.reflect.Field)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Method (java.lang.reflect.Method)4 RefInteger (lucee.commons.lang.types.RefInteger)1 RefIntegerImpl (lucee.commons.lang.types.RefIntegerImpl)1 JavaObject (lucee.runtime.java.JavaObject)1 ConstructorInstance (lucee.runtime.reflection.pairs.ConstructorInstance)1 VariableUtilImpl (lucee.runtime.util.VariableUtilImpl)1