/** * Set instance or class field value. Try to set field value throwing exception if field not found. If * <code>object</code> argument is a class, named field should be static; otherwise exception is thrown. * <p> * This setter tries to adapt <code>value</code> to field type: if <code>value</code> is a string and field type is * not, delegates {@link Converter#asObject(String, Class)}. Anyway, if <code>value</code> is not a string it should * be assignable to field type otherwise bug error is thrown. * * @param object instance or class to set field value to, * @param fieldName field name, * @param value field value. * @throws IllegalArgumentException if <code>object</code> or <code>fieldName</code> argument is null. * @throws NoSuchBeingException if field not found. * @throws BugError if object is null and field is not static or if object is not null and field is static. * @throws BugError if value is not assignable to field type. */ public static void setFieldValue(Object object, String fieldName, Object value) { Params.notNull(object, "Object instance or class"); Params.notNull(fieldName, "Field name"); if(object instanceof Class<?>) { setFieldValue(null, (Class<?>)object, fieldName, value); } else { setFieldValue(object, object.getClass(), fieldName, value); } }
/** * Set instance field declared into superclass. Try to set field value throwing exception if field is not declared * into superclass; if field is static object instance should be null. * * @param object instance to set field value to or null if field is static, * @param clazz instance superclass where field is declared, * @param fieldName field name, * @param value field value. * @throws NoSuchBeingException if field not found. * @throws BugError if object is null and field is not static or if object is not null and field is static. */ public static void setFieldValue(Object object, Class<?> clazz, String fieldName, Object value) { Field field = getField(clazz, fieldName); if(object == null ^ Modifier.isStatic(field.getModifiers())) { throw new BugError("Cannot access static field |%s| from instance |%s|.", fieldName, clazz); } setFieldValue(object, field, value); }
/** * Inject dependencies described by given managed class into related managed instance. For every dependency field retrieve * its value using {@link DependencyProcessor#getDependencyValue(ManagedClassSPI, Class)} and inject it reflexively. * * @param managedClass managed class, * @param instance instance of given managed class. * @throws BugError if a field dependency cannot be resolved. */ @Override public void postProcessInstance(ManagedClassSPI managedClass, Object instance) { if (instance == null) { // null instance is silently ignored since container ensure not null instance argument return; } for (Field dependency : managedClass.getDependencies()) { if (dependency.isSynthetic()) { // it seems there can be injected fields, created via byte code manipulation, when run with test coverage active // not clear why and how but was consistently observed on mock object from unit test run with coverage continue; } Classes.setFieldValue(instance, dependency, getDependencyValue(managedClass, dependency.getType())); } } }
setFieldValue(object, field, instance);
for (Parameter parameter : parameters) { Field field = Classes.getField(type, Strings.toMemberName(parameter.getName())); Classes.setFieldValue(object, field, asObject(parameter.getValue(), field.getType()));
/** * Initialize instance fields from managed class configuration object. * * @param managedClass managed class, * @param instance instance of managed class. * @throws ConverterException if configured value cannot be converted to field type. * @throws BugError if attempt to assign instance field to not POJO type. */ @Override public void postProcessInstance(ManagedClassSPI managedClass, Object instance) { Config config = managedClass.getConfig(); if (config == null) { return; } List<Config> fields = config.findChildren("instance-field"); if (!fields.isEmpty() && !InstanceType.POJO.equals(managedClass.getInstanceType())) { throw new BugError("Cannot assign instance field on non %s type.", InstanceType.POJO); } for (Config field : fields) { Classes.setFieldValue(instance, field.getAttribute("name"), field.getAttribute("value")); } } }
Classes.setFieldValue(null, field, config.getAttribute("value", field.getType()));
Classes.setFieldValue(null, staticField, value);