Skip to content

Commit

Permalink
PLANNER-352 javadocs for new plumbing + extra fail fast + rename fiel…
Browse files Browse the repository at this point in the history
…d names to less abstract name
  • Loading branch information
ge0ffrey committed Jun 15, 2015
1 parent 0a9cc55 commit f95eca9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
Expand Up @@ -21,22 +21,28 @@
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type; import java.lang.reflect.Type;


/**
* A {@link MemberAccessor} based on a getter and optionally a setter.
*/
public final class BeanPropertyMemberAccessor implements MemberAccessor { public final class BeanPropertyMemberAccessor implements MemberAccessor {


private final Class<?> propertyType; private final Class<?> propertyType;
private final String propertyName; private final String propertyName;
private final Method readMethod; private final Method getterMethod;
private final Method writeMethod; private final Method setterMethod;


public BeanPropertyMemberAccessor(Method readMethod) { public BeanPropertyMemberAccessor(Method getterMethod) {
this.readMethod = readMethod; this.getterMethod = getterMethod;
readMethod.setAccessible(true); // Performance hack by avoiding security checks getterMethod.setAccessible(true); // Performance hack by avoiding security checks
Class declaringClass = readMethod.getDeclaringClass(); Class declaringClass = getterMethod.getDeclaringClass();
propertyType = readMethod.getReturnType(); if (!ReflectionHelper.isGetterMethod(getterMethod)) {
propertyName = ReflectionHelper.getGetterPropertyName(readMethod); throw new IllegalArgumentException("The getterMethod (" + getterMethod + ") is not a valid getter.");
writeMethod = ReflectionHelper.getSetterMethod(declaringClass, readMethod.getReturnType(), propertyName); }
if (writeMethod != null) { propertyType = getterMethod.getReturnType();
writeMethod.setAccessible(true); // Performance hack by avoiding security checks propertyName = ReflectionHelper.getGetterPropertyName(getterMethod);
setterMethod = ReflectionHelper.getSetterMethod(declaringClass, getterMethod.getReturnType(), propertyName);
if (setterMethod != null) {
setterMethod.setAccessible(true); // Performance hack by avoiding security checks
} }
} }


Expand All @@ -51,38 +57,38 @@ public Class<?> getType() {


@Override @Override
public Type getGenericType() { public Type getGenericType() {
return readMethod.getGenericReturnType(); return getterMethod.getGenericReturnType();
} }


public Object executeGetter(Object bean) { public Object executeGetter(Object bean) {
try { try {
return readMethod.invoke(bean); return getterMethod.invoke(bean);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new IllegalStateException("Cannot call property (" + propertyName throw new IllegalStateException("Cannot call property (" + propertyName
+ ") getter method (" + readMethod + ") on bean of class (" + bean.getClass() + ").", e); + ") getterMethod (" + getterMethod + ") on bean of class (" + bean.getClass() + ").", e);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
throw new IllegalStateException("The property (" + propertyName throw new IllegalStateException("The property (" + propertyName
+ ") getter method (" + readMethod + ") on bean of class (" + bean.getClass() + ") getterMethod (" + getterMethod + ") on bean of class (" + bean.getClass()
+ ") throws an exception.", + ") throws an exception.",
e.getCause()); e.getCause());
} }
} }


@Override @Override
public boolean supportSetter() { public boolean supportSetter() {
return writeMethod != null; return setterMethod != null;
} }


public void executeSetter(Object bean, Object value) { public void executeSetter(Object bean, Object value) {
try { try {
writeMethod.invoke(bean, value); setterMethod.invoke(bean, value);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new IllegalStateException("Cannot call property (" + propertyName throw new IllegalStateException("Cannot call property (" + propertyName
+ ") setter method (" + writeMethod + ") on bean of class (" + bean.getClass() + ") setterMethod (" + setterMethod + ") on bean of class (" + bean.getClass()
+ ") for value (" + value + ").", e); + ") for value (" + value + ").", e);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
throw new IllegalStateException("The property (" + propertyName throw new IllegalStateException("The property (" + propertyName
+ ") setter method (" + writeMethod + ") on bean of class (" + bean.getClass() + ") setterMethod (" + setterMethod + ") on bean of class (" + bean.getClass()
+ ") throws an exception for value (" + value + ").", + ") throws an exception for value (" + value + ").",
e.getCause()); e.getCause());
} }
Expand All @@ -94,22 +100,22 @@ public void executeSetter(Object bean, Object value) {


@Override @Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
return readMethod.isAnnotationPresent(annotationClass); return getterMethod.isAnnotationPresent(annotationClass);
} }


@Override @Override
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return readMethod.getAnnotation(annotationClass); return getterMethod.getAnnotation(annotationClass);
} }


@Override @Override
public Annotation[] getAnnotations() { public Annotation[] getAnnotations() {
return readMethod.getAnnotations(); return getterMethod.getAnnotations();
} }


@Override @Override
public Annotation[] getDeclaredAnnotations() { public Annotation[] getDeclaredAnnotations() {
return readMethod.getDeclaredAnnotations(); return getterMethod.getDeclaredAnnotations();
} }


} }
Expand Up @@ -22,6 +22,9 @@
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type; import java.lang.reflect.Type;


/**
* A {@link MemberAccessor} based on a field.
*/
public final class FieldMemberAccessor implements MemberAccessor { public final class FieldMemberAccessor implements MemberAccessor {


private final Field field; private final Field field;
Expand Down
Expand Up @@ -24,7 +24,9 @@


/** /**
* Fast and easy access to a {@link Member} of a bean, * Fast and easy access to a {@link Member} of a bean,
* which is a property (with a getter and setter {@link Method}) or a {@link Field}. * which is a property (with a getter and optional setter {@link Method}) or a {@link Field}.
* @see BeanPropertyMemberAccessor
* @see FieldMemberAccessor
*/ */
public interface MemberAccessor extends AnnotatedElement { public interface MemberAccessor extends AnnotatedElement {


Expand Down

0 comments on commit f95eca9

Please sign in to comment.