com.gitee.feizns.convert.method.valueof.TwoParametersValueOfConverter Maven / Gradle / Ivy
package com.gitee.feizns.convert.method.valueof;
import com.gitee.feizns.convert.method.AbstractMethodConverter;
import com.gitee.feizns.convert.method.MethodSelectionStrategyList;
import com.gitee.feizns.convert.method.MethodWrapper;
import com.gitee.feizns.reflect.MethodUtils;
import java.lang.reflect.Method;
import java.util.stream.Stream;
/**
* valueOf(Class targetType, Object args) : 双参数 valueOf 转换策略
* @author feizns
* @since 2019/5/27
*/
public class TwoParametersValueOfConverter extends AbstractValueOfMethodConverter {
@Override
protected Stream filterMethod(Stream methodStream) {
return methodStream.filter(method -> method.getParameterCount() == 2 && method.getParameterTypes()[0] == Class.class);
}
@Override
protected MethodSelectionStrategyList getSelectionStrategy(Class> targetType) {
return new MethodSelectionStrategyList(
new MethodSelectionStrategyByParameterImpl(),
new MethodSelectionStrategyByConverterImpl(this)
);
}
//根据方法的参数最优匹配选择最优的方法
private class MethodSelectionStrategyByParameterImpl extends AbstractMethodSelectionStrategyByParameter {
@Override
protected boolean isYou(Method method, Class> inheritanceClz) {
return method.getParameterTypes()[1] == inheritanceClz;
}
@Override
protected MethodWrapper getInvokeStrategy(Method method, Class> targetType, Object original) {
return () -> method == null ? null : MethodUtils.invoke(method, targetType, original);
}
}
//根据支持的转换器列表寻找最优的方法
private class MethodSelectionStrategyByConverterImpl extends AbstractMethodSelectionStrategyByConverter {
public MethodSelectionStrategyByConverterImpl(AbstractMethodConverter abstractMethodConverter) {
super(abstractMethodConverter);
}
@Override
protected Class> getType(Method method) {
return method.getParameterTypes()[1];
}
@Override
protected Object invoke(Method method, Class> targetType, Object original) {
return MethodUtils.invoke(method, targetType, original);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy