Search in sources :

Example 1 with InputForAnnotatedConstructor

use of beast.core.InputForAnnotatedConstructor in project beast2 by CompEvol.

the class XMLParserUtils method listInputs.

// replace
/**
 * return list of input types specified by Inputs or Param annotations
 * @param clazz Class to generate the list for
 * @param beastObject instantiation of the class, or null if not available
 * @return
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws SecurityException
 * @throws NoSuchMethodException
 * @throws IllegalArgumentException
 */
public static List<InputType> listInputs(Class<?> clazz, BEASTInterface beastObject) throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchMethodException, SecurityException {
    List<InputType> inputTypes = new ArrayList<>();
    // First, collect Input members
    try {
        if (beastObject == null) {
            beastObject = (BEASTInterface) clazz.newInstance();
        }
        List<Input<?>> inputs = null;
        inputs = beastObject.listInputs();
        for (Input<?> input : inputs) {
            if (!(input instanceof InputForAnnotatedConstructor)) {
                try {
                    // force class types to be determined
                    if (input.getType() == null) {
                        input.determineClass(beastObject);
                    }
                    inputTypes.add(new InputType(input.getName(), input.getType(), true, input.defaultValue));
                } catch (Exception e) {
                    // seems safe to ignore
                    e.printStackTrace();
                }
            }
        }
    } catch (InstantiationException e) {
    // this can happen if there is no constructor without arguments,
    // e.g. when there are annotated constructors only
    }
    // Second, collect types of annotated constructor
    Constructor<?>[] allConstructors = clazz.getDeclaredConstructors();
    for (Constructor<?> ctor : allConstructors) {
        Annotation[][] annotations = ctor.getParameterAnnotations();
        List<Param> paramAnnotations = new ArrayList<>();
        for (Annotation[] a0 : annotations) {
            for (Annotation a : a0) {
                if (a instanceof Param) {
                    paramAnnotations.add((Param) a);
                }
            }
        }
        Class<?>[] types = ctor.getParameterTypes();
        Type[] gtypes = ctor.getGenericParameterTypes();
        if (types.length > 0 && paramAnnotations.size() > 0) {
            int offset = 0;
            if (types.length == paramAnnotations.size() + 1) {
                offset = 1;
            }
            for (int i = 0; i < paramAnnotations.size(); i++) {
                Param param = paramAnnotations.get(i);
                Type type = types[i + offset];
                Class<?> clazz2 = null;
                try {
                    clazz2 = Class.forName(type.getTypeName());
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (clazz2.isAssignableFrom(List.class)) {
                    Type[] genericTypes2 = ((ParameterizedType) gtypes[i + offset]).getActualTypeArguments();
                    Class<?> theClass = (Class<?>) genericTypes2[0];
                    InputType t = new InputType(param.name(), theClass, false, param.defaultValue());
                    inputTypes.add(t);
                } else {
                    InputType t = new InputType(param.name(), types[i + offset], false, param.defaultValue());
                    inputTypes.add(t);
                }
            }
        }
    }
    return inputTypes;
}
Also used : InputForAnnotatedConstructor(beast.core.InputForAnnotatedConstructor) ArrayList(java.util.ArrayList) ParameterizedType(java.lang.reflect.ParameterizedType) Input(beast.core.Input) InputForAnnotatedConstructor(beast.core.InputForAnnotatedConstructor) Constructor(java.lang.reflect.Constructor) Annotation(java.lang.annotation.Annotation) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Param(beast.core.Param)

Aggregations

Input (beast.core.Input)1 InputForAnnotatedConstructor (beast.core.InputForAnnotatedConstructor)1 Param (beast.core.Param)1 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1