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");
}
Aggregations