org.macrocloud.kernel.toolkit.convert.EnumToStringConverter Maven / Gradle / Ivy
package org.macrocloud.kernel.toolkit.convert;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.extern.slf4j.Slf4j;
import org.macrocloud.kernel.toolkit.utils.ConvertUtil;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.lang.Nullable;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* 接收参数 同 jackson Enum -》 String 转换.
*
* @author macro
*/
/** The Constant log. */
/** The Constant log. */
@Slf4j
public class EnumToStringConverter implements ConditionalGenericConverter {
/** 缓存 Enum 类信息,提供性能. */
private static final ConcurrentMap, AccessibleObject> ENUM_CACHE_MAP = new ConcurrentHashMap<>(8);
/**
* Gets the annotation.
*
* @param clazz the clazz
* @return the annotation
*/
@Nullable
private static AccessibleObject getAnnotation(Class> clazz) {
Set accessibleObjects = new HashSet<>();
// JsonValue METHOD, FIELD
Field[] fields = clazz.getDeclaredFields();
Collections.addAll(accessibleObjects, fields);
// methods
Method[] methods = clazz.getDeclaredMethods();
Collections.addAll(accessibleObjects, methods);
for (AccessibleObject accessibleObject : accessibleObjects) {
// 复用 jackson 的 JsonValue 注解
JsonValue jsonValue = accessibleObject.getAnnotation(JsonValue.class);
if (jsonValue != null && jsonValue.value()) {
accessibleObject.setAccessible(true);
return accessibleObject;
}
}
return null;
}
/**
* Title: matches
* Description:
.
*
* @param sourceType the source type
* @param targetType the target type
* @return true, if successful
* @see org.springframework.core.convert.converter.ConditionalConverter#matches(org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
*/
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
/**
* Title: getConvertibleTypes
* Description:
.
*
* @return the convertible types
* @see org.springframework.core.convert.converter.GenericConverter#getConvertibleTypes()
*/
@Override
public Set getConvertibleTypes() {
Set pairSet = new HashSet<>(3);
pairSet.add(new ConvertiblePair(Enum.class, String.class));
pairSet.add(new ConvertiblePair(Enum.class, Integer.class));
pairSet.add(new ConvertiblePair(Enum.class, Long.class));
return Collections.unmodifiableSet(pairSet);
}
/**
* Title: convert
* Description:
.
*
* @param source the source
* @param sourceType the source type
* @param targetType the target type
* @return the object
* @see org.springframework.core.convert.converter.GenericConverter#convert(java.lang.Object, org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
*/
@Override
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;
}
Class> sourceClazz = sourceType.getType();
AccessibleObject accessibleObject = ENUM_CACHE_MAP.computeIfAbsent(sourceClazz, EnumToStringConverter::getAnnotation);
Class> targetClazz = targetType.getType();
// 如果为null,走默认的转换
if (accessibleObject == null) {
if (String.class == targetClazz) {
return ((Enum) source).name();
}
int ordinal = ((Enum) source).ordinal();
return ConvertUtil.convert(ordinal, targetClazz);
}
try {
return EnumToStringConverter.invoke(sourceClazz, accessibleObject, source, targetClazz);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* Invoke.
*
* @param clazz the clazz
* @param accessibleObject the accessible object
* @param source the source
* @param targetClazz the target clazz
* @return the object
* @throws IllegalAccessException the illegal access exception
* @throws InvocationTargetException the invocation target exception
*/
@Nullable
private static Object invoke(Class> clazz, AccessibleObject accessibleObject, Object source, Class> targetClazz)
throws IllegalAccessException, InvocationTargetException {
Object value = null;
if (accessibleObject instanceof Field) {
Field field = (Field) accessibleObject;
value = field.get(source);
} else if (accessibleObject instanceof Method) {
Method method = (Method) accessibleObject;
Class> paramType = method.getParameterTypes()[0];
// 类型转换
Object object = ConvertUtil.convert(source, paramType);
value = method.invoke(clazz, object);
}
if (value == null) {
return null;
}
return ConvertUtil.convert(value, targetClazz);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy