aknot/src/main/org/atriasoft/aknot/pojo/IntrospectionPropertyMethodGetter.java
Edouard DUPIN cff3646783 [FIX] arbo
2025-05-24 00:24:25 +02:00

76 lines
2.6 KiB
Java

package org.atriasoft.aknot.pojo;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.atriasoft.aknot.exception.AknotException;
import org.atriasoft.aknot.model.IntrospectionPropertyGetter;
public class IntrospectionPropertyMethodGetter implements IntrospectionPropertyGetter {
// private static Class<?>[] getTypefunction(final Method setter, final Method getter) throws Exception {
// Class<?> type = null;
// Class<?> subType = null;
// if (setter == null && getter == null) {
// // impossible case...
// throw new Exception("kjhkhjkj");
// }
// if (getter != null) {
// type = getter.getReturnType();
// if (Enum.class.isAssignableFrom(type)) {
// LOGGER.trace("Find an enum ...");
// } else {
// Type empppe = getter.getGenericReturnType();
// if (empppe instanceof ParameterizedType plopppppp) {
// Type[] realType = plopppppp.getActualTypeArguments();
// if (realType.length > 0) {
// subType = Class.forName(realType[0].getTypeName());
// }
// }
// }
// }
// if (setter != null) {
// if (type != null && setter.getParameters()[0].getType() != type) {
// throw new Exception("The type of the setter ands the type return by the getter are not the same ...");
// }
// type = setter.getParameters()[0].getType();
// if (List.class.isAssignableFrom(type)) {
// Class<?> internalModelClass = null;
// Type[] empppe = setter.getGenericParameterTypes();
// if (empppe.length > 0) {
// if (empppe[0] instanceof ParameterizedType plopppppp) {
// Type[] realType = plopppppp.getActualTypeArguments();
// if (realType.length > 0) {
// LOGGER.warn(" -->> " + realType[0]);
// internalModelClass = Class.forName(realType[0].getTypeName());
// }
// }
// }
// if (getter!=null && internalModelClass != subType) {
// throw new Exception("The type of the setter and the type return by the getter are not the same ...");
// }
// subType = internalModelClass;
// }
// }
// return new Class<?>[] {type, subType};
// }
//
protected Method getter;
public IntrospectionPropertyMethodGetter(final Method getter) throws Exception {
this.getter = getter;
}
@Override
public Object getValue(final Object object) throws AknotException {
if (this.getter == null) {
throw new AknotException("no getter availlable");
}
try {
return this.getter.invoke(object);
} catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
e.printStackTrace();
throw new AknotException("Can not set value ... " + e.getMessage());
}
}
}