Search in sources :

Example 1 with EditorTypeInner

use of com.massivecraft.massivecore.command.editor.annotation.EditorTypeInner in project MassiveCore by MassiveCraft.

the class RegistryType method getInnerTypes.

// -------------------------------------------- //
// GET INNER TYPES
// -------------------------------------------- //
public static List<Type<?>> getInnerTypes(Field field, java.lang.reflect.Type fieldType, int amountRequired) {
    // Annotation
    if (field != null) {
        try {
            EditorTypeInner annotation = ReflectionUtil.getAnnotation(field, EditorTypeInner.class);
            if (annotation != null) {
                // Create
                List<Type<?>> ret = new MassiveList<>();
                // Fill
                Class<?>[] innerTypeClasses = annotation.value();
                for (Class<?> innerTypeClass : innerTypeClasses) {
                    Type<?> innerType = ReflectionUtil.getSingletonInstance(innerTypeClass);
                    ret.add(innerType);
                }
                // Return
                return ret;
            }
        } catch (Throwable t) {
        // This has to do with backwards compatibility (Usually 1.7).
        // The annotations may trigger creation of type class instances.
        // Those type classes may refer to Bukkit classes not present.
        // This issue was first encountered for TypeDataItemStack.
        }
        if (fieldType == null) {
            fieldType = field.getGenericType();
        }
    }
    // Reflection
    if (fieldType != null) {
        if (fieldType instanceof ParameterizedType) {
            // Create
            List<Type<?>> ret = new MassiveList<>();
            // Fill
            ParameterizedType parameterizedType = (ParameterizedType) fieldType;
            int count = 0;
            for (java.lang.reflect.Type actualTypeArgument : parameterizedType.getActualTypeArguments()) {
                boolean strictThrow = (amountRequired < 0 || count < amountRequired);
                Type<?> innerType = getType(actualTypeArgument, strictThrow);
                ret.add(innerType);
                count++;
            }
            // Return
            return ret;
        }
        throw new IllegalArgumentException("Not ParameterizedType: " + fieldType);
    }
    throw new IllegalArgumentException("Failure");
}
Also used : EditorTypeInner(com.massivecraft.massivecore.command.editor.annotation.EditorTypeInner) MassiveList(com.massivecraft.massivecore.collections.MassiveList) ParameterizedType(java.lang.reflect.ParameterizedType) TypeFireworkEffectType(com.massivecraft.massivecore.command.type.enumeration.TypeFireworkEffectType) TypeOcelotType(com.massivecraft.massivecore.command.type.enumeration.TypeOcelotType) TypeRabbitType(com.massivecraft.massivecore.command.type.enumeration.TypeRabbitType) TypeWorldType(com.massivecraft.massivecore.command.type.enumeration.TypeWorldType) EditorType(com.massivecraft.massivecore.command.editor.annotation.EditorType) TypeSkeletonType(com.massivecraft.massivecore.command.type.enumeration.TypeSkeletonType) TypeEntityType(com.massivecraft.massivecore.command.type.enumeration.TypeEntityType) ParameterizedType(java.lang.reflect.ParameterizedType)

Aggregations

MassiveList (com.massivecraft.massivecore.collections.MassiveList)1 EditorType (com.massivecraft.massivecore.command.editor.annotation.EditorType)1 EditorTypeInner (com.massivecraft.massivecore.command.editor.annotation.EditorTypeInner)1 TypeEntityType (com.massivecraft.massivecore.command.type.enumeration.TypeEntityType)1 TypeFireworkEffectType (com.massivecraft.massivecore.command.type.enumeration.TypeFireworkEffectType)1 TypeOcelotType (com.massivecraft.massivecore.command.type.enumeration.TypeOcelotType)1 TypeRabbitType (com.massivecraft.massivecore.command.type.enumeration.TypeRabbitType)1 TypeSkeletonType (com.massivecraft.massivecore.command.type.enumeration.TypeSkeletonType)1 TypeWorldType (com.massivecraft.massivecore.command.type.enumeration.TypeWorldType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1