net.dreamlu.mica.core.utils.ConvertUtil Java Examples

The following examples show how to use net.dreamlu.mica.core.utils.ConvertUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EnumToStringConverter.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Nullable
@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;
}
 
Example #2
Source File: EnumToStringConverter.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: MicaConverter.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * cglib convert
 *
 * @param value     源对象属性
 * @param target    目标对象属性类
 * @param fieldName 目标的field名,原为 set 方法名,MicaBeanCopier 里做了更改
 * @return {Object}
 */
@Override
@Nullable
public Object convert(Object value, Class target, final Object fieldName) {
	if (value == null) {
		return null;
	}
	// 类型一样,不需要转换
	if (ClassUtil.isAssignableValue(target, value)) {
		return value;
	}
	try {
		TypeDescriptor targetDescriptor = MicaConverter.getTypeDescriptor(targetClazz, (String) fieldName);
		// 1. 判断 sourceClazz 为 Map
		if (Map.class.isAssignableFrom(sourceClazz)) {
			return ConvertUtil.convert(value, targetDescriptor);
		} else {
			TypeDescriptor sourceDescriptor = MicaConverter.getTypeDescriptor(sourceClazz, (String) fieldName);
			return ConvertUtil.convert(value, sourceDescriptor, targetDescriptor);
		}
	} catch (Throwable e) {
		log.warn("MicaConverter error", e);
		return null;
	}
}
 
Example #4
Source File: UserCopy2Test.java    From mica-example with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	// 设置 cglib 源码生成目录
	String sourcePath = "/Users/lcm/git/mica/mica-example/web-example/src/test/java";
	System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath);

	// 1. 初始化 user,赋值
	User user = new User();
	user.setId(250);
	user.setName("如梦技术");
	user.setAge(30);
	user.setBirthday(LocalDateTime.now());

	// 2. 初始化 userVo
	UserVo userVo = new UserVo();
	// 3. 构造 BeanCopier,不是用类型转换
	BeanCopier copier = BeanCopier.create(User.class, UserVo.class, true);
	// 4. 拷贝对象,不是用类型转换,转换器可以使用 null
	copier.copy(user, userVo, new Converter() {
		@Override
		public Object convert(Object o, Class aClass, Object o1) {
			if (o == null) {
				return null;
			}
			return ConvertUtil.convert(o, aClass);
		}
	});

	// 5. 打印结果:UserVo(name=如梦技术, age=30, birthday=19-4-30 下午9:45)
	System.out.println(userVo);
}
 
Example #5
Source File: CssQueryMethodInterceptor.java    From mica with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Nullable
@Override
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	// 如果是 toString eq 等方法都不准确,故直接返回死值
	if (ReflectionUtils.isObjectMethod(method)) {
		return methodProxy.invokeSuper(object, args);
	}
	// 非 bean 方法
	PropertyDescriptor propertyDescriptor = BeanUtils.findPropertyForMethod(method, clazz);
	if (propertyDescriptor == null) {
		return methodProxy.invokeSuper(object, args);
	}
	// 非 read 的方法,只处理 get 方法 is
	if (!method.equals(propertyDescriptor.getReadMethod())) {
		return methodProxy.invokeSuper(object, args);
	}
	// 兼容 lombok bug 强制首字母小写: https://github.com/rzwitserloot/lombok/issues/1861
	String fieldName = StringUtil.firstCharToLower(propertyDescriptor.getDisplayName());
	Field field = clazz.getDeclaredField(fieldName);
	if (field == null) {
		return methodProxy.invokeSuper(object, args);
	}
	CssQuery cssQuery = field.getAnnotation(CssQuery.class);
	// 没有注解,不代理
	if (cssQuery == null) {
		return methodProxy.invokeSuper(object, args);
	}
	Class<?> returnType = method.getReturnType();
	boolean isColl = Collection.class.isAssignableFrom(returnType);
	String cssQueryValue = cssQuery.value();
	// 是否为 bean 中 bean
	boolean isInner = cssQuery.inner();
	if (isInner) {
		return proxyInner(cssQueryValue, method, returnType, isColl);
	}
	Object proxyValue = proxyValue(cssQueryValue, cssQuery, returnType, isColl);
	if (String.class.isAssignableFrom(returnType)) {
		return proxyValue;
	}
	// 用于读取 field 上的注解
	TypeDescriptor typeDescriptor = new TypeDescriptor(field);
	return ConvertUtil.convert(proxyValue, typeDescriptor);
}
 
Example #6
Source File: BeanConvertTest.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public Double PrimitiveSpring() {
	return ConvertUtil.convert("1.23456", Double.TYPE);
}