Search in sources :

Example 1 with MethodInstance

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

the class Reflector method getMethodInstance.

/**
 * gets the MethodInstance matching given Parameter
 * @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 NoSuchMethodException
 * @throws PageException
 */
public static MethodInstance getMethodInstance(Object obj, Class clazz, String methodName, Object[] args) throws NoSuchMethodException {
    MethodInstance mi = getMethodInstanceEL(obj, clazz, KeyImpl.getInstance(methodName), args);
    if (mi != null)
        return mi;
    Class[] classes = getClasses(args);
    // StringBuilder sb=null;
    JavaObject jo;
    Class c;
    ConstructorInstance ci;
    for (int i = 0; i < classes.length; i++) {
        if (args[i] instanceof JavaObject) {
            jo = (JavaObject) args[i];
            c = jo.getClazz();
            ci = Reflector.getConstructorInstance(c, new Object[0], null);
            if (ci == null) {
                throw new NoSuchMethodException("The " + pos(i + 1) + " parameter of " + methodName + "(" + getDspMethods(classes) + ") ia a object created " + "by the createObject function (JavaObject/JavaProxy). This object has not been instantiated because it does not have a constructor " + "that takes zero arguments. " + Constants.NAME + " cannot instantiate it for you, please use the .init(...) method to instantiate it with the correct parameters first");
            }
        }
    }
    /*
        the argument list contains objects created by createObject, 
        that are no instantiated (first,third,10th) and because this object have no constructor taking no arguments, Lucee cannot instantiate them.
        you need first to instantiate this objects. 
        */
    throw new NoSuchMethodException("No matching Method for " + methodName + "(" + getDspMethods(classes) + ") found for " + Caster.toTypeName(clazz));
}
Also used : JavaObject(lucee.runtime.java.JavaObject) MethodInstance(lucee.runtime.reflection.pairs.MethodInstance) JavaObject(lucee.runtime.java.JavaObject) ConstructorInstance(lucee.runtime.reflection.pairs.ConstructorInstance)

Example 2 with MethodInstance

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

the class Reflector method getGetterEL.

/**
 * to get a Getter Method of a Object
 * @param clazz Class to invoke method from
 * @param prop Name of the Method without get
 * @return return Value of the getter Method
 */
public static MethodInstance getGetterEL(Class clazz, String prop) {
    prop = "get" + StringUtil.ucFirst(prop);
    MethodInstance mi = getMethodInstanceEL(null, clazz, KeyImpl.getInstance(prop), ArrayUtil.OBJECT_EMPTY);
    if (mi == null)
        return null;
    if (mi.getMethod().getReturnType() == void.class)
        return null;
    return mi;
}
Also used : MethodInstance(lucee.runtime.reflection.pairs.MethodInstance)

Example 3 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
     * @deprecated use instead <code>getSetter(Object obj, String prop,Object value, MethodInstance defaultValue)</code>
     
    public static MethodInstance getSetterEL(Object obj, String prop,Object value)  {
        prop="set"+StringUtil.ucFirst(prop);
        MethodInstance mi = getMethodInstanceEL(obj.getClass(),KeyImpl.getInstance(prop),new Object[]{value});
        if(mi==null) return null;
        Method m=mi.getMethod();
        
        if(m.getReturnType()!=void.class) return null;
        return mi;
    }*/
/**
 * 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
 */
public static MethodInstance getSetter(Object obj, String prop, Object value, MethodInstance defaultValue) {
    prop = "set" + StringUtil.ucFirst(prop);
    MethodInstance mi = getMethodInstanceEL(obj, obj.getClass(), KeyImpl.getInstance(prop), new Object[] { value });
    if (mi == null)
        return defaultValue;
    Method m = mi.getMethod();
    if (m.getReturnType() != void.class)
        return defaultValue;
    return mi;
}
Also used : MethodInstance(lucee.runtime.reflection.pairs.MethodInstance) Method(java.lang.reflect.Method)

Example 4 with MethodInstance

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

the class Reflector method getGetter.

/**
 * to get a Getter Method of a Object
 * @param clazz Class to invoke method from
 * @param prop Name of the Method without get
 * @return return Value of the getter Method
 * @throws NoSuchMethodException
 * @throws PageException
 */
public static MethodInstance getGetter(Class clazz, String prop) throws PageException, NoSuchMethodException {
    String getterName = "get" + StringUtil.ucFirst(prop);
    MethodInstance mi = getMethodInstanceEL(null, clazz, KeyImpl.getInstance(getterName), ArrayUtil.OBJECT_EMPTY);
    if (mi == null) {
        String isName = "is" + StringUtil.ucFirst(prop);
        mi = getMethodInstanceEL(null, clazz, KeyImpl.getInstance(isName), ArrayUtil.OBJECT_EMPTY);
        if (mi != null) {
            Method m = mi.getMethod();
            Class rtn = m.getReturnType();
            if (rtn != Boolean.class && rtn != boolean.class)
                mi = null;
        }
    }
    if (mi == null)
        throw new ExpressionException("No matching property [" + prop + "] found in [" + Caster.toTypeName(clazz) + "]");
    Method m = mi.getMethod();
    if (m.getReturnType() == void.class)
        throw new NoSuchMethodException("invalid return Type, method [" + m.getName() + "] for Property [" + getterName + "] must have return type not void");
    return mi;
}
Also used : MethodInstance(lucee.runtime.reflection.pairs.MethodInstance) Method(java.lang.reflect.Method) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 5 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, Object defaultValue) {
    if (isInit) {
        return variableUtil.get(pc, object, propertyName, defaultValue);
    }
    // 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) {
        }
    }
    // Getter
    MethodInstance mi = Reflector.getGetterEL(clazz, propertyName);
    if (mi != null) {
        if (Modifier.isStatic(mi.getMethod().getModifiers())) {
            try {
                return mi.invoke(null);
            } catch (Exception e) {
            }
        }
    }
    try {
        return variableUtil.get(pc, init(), propertyName, defaultValue);
    } catch (PageException e1) {
        return defaultValue;
    }
}
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)

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