Search in sources :

Example 1 with EditorType

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

the class RegistryType method getType.

// -------------------------------------------- //
// GET TYPE
// -------------------------------------------- //
public static Type<?> getType(Field field, java.lang.reflect.Type fieldType, boolean strictThrow) {
    if (field != null) {
        try {
            EditorType annotationType = ReflectionUtil.getAnnotation(field, EditorType.class);
            if (annotationType != null) {
                Class<?> typeClass = annotationType.value();
                Type<?> type = ReflectionUtil.getSingletonInstance(typeClass);
                return type;
            }
        } 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();
        }
    }
    if (fieldType != null) {
        if (fieldType instanceof ParameterizedType) {
            Class<?> fieldClass = field == null ? null : field.getType();
            List<Type<?>> innerTypes;
            if (ReflectionUtil.isRawTypeAssignableFromAny(BackstringSet.class, fieldType, fieldClass)) {
                innerTypes = getInnerTypes(field, fieldType, 1);
                return TypeBackstringSet.get((Type<? extends Enum>) innerTypes.get(0));
            }
            if (ReflectionUtil.isRawTypeAssignableFromAny(List.class, fieldType, fieldClass)) {
                innerTypes = getInnerTypes(field, fieldType, 1);
                return TypeList.get(innerTypes.get(0));
            }
            if (ReflectionUtil.isRawTypeAssignableFromAny(Set.class, fieldType, fieldClass)) {
                innerTypes = getInnerTypes(field, fieldType, 1);
                return TypeSet.get(innerTypes.get(0));
            }
            if (ReflectionUtil.isRawTypeAssignableFromAny(Map.Entry.class, fieldType, fieldClass)) {
                innerTypes = getInnerTypes(field, fieldType, 2);
                return TypeEntry.get(innerTypes.get(0), innerTypes.get(1));
            }
            if (ReflectionUtil.isRawTypeAssignableFromAny(Map.class, fieldType, fieldClass)) {
                innerTypes = getInnerTypes(field, fieldType, 2);
                return TypeMap.get(innerTypes.get(0), innerTypes.get(1));
            }
            if (strictThrow)
                throw new IllegalArgumentException("Unhandled ParameterizedType: " + fieldType);
            return null;
        }
        if (fieldType instanceof Class) {
            Type<?> type = registry.get(fieldType);
            if (strictThrow && type == null)
                throw new IllegalStateException(fieldType + " is not registered.");
            return type;
        }
        throw new IllegalArgumentException("Neither ParameterizedType nor Class: " + fieldType);
    }
    throw new IllegalArgumentException("No Information Supplied");
}
Also used : 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) EditorType(com.massivecraft.massivecore.command.editor.annotation.EditorType) TypeMap(com.massivecraft.massivecore.command.type.container.TypeMap) Map(java.util.Map) MassiveMap(com.massivecraft.massivecore.collections.MassiveMap)

Aggregations

MassiveMap (com.massivecraft.massivecore.collections.MassiveMap)1 EditorType (com.massivecraft.massivecore.command.editor.annotation.EditorType)1 TypeMap (com.massivecraft.massivecore.command.type.container.TypeMap)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 Map (java.util.Map)1