templates.thrift.service.perstruct.exception.decorator.class.vm Maven / Gradle / Ivy
#parse("${include}/header.include.vm")
#parse("${include}/generic.include.vm")
#parse("thrift/include/decorator.include.vm")
#if(!$sourceinfo.exception)
#set($codewriter.saveCurrentFile = false)
#stop
#end
#set ( $baseClass = $sourceinfo.baseClass)
#set ( $className ="${baseClass.simpleName}" )
#set ( $fields = $TOOL.sortByName($sourceinfo.getFieldDescriptors()) )
#set ( $pkg = ${sourceinfo.decoratorPackage})
$codewriter.setCurrentJavaFilename("$pkg", "${className}.java")
${sourceinfo.removeDecorateTypesFromImports()}
## 读取源码注释
## 默认#set语句不允许设置null,所以这里要通过#if判断来将$classDoc设置为空
#if($JavadocReader.read(${baseClass}))
#set ($classDoc = $JavadocReader.read(${baseClass}))
#else
#set ($classDoc = "")
#end
package $pkg;
#outputImports()
import com.gitee.l0km.xthrift.base.exception.BaseServiceException;
import java.io.PrintStream;
import java.io.PrintWriter;
import com.facebook.swift.codec.ThriftStruct;
#if(!${fields.empty})
import com.facebook.swift.codec.ThriftField;
import com.facebook.swift.codec.ThriftField.Requiredness;
#end
#importCodegen()
#if ($sourceinfo.needTransformer)
import com.gitee.l0km.xthrift.thrift.TypeTransformer;
#end
import static com.google.common.base.Preconditions.checkNotNull;
/**
#if(${classDoc.getClassComment()})
${classDoc.getClassComment(false,true,true)}
* ========================================
#end
* decorator pattern 装饰者模式代理{@link #typeName(${baseClass})}
* 代理对象必须有默认构造方法,String参数构造方法可选,必须是普通类型,不可以有@ThriftStruct注释
* 转发所有{@link #typeName(${baseClass})}get/set方法到{@link ${esc.hash}delegate()}指定的实例,
* $GENERAED_BY
*
*/
@ThriftStruct
public final class ${className} extends BaseServiceException
implements ThriftDecorator<#typeName(${baseClass})> {
private static final long serialVersionUID = 1L;
private final #typeName(${baseClass}) delegate;
public ${className}(){
this(new #typeName(${baseClass})());
}
#if($sourceinfo.hasStringConstructor())
public ${className}(String message){
this(new #typeName(${baseClass})(message));
}
#end
public ${className}(#typeName(${baseClass}) delegate) {
super(checkNotNull(delegate,"delegate is null").getMessage(),delegate.getCause());
// 检查被装饰类是否有@ThriftStruct注释
if(delegate.getClass().isAnnotationPresent(ThriftStruct.class)){
throw new IllegalArgumentException(
String.format("NOT ALLOW %s have @ThriftStruct annotation",delegate.getClass().getName()));
}
this.delegate = delegate;
}
/**
* @return 返回被装饰的{@link #typeName(${baseClass})}实例
*/
@Override
public #typeName(${baseClass}) delegate() {
return delegate;
}
@Override
public void printStackTrace() {
delegate().printStackTrace();
}
@Override
public void printStackTrace(PrintStream s) {
delegate().printStackTrace(s);
}
@Override
public void printStackTrace(PrintWriter s) {
delegate().printStackTrace(s);
}
@Override
public StackTraceElement[] getStackTrace() {
return delegate().getStackTrace();
}
@Override
public Throwable initCause(Throwable cause) {
return delegate().initCause(cause);
}
@Override
public int hashCode() {
return delegate().hashCode();
}
@Override
public boolean equals(Object obj) {
return delegate().equals(obj);
}
@Override
public String toString() {
return delegate().toString();
}
#set($fieldCount = 4 )
#foreach ($field in $fields)
#set($fieldCount = $fieldCount + 1 )
#set($propertyType = $field.propertyType)
#if($propertyType.primitive)
#set ($requireness = 'Requiredness.REQUIRED' )
#else
#set ($requireness = 'Requiredness.OPTIONAL' )
#end
#set ($readMethod = $field.readMethod)
$!{classDoc.getMethodComment($readMethod)}##
@ThriftField(value = $fieldCount,name = "$schemainfo.escapeField($field.name)",requiredness=$requireness)
#renderCodegen($field)
public $sourceinfo.toThriftType($readMethod.genericReturnType) ${readMethod.name}(){
return #returnValue($readMethod.genericReturnType,"delegate().${readMethod.name}()");
}
#set ($writeMethod = $field.writeMethod)
$!{classDoc.getMethodComment($writeMethod)}##
@ThriftField
public void ${writeMethod.name}($sourceinfo.toThriftType($readMethod.genericReturnType) $field.name){
delegate().${writeMethod.name}(#setValue($readMethod.genericReturnType,"$field.name"));
}
#end##foreach
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy