/** * Get class or superclass named field with checked exception. Tries to get requested field from given class; if not * found try with superclass, if any. If both attempts fail throw {@link NoSuchFieldException}. * * @param clazz class to search for named field, * @param fieldName the name of field to retrieve. * @return requested field. * @throws NoSuchFieldException if class or superclass has no field with requested name. */ public static Field getFieldEx(Class<?> clazz, String fieldName) throws NoSuchFieldException { try { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); return field; } catch(NoSuchFieldException e) { Class<?> superclass = clazz.getSuperclass(); if(superclass != null && clazz.getPackage().equals(superclass.getPackage())) { return getFieldEx(superclass, fieldName); } throw e; } catch(SecurityException e) { throw new BugError(e); } }
Field field = Classes.getFieldEx(clazz, (String) fieldName); if (value == null && field.getType().isPrimitive()) { log.warn("Attempt to assing null value to primitive field |%s#%s|. Ignore it.", instance.getClass(), field.getName());
/** * Get child node by name creating it on the fly if not set. This method attempt to retrieve child node for wrapped * object field with requested name; it returns null if there is no object field with requested child name. If field * value is null create child node and set it. * * @param childName child name. * @return child node, newly created or retrieved from wrapped object field. */ @Override public Node getChild(String childName) throws IllegalAccessException { Field field = null; try { field = Classes.getFieldEx(object.getClass(), Strings.toMemberName(childName)); } catch (NoSuchFieldException e) { // best effort approach: form to object mapping is not so strictly but still log missing fields log.debug("Missing field |%s| from object |%s|.", childName, object.getClass()); return null; } Object child = field.get(object); // not test coverable condition: if child is null child instanceof Collection cannot be true if (child == null || (child instanceof Collection<?> && ((Collection<?>) child).size() == 0)) { child = FormObject.this.factory.newInstance(field.getType()); field.set(object, child); } return FormObject.this.factory.createNode(child, type(field)); }
/** * Set value to wrapped object field identified by requested child name. Given string value is converted to wrapped * object type using {@link Converter}. * * @param childName wrapped object field name, * @param value string value loaded from form field, null and empty accepted. * @throws ConverterException if field value conversion fails. * @throws IllegalAccessException this exception is required by reflective field signature but never thrown. */ @Override public void setValue(String childName, Object value) throws ConverterException, IllegalAccessException { Field field = null; try { field = Classes.getFieldEx(object.getClass(), Strings.toMemberName(childName)); } catch (NoSuchFieldException e) { // best effort approach: form to object mapping is not so strictly - i.e. ignores missing fields log.debug("Missing field |%s| from object |%s|.", childName, object.getClass()); return; } if (value instanceof UploadedFile) { if (Types.isKindOf(field.getType(), File.class)) { field.set(object, ((UploadedFile) value).getFile()); } else { field.set(object, value); } } else { field.set(object, converter.asObject((String) value, field.getType())); } } }
return Classes.getFieldEx(object.getClass(), Strings.toMemberName(property)).get(object); } catch (NoSuchFieldException expectedMissingField) { return getContentObject(object, property);