net.gdface.thrift.ThriftExceptionTransformer Maven / Gradle / Ivy
The newest version!
package net.gdface.thrift;
import static com.google.common.base.Preconditions.*;
import static net.gdface.thrift.ThriftUtils.*;
import java.lang.reflect.Constructor;
import java.util.Map;
import com.facebook.swift.codec.metadata.ThriftCatalogWithTransformer;
import com.facebook.swift.codec.metadata.ThriftStructMetadata;
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
/**
* 有{@link com.facebook.swift.codec.ThriftStruct}注释的异常类型之间的转换
* @author guyadong
*
* @param thrift 生成的client异常类型
* @param 实现 {@link ThriftDecorator}的装饰类异常类型
*/
public class ThriftExceptionTransformer>
implements Function{
private final ThriftStructMetadata leftMetadata;
private final ThriftStructMetadata rightMetadata;
public ThriftExceptionTransformer(Class left, Class right) {
checkArgument(isThriftException(left,right),"left and right must not be Exception with @ThriftStruct annotation" );
this.leftMetadata= ThriftCatalogWithTransformer.CATALOG.getThriftStructMetadata(left);
this.rightMetadata= ThriftCatalogWithTransformer.CATALOG.getThriftStructMetadata(right);
}
@Override
public R apply(L input) {
if(null == input){
return null;
}
try {
Map data = ThriftUtils.getFieldValues(input,leftMetadata);
String message = input.getMessage();
Constructor constructor = getStringConstructor();
if(null == constructor || Strings.isNullOrEmpty(message)){
// 默认构造方法创建实例
return constructStruct(data,rightMetadata);
}else{
// 调用String参数的构造方法创建实例
R instance = constructor.newInstance(message);
return fillStructField(data,rightMetadata,instance);
}
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
/**
* 获取String参数的构造方法,没有则返回{@code null}
* @return
*/
@SuppressWarnings("unchecked")
private Constructor getStringConstructor(){
return ThriftUtils.getConstructor((Class)rightMetadata.getStructClass(),String.class);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder()
.append("ThriftExceptionTransformer [leftClass=")
.append(leftMetadata.getStructType())
.append(", rightClass=")
.append(rightMetadata.getStructType())
.append("]");
return builder.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy