Where’s my Java reflection?, Part II
Just to give the final implementation started in “Where’s my Java reflection“, here it goes the implementation of the generator for the method bindErrors (check last post “Where’s my Java reflection“ to get into context):
private void composeBindErrorsMethod(TreeLogger logger,
SourceWriter sourceWriter, TypeOracle typeOracle) {
sourceWriter.println("public void bindErrors("
+ parameterizedType.getQualifiedSourceName()
+ " object, Map<String, List<String>> errors) {");
// Get the fields declared in the parameterized type
JField[] fields = parameterizedType.getFields();
for (JField field : fields) {
JClassType classType = field.getType().isClass();
if (classType != null) {
JClassType erroableType = typeOracle.findType(Errorable.class
.getName());
if (classType.isAssignableTo(erroableType)) {
sourceWriter.println("if (errors.containsKey(\""
+ field.getName() + "\")){");
sourceWriter.println("object." + field.getName()
+ ".setErrors(errors.get(\"" + field.getName()
+ "\"));");
sourceWriter.println("}");
}
}
}
sourceWriter.println("}");
Again, hope it helps anyone.