private void setText(String text) { Field field = getFields(this, ISetValue.class).get(0); ISetValue setValueElement = (ISetValue) getValueField(field, this); doActionRule.accept(text, val -> setValueAction(val, setValueElement)); }
private static String printObject(Object obj) { List<String> result = new ArrayList<>(); for (Field field : getFields(obj, Object.class)) { Object value = getValueField(field, obj); String strValue = null; if (value == null) strValue = "#NULL#"; else if (isClass(value.getClass(), String.class)) strValue = (String) value; else if (isClass(value.getClass(), Enum.class)) strValue = value.toString(); else if (field.isAnnotationPresent(Complex.class)) strValue = "#(#" + printObject(value) + "#)#"; if (strValue != null) result.add(String.format("%s#:#%s", field.getClass().getSimpleName(), strValue)); } return print(result, "#;#", "%s"); }
private static boolean isExpectedClass(Field field, Class<?>... types) { if (types == null || types.length == 0) return true; for (Class<?> type : types) if (isClass(field, type) || isInterface(field, type)) return true; return false; }
public static List<Field> getFields(Object obj, Class<?>[] types, Class<?>... stopTypes) { return getFields(getFieldsDeep(obj.getClass(), stopTypes), types, f -> !isStatic(f.getModifiers())); } public static List<Field> getFields(List<Field> fields, Class<?>[] types, Function<Field, Boolean> filter) {
public static boolean isClass(Field field, Class<?> expected) { return isClass(field.getType(), expected); }
public <T> T asEntity(Class<T> entityClass) { try { T data = newEntity(entityClass); foreach(getFields(this, IHasValue.class), item -> { Field field = LinqUtils.first(getFields(data, String.class), f -> namesEqual(f.getName(), item.getName())); if (field == null) return; try { field.set(data, ((IHasValue) getValueField(item, this)).getValue()); } catch (Exception ignore) { } }); return data; } catch (Exception ex) { throw exception("Can't get entity from Form" + getName() + " for class: " + entityClass.getClass()); } }
public static boolean fieldHasAnnotation(Field field, Class annotationClass, Class interfaceClass) { return isInterface(field, interfaceClass) && field.isAnnotationPresent(annotationClass); } }
public static List<Field> getFields(Object obj, Class<?>... types) { return getFields(obj, types, Object.class); } public static List<Field> getFields(Object obj, Class<?>[] types, Class<?>... stopTypes) {
private IBaseElement getInstancePage(Object parent, Field field, Class<?> type, Class<?> parentType) throws IllegalAccessException, InstantiationException { IBaseElement instance = (IBaseElement) getValueField(field, parent); if (instance == null) { instance = (IBaseElement) type.newInstance(); } fillPageFromAnnotation(field, instance); return instance; }
public static <T> T getFirstField(Object obj, Class<?>... types) { return (T) getValueField(LinqUtils.first(obj.getClass().getDeclaredFields(), field -> isExpectedClass(field, types)), obj); } private static boolean isExpectedClass(Field field, Class<?>... types) {
public Form(Class<T> clazz) { this.entityClass = checkEntityIsNotNull(clazz); }
private E rowToEntity(MapArray<String, ICell> row) { E entity = newEntity(entityClass); if (row == null) return entity; Field[] fields = entity.getClass().getFields(); row.pairs.forEach(entry -> setEntityField(entity, fields, entry.key, entry.value.getText())); return entity; }
private void setEntityField(E entity, Field[] fields, String fieldName, String value) { setField(entity, fields, fieldName, field -> convertStringToType(value, field)); }
public static boolean isClass(Field field, Class<?> expected) { return isClass(field.getType(), expected); }
public <T> T asEntity(Class<T> entityClass) { try { T data = newEntity(entityClass); foreach(getFields(this, IHasValue.class), item -> { Field field = LinqUtils.first(getFields(data, String.class), f -> namesEqual(f.getName(), item.getName())); if (field == null) return; try { field.set(data, ((IHasValue) getValueField(item, this)).getValue()); } catch (Exception ignore) { } }); return data; } catch (Exception ex) { throw exception("Can't get entity from Form" + getName() + " for class: " + entityClass.getClass()); } }
public static boolean isInterface(Field field, Class<?> expected) { return isInterface(field.getType(), expected); }
private IBaseElement getInstancePage(Object parent, Field field, Class<?> type, Class<?> parentType) throws IllegalAccessException, InstantiationException { IBaseElement instance = (IBaseElement) getValueField(field, parent); if (instance == null) { instance = (IBaseElement) type.newInstance(); } fillPageFromAnnotation(field, instance); return instance; }
public static List<Field> getFields(Object obj, Class<?>[] types, Class<?>... stopTypes) { return getFields(getFieldsDeep(obj.getClass(), stopTypes), types, f -> !isStatic(f.getModifiers())); } public static List<Field> getFields(List<Field> fields, Class<?>[] types, Function<Field, Boolean> filter) {
public static <T> T getFirstField(Object obj, Class<?>... types) { return (T) getValueField(LinqUtils.first(obj.getClass().getDeclaredFields(), field -> isExpectedClass(field, types)), obj); } private static boolean isExpectedClass(Field field, Class<?>... types) {