Last active
August 29, 2015 14:01
-
-
Save uklance/0d461f9618b3a131ca3b to your computer and use it in GitHub Desktop.
Tapestry: Custom BeanModel to support "is" getters for javal.ang.Boolean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppModule { | |
public static BeanModelSource decorateBeanModelSource(BeanModelSource defaultImpl) { | |
return new MyBeanModelSource(defaultImpl); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyBeanModelSource { | |
private BeanModelSource defaultImpl; | |
public MyBeanModelSource(BeanModelSource defaultImpl) { | |
this.defaultImpl = defaultImpl; | |
} | |
@Deprecated | |
public <T> BeanModel<T> create(Class<T> beanClass, boolean filterReadOnlyProperties, Messages messages) { | |
return filterReadOnlyProperties | |
? createEditModel(beanClass, messages) | |
: createDisplayModel(beanClass, messages) | |
} | |
public <T> BeanModel<T> createEditModel(Class<T> beanClass, Messages messages) { | |
// I assume this works since the Boolean setters comply with the java beans spec. | |
return defaultImpl.createEditModel(beanClass, messages); | |
} | |
public <T> BeanModel<T> createDisplayModel(Class<T> beanClass, Messages messages) { | |
BeanModel<T> model = defaultImpl.createDisplayModel(beanClass, messages); | |
addBooleanIsGetters(model); | |
return model; | |
} | |
private <T> void addBooleanIsGetters(BeanModel<T> model) { | |
Method[] methods = model.getBeanType().getMethods(); | |
for (final Method method : methods) { | |
if (method.getParameterTypes().length == 0 | |
&& method.getReturnType().equals(Boolean.class) | |
&& method.getName().startsWith("is") { | |
char firstChar = Character.toLowercase(method.getName().charAt(2)); | |
String propertyName = firstChar + method.getName().substring(3); | |
PropertyConduit conduit = new PropertyConduit { | |
public Object get(Object instance) { | |
// TODO: try / catch | |
return method.invoke(instance); | |
} | |
public Class getPropertyType() { | |
returh Boolean.class; | |
} | |
public void set(Object instance, Object value) { | |
// TODO: implement | |
throw new UnsupportedOperationException(); | |
} | |
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { | |
return method.getAnnotation(annotationClass); | |
} | |
}; | |
model.add(propertyName, conduit); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment