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;
}
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;
}
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;
}
}
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);
}
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);
}
Aggregations