/** * Returns the field of the given object or null if it doesnt exist. */ public static Field getField(String fieldName, Object object) { return getField(fieldName, object.getClass()); }
/** * Returns the field of the given object or null if it doesnt exist. */ public static Field getField(String fieldName, Object object) { return getField(fieldName, object.getClass()); }
/** * Returns the field of the given class or null if it doesnt exist. */ public static Field getField(String fieldName, Class<?> clazz) { Field field = null; try { field = clazz.getDeclaredField(fieldName); } catch (SecurityException e) { throw LOG.unableToAccessField(field, clazz.getName()); } catch (NoSuchFieldException e) { // for some reason getDeclaredFields doesnt search superclasses // (which getFields() does ... but that gives only public fields) Class<?> superClass = clazz.getSuperclass(); if (superClass != null) { return getField(fieldName, superClass); } } return field; }
/** * Returns the field of the given class or null if it doesnt exist. */ public static Field getField(String fieldName, Class<?> clazz) { Field field = null; try { field = clazz.getDeclaredField(fieldName); } catch (SecurityException e) { throw LOG.unableToAccessField(field, clazz.getName()); } catch (NoSuchFieldException e) { // for some reason getDeclaredFields doesnt search superclasses // (which getFields() does ... but that gives only public fields) Class<?> superClass = clazz.getSuperclass(); if (superClass != null) { return getField(fieldName, superClass); } } return field; }
public static void applyFieldDeclaration(FieldDeclaration declaration, Object target) { Method setterMethod = ReflectUtil.getSetter(declaration.getName(), target.getClass(), declaration.getValue().getClass()); if(setterMethod != null) { try { setterMethod.invoke(target, declaration.getValue()); } catch (Exception e) { throw LOG.exceptionWhileApplyingFieldDeclatation(declaration.getName(), target.getClass().getName(), e); } } else { Field field = ReflectUtil.getField(declaration.getName(), target); ensureNotNull("Field definition uses unexisting field '" + declaration.getName() + "' on class " + target.getClass().getName(), "field", field); // Check if the delegate field's type is correct if (!fieldTypeCompatible(declaration, field)) { throw LOG.incompatibleTypeForFieldDeclaration(declaration, target, field); } ReflectUtil.setField(field, target, declaration.getValue()); } }
public static void applyFieldDeclaration(FieldDeclaration declaration, Object target) { Method setterMethod = ReflectUtil.getSetter(declaration.getName(), target.getClass(), declaration.getValue().getClass()); if(setterMethod != null) { try { setterMethod.invoke(target, declaration.getValue()); } catch (Exception e) { throw LOG.exceptionWhileApplyingFieldDeclatation(declaration.getName(), target.getClass().getName(), e); } } else { Field field = ReflectUtil.getField(declaration.getName(), target); ensureNotNull("Field definition uses unexisting field '" + declaration.getName() + "' on class " + target.getClass().getName(), "field", field); // Check if the delegate field's type is correct if (!fieldTypeCompatible(declaration, field)) { throw LOG.incompatibleTypeForFieldDeclaration(declaration, target, field); } ReflectUtil.setField(field, target, declaration.getValue()); } }
/** * Returns the field of the given object or null if it doesnt exist. */ public static Field getField(String fieldName, Object object) { return getField(fieldName, object.getClass()); }
/** * Returns the field of the given class or null if it doesnt exist. */ public static Field getField(String fieldName, Class<?> clazz) { Field field = null; try { field = clazz.getDeclaredField(fieldName); } catch (SecurityException e) { throw LOG.unableToAccessField(field, clazz.getName()); } catch (NoSuchFieldException e) { // for some reason getDeclaredFields doesnt search superclasses // (which getFields() does ... but that gives only public fields) Class<?> superClass = clazz.getSuperclass(); if (superClass != null) { return getField(fieldName, superClass); } } return field; }
public static void applyFieldDeclaration(FieldDeclaration declaration, Object target) { Method setterMethod = ReflectUtil.getSetter(declaration.getName(), target.getClass(), declaration.getValue().getClass()); if(setterMethod != null) { try { setterMethod.invoke(target, declaration.getValue()); } catch (Exception e) { throw LOG.exceptionWhileApplyingFieldDeclatation(declaration.getName(), target.getClass().getName(), e); } } else { Field field = ReflectUtil.getField(declaration.getName(), target); ensureNotNull("Field definition uses unexisting field '" + declaration.getName() + "' on class " + target.getClass().getName(), "field", field); // Check if the delegate field's type is correct if (!fieldTypeCompatible(declaration, field)) { throw LOG.incompatibleTypeForFieldDeclaration(declaration, target, field); } ReflectUtil.setField(field, target, declaration.getValue()); } }