use of com.haulmont.cuba.core.config.EnumStore in project cuba by cuba-platform.
the class TypeFactory method getInstance.
/**
* Get a TypeFactory instance appropriate for the return type of the
* specified configuration interface method.
*
* @param configInterface The configuration interface.
* @param method The method.
* @return An appropriate TypeFactory.
* @throws IllegalArgumentException If the type is not supported.
*/
public static TypeFactory getInstance(Class<?> configInterface, Method method) {
Class<?> returnType = method.getReturnType();
if (returnType.isPrimitive()) {
returnType = ClassUtils.primitiveToWrapper(returnType);
}
Factory factory = ConfigUtil.getAnnotation(configInterface, method, Factory.class, true);
if (factory != null) {
try {
if ("".equals(factory.method())) {
return factory.factory().newInstance();
} else {
String methodName = factory.method();
Method factoryMethod = returnType.getMethod(methodName, String.class);
if (!isAcceptableMethod(returnType, factoryMethod)) {
throw new IllegalArgumentException("Invalid factory method: " + factoryMethod);
}
return new StaticTypeFactory(factoryMethod);
}
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException("Unable to instantiate an type factory", e);
}
} else {
if (Entity.class.isAssignableFrom(returnType)) {
return AppBeans.get(ENTITY_FACTORY_BEAN_NAME, TypeFactory.class);
} else {
if (EnumClass.class.isAssignableFrom(returnType)) {
EnumStore mode = ConfigUtil.getAnnotation(configInterface, method, EnumStore.class, true);
if (mode != null && EnumStoreMode.ID == mode.value()) {
@SuppressWarnings("unchecked") Class<EnumClass> enumeration = (Class<EnumClass>) returnType;
Class<?> idType = ConfigUtil.getEnumIdType(enumeration);
TypeFactory idFactory = getInferred(idType);
try {
Method fromIdMethod = returnType.getMethod("fromId", idType);
if (!isAcceptableMethod(returnType, fromIdMethod) || idFactory == null) {
throw new IllegalArgumentException("Cannot use method as factory method: " + method);
}
return new EnumClassFactory(idFactory, fromIdMethod);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("fromId method is not found for " + enumeration.getName());
}
}
}
TypeFactory factoryT = getInferred(returnType);
if (factoryT == null) {
throw new IllegalArgumentException("Unsupported return type for " + method);
}
return factoryT;
}
}
}
use of com.haulmont.cuba.core.config.EnumStore in project cuba by cuba-platform.
the class TypeStringify method getInstance.
/**
* Get a TypeStringify instance appropriate for the parameter type of the
* specified configuration interface method.
*
* @param configInterface The configuration interface.
* @param method The method.
* @return An appropriate TypeStringify.
* @throws IllegalArgumentException If the type is not supported.
*/
public static TypeStringify getInstance(Class<?> configInterface, Method method) {
Class<?> methodType = ConfigUtil.getMethodType(method);
try {
Stringify stringify = ConfigUtil.getAnnotation(configInterface, method, Stringify.class, true);
if (stringify != null) {
if ("".equals(stringify.method())) {
return stringify.stringify().newInstance();
} else {
String methodName = stringify.method();
return new MethodTypeStringify(methodType.getMethod(methodName));
}
} else {
if ((method.getParameterTypes().length > 0)) {
if (Entity.class.isAssignableFrom(method.getParameterTypes()[0])) {
return new EntityStringify();
}
}
if (EnumClass.class.isAssignableFrom(methodType)) {
EnumStore mode = ConfigUtil.getAnnotation(configInterface, method, EnumStore.class, true);
if (mode != null && EnumStoreMode.ID == mode.value()) {
@SuppressWarnings("unchecked") Class<EnumClass> enumeration = (Class<EnumClass>) methodType;
TypeStringify idStringify = getInferred(ConfigUtil.getEnumIdType(enumeration));
return new EnumClassStringify(idStringify);
}
}
return getInferred(methodType);
}
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | RuntimeException e) {
throw new RuntimeException("Type stringify error", e);
}
}