/** * Will find the first field which has this annotation type * * @param annotationType * the annotation type to look for on this field * @return the name of the field which has this annotation OR null if none found */ public String getFieldNameByAnnotation(Class<? extends Annotation> annotationType) { if (annotationType == null) { throw new IllegalArgumentException("annotationType must not be null"); } String fieldName = null; Collection<ClassProperty> cps = namesToProperties.values(); for (ClassProperty classProperty : cps) { if (classProperty.getAnnotation(annotationType) != null) { fieldName = classProperty.getFieldName(); break; } } return fieldName; }
/** * @return the annotations present on the property */ public Annotation[] getAnnotations() { Collection<Annotation> c = getAnnotationsCollection(); return c.toArray(new Annotation[c.size()]); }
private void makeType() { if (getGetter() != null) { Method m = getGetter(); setType( m.getReturnType() ); } else if (getSetter() != null) { Method m = getSetter(); Class<?>[] params = m.getParameterTypes(); if (params != null) { if (params.length == 1) { setType( params[0] ); // normal setter } else if (params.length == 2) { setType( params[1] ); // indexed/mapped setter Field f = getField(); setType( f.getType() ); Class<?> type = getType(); if (type != null) { if (type.isArray()) {
/** * @return true if this field value can be retrieved and is public (either the getter or the field) */ public boolean isPublicGettable() { return (isPublicField() || getGetter() != null); } /**
if (propertyMap.containsKey(fieldName)) { p = propertyMap.get(fieldName); p.setGetter(method); } else { p = new ClassProperty(fieldName, method, null); propertyMap.put(fieldName, p); if (propertyMap.containsKey(fieldName)) { p = propertyMap.get(fieldName); p.setSetter(method); } else { p = new ClassProperty(fieldName, null, method); propertyMap.put(fieldName, p); ClassProperty p = entry.getValue(); if (p != null) { if (! includeStaticFields && p.isStatic()) { continue; // skip any props with static fields if (ignoredFieldNames.contains(p.getFieldName())) { continue; // skip ignored names } else { if (p.isGettable() && p.isSettable()) { properties.add(p);
throw new IllegalArgumentException("ClassProperty cannot be null"); if (cp.isPublicField()) { Field field = cp.getField(); try { field.set(obj, value); } catch (Exception e) { throw new FieldSetValueException("Field set failure setting value ("+value+") for name ("+cp.getFieldName()+") on: " + obj, cp.getFieldName(), value, obj, e); Method setter = cp.getSetter(); try { throw new FieldSetValueException("Setter method failure setting value ("+value+") for name ("+cp.getFieldName()+") on: " + obj, cp.getFieldName(), value, obj, e);
if (cp.isPublicField()) { Field field = cp.getField(); try { value = field.get(obj); } catch (Exception e) { throw new FieldGetValueException("Field get failure getting value for name ("+cp.getFieldName()+") from: " + obj, cp.getFieldName(), obj, e); Method getter = cp.getGetter(); try { } catch (Exception e) { throw new FieldGetValueException("Getter method failure getting value for name ("+cp.getFieldName()+") from: " + obj, cp.getFieldName(), obj, e);
if (! cp.isIndexed()) { throw new IllegalArgumentException("This field ("+name+") is not an indexed field"); isArray = cp.isArray(); +index+") value for name ("+cp.getFieldName()+") on: " + obj, cp.getFieldName(), obj, e); } else { // List Class<?> type = cp.getType(); if (type.isInterface()) { indexedObject = new ArrayList(index+1);
Annotation[] annotations = method.getAnnotations(); for (Annotation annotation : annotations) { if (annotation != null) cp.addAnnotation(annotation); if (cp.getField() == null) { cp.setField(field); // ensure there is a field set for all methods if (annotation != null) cp.addAnnotation(annotation);
if (! cp.isMapped()) { throw new IllegalArgumentException("This field ("+name+") is not an mapped field"); +key+") value for name ("+cp.getFieldName()+") on: " + obj, cp.getFieldName(), obj, e); Class<?> type = cp.getType(); if (type.isInterface()) { map = new ArrayOrderedMap(5);
if (! cp.isIndexed()) { throw new IllegalArgumentException("This field ("+name+") is not an indexed field"); isArray = cp.isArray(); +index+") value for name ("+cp.getFieldName()+") from: " + obj, cp.getFieldName(), obj, e);
/** * @return true if this field value can be set and is public (either the setter or the field) */ public boolean isPublicSettable() { boolean settable = false; if (! finalField) { // no setting finals if (isPublicField() || getSetter() != null) { settable = true; } } return settable; } /**
/** * @param publicFields * takes an array of fields and populates the internal storage mechanisms */ private void populateFields(Field[] publicFields) { // set the fields for (Field publicField : publicFields) { if (publicField != null) { String fieldName = publicField.getName(); if (!namesToProperties.containsKey(fieldName)) { ClassProperty p = new ClassProperty(fieldName, publicField); namesToProperties.put(fieldName, p); } } } }
if (! cp.isMapped()) { throw new IllegalArgumentException("This field ("+name+") is not an mapped field"); +key+") value for name ("+cp.getFieldName()+") from: " + obj, cp.getFieldName(), obj, e);
/** * @param name the fieldName * @return the type of this field * @throws FieldnameNotFoundException if this fieldName is invalid */ public Class<?> getFieldType(String name) { ClassProperty cp = getAnyPropertyOrFail(name); Class<?> type = cp.getType(); return type; }
/** * @return true if the associated field object is set */ public boolean isField() { return (getField() != null); } /**
/** * @param annotationType * the annotation type to look for on this field * @param name the fieldName * @return the annotation if found OR null if none found * @throws FieldnameNotFoundException if this fieldName is invalid */ @SuppressWarnings("hiding") public <T extends Annotation> T getFieldAnnotation(Class<T> annotationType, String name) { if (annotationType == null) { throw new IllegalArgumentException("annotationType must not be null"); } ClassProperty cp = getAnyPropertyOrFail(name); T a = cp.getAnnotation(annotationType); return a; }