org.parallelj.launching.reflect.InBuilderFactory Maven / Gradle / Ivy
package org.parallelj.launching.reflect;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.parallelj.internal.reflect.AnnotationBasedBuilderFactory;
import org.parallelj.internal.reflect.ElementBuilder;
import org.parallelj.launching.In;
import org.parallelj.launching.LaunchingMessageKind;
import org.parallelj.launching.inout.Argument;
import org.parallelj.launching.inout.IProgramInputOutputs;
import org.parallelj.launching.parser.Parser;
public class InBuilderFactory extends AnnotationBasedBuilderFactory {
public static class InBuilder extends AnnotationBasedBuilder {
@Override
public ElementBuilder complete() {
// Get all the informations about the field annotated with @In
String fieldName = this.getMember().getName();
Class> fieldType = this.getMember().getType();
Class extends Parser> parser = this.getMember().getAnnotation(In.class).parser();
int number = this.getMember().getAnnotation(In.class).index();
Method writeFieldMethod = null;
// Get the setter method for the Field
Class> programType = this.getBuilder().getType();
try {
for (PropertyDescriptor descriptor : Introspector.getBeanInfo(programType)
.getPropertyDescriptors()) {
if (descriptor.getName().equals(fieldName)) {
writeFieldMethod = descriptor.getWriteMethod();
}
}
} catch (IntrospectionException e) {
LaunchingMessageKind.ELAUNCH0004.format(programType, fieldName, e);
}
if (writeFieldMethod==null) {
LaunchingMessageKind.ELAUNCH0004.format(programType, fieldName, new Exception());
}
Argument argument = new Argument(fieldName, number, fieldType, parser, writeFieldMethod);
// Add the argument to the KProgram
((IProgramInputOutputs)this.getProgram()).addArgument(argument);
return super.complete();
}
}
public InBuilderFactory(
List>> types) {
super(types);
}
static final List>> types = new ArrayList>>();
static {
types.add(InBuilder.class);
}
public InBuilderFactory() {
super(types);
}
}