package ru.ulstu.odin.model;

import ru.ulstu.core.error.OdinException;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class OdinCollectionField extends OdinField {
    private final String path;

    public OdinCollectionField(Field field) {
        super(field, OdinFieldType.COLLECTION);
        ParameterizedType genericType = (ParameterizedType) field.getGenericType();
        Type fieldElementClass = genericType.getActualTypeArguments()[0];
        try {
            OdinDto someInstance = (OdinDto) ((Class) (fieldElementClass)).newInstance();
            this.path = someInstance.getControllerPath();
        } catch (IllegalAccessException | InstantiationException e) {
            throw new OdinException(String.format("Can't create new instance, check default constructor of %s",
                    fieldElementClass.getTypeName()));
        }
    }

    public String getPath() {
        return path;
    }
}
