001: package org.vraptor.introspector;
002:
003: import java.lang.reflect.Field;
004: import java.lang.reflect.Type;
005: import java.util.Collection;
006:
007: import org.apache.log4j.Logger;
008: import org.vraptor.annotations.Conversion;
009: import org.vraptor.annotations.Read;
010: import org.vraptor.component.ComponentInstantiationException;
011: import org.vraptor.component.FieldAnnotation;
012: import org.vraptor.converter.Converter;
013: import org.vraptor.reflection.GettingException;
014: import org.vraptor.reflection.ReflectionUtil;
015: import org.vraptor.reflection.SettingException;
016:
017: /**
018: * A field based read parameter.
019: *
020: * @author Guilherme Silveira
021: */
022: public class FieldReadParameter implements ReadParameter {
023:
024: private static final Logger LOG = Logger
025: .getLogger(FieldReadParameter.class);
026:
027: private final Field field;
028:
029: private final Class<? extends Converter> converter;
030:
031: private final String key;
032:
033: private final boolean mightCreate;
034:
035: public FieldReadParameter(FieldAnnotation<Read> info) {
036: Read read = info.getAnnotation();
037: this .field = info.getField();
038: this .key = new KeyExtractor().extractReadKey(info);
039: this .mightCreate = read.create();
040: if (field.isAnnotationPresent(Conversion.class)) {
041: this .converter = field.getAnnotation(Conversion.class)
042: .value();
043: } else {
044: this .converter = null;
045: }
046: if (LOG.isDebugEnabled()) {
047: LOG.debug("Created new FieldReadParameter " + this .key);
048: }
049: }
050:
051: /**
052: * The parameter key
053: *
054: * @return key
055: */
056: public String getKey() {
057: return key;
058: }
059:
060: /**
061: * Should it instantiate required fields on the fly?
062: *
063: * @return true if it should instantiate them
064: */
065: public boolean mightCreate() {
066: return this .mightCreate;
067: }
068:
069: /**
070: * Returns the overriden converter class attached to this read parameter
071: *
072: * @return the converter class
073: */
074: public Class<? extends Converter> getOverridenConverter() {
075: return this .converter;
076: }
077:
078: /**
079: *
080: * @throws SettingException
081: * setting exception
082: * @see org.vraptor.introspector.ReadParameter#guaranteeExistence(java.lang.Object)
083: */
084: public Object guaranteeExistence(Object component,
085: Object[] methodParams) throws SettingException {
086: try {
087: Object value = ReflectionUtil.get(component, field);
088: if (value == null && mightCreate()) {
089: if (Collection.class.isAssignableFrom(field.getType())) {
090: value = ReflectionUtil.instantiateCollection(field
091: .getType());
092: } else {
093: value = ReflectionUtil.genericInstantiate(field
094: .getType());
095: }
096: ReflectionUtil.setField(component, field, value);
097: }
098: return value;
099: } catch (GettingException e) {
100: throw new SettingException(e.getMessage(), e);
101: } catch (ComponentInstantiationException e) {
102: throw new SettingException(e.getMessage(), e.getCause());
103: }
104:
105: }
106:
107: public Class<?> getType() {
108: return this .field.getType();
109: }
110:
111: public void set(Object component, Object[] methodParams,
112: Object value) throws SettingException {
113: ReflectionUtil.setField(component, field, value);
114: }
115:
116: public Type getGenericType() {
117: return field.getGenericType();
118: }
119: }
|