io.geewit.web.convert.converter.EnumNameConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gw-web-converter Show documentation
Show all versions of gw-web-converter Show documentation
A Java Utils Libraray By Geewit
The newest version!
package io.geewit.web.convert.converter;
import io.geewit.core.utils.enums.EnumUtils;
import io.geewit.core.utils.enums.Name;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalConverter;
import org.springframework.core.convert.converter.Converter;
/**
* 自动转换类型
* @param
* @author geewit
*/
public class EnumNameConverter & Name> implements Converter, ConditionalConverter {
private TypeDescriptor targetType;
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
if(!sourceType.isAssignableTo(TypeDescriptor.valueOf(String.class))) {
return false;
}
if(targetType.isAssignableTo(TypeDescriptor.valueOf(Enum.class)) && targetType.isAssignableTo(TypeDescriptor.valueOf(Name.class))) {
this.targetType = targetType;
return true;
} else {
return false;
}
}
@Override
public T convert(String source) {
return EnumUtils.forToken((Class) this.targetType.getObjectType(), source);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy