templates.thrift.client.perstruct.bean.decorator.class.vm Maven / Gradle / Ivy
#parse("${include}/header.include.vm")
#parse("${include}/generic.include.vm")
#parse("thrift/include/client.struct.decorator.include.vm")
#if(!$sourceinfo.bean)
#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")
package $pkg;
#outputImports()
import com.facebook.swift.codec.ThriftStruct;
import com.facebook.swift.codec.ThriftField;
import com.facebook.swift.codec.ThriftField.Requiredness;
#if ($sourceinfo.needTransformer)
import com.gitee.l0km.xthrift.thrift.TypeTransformer;
#end
/**
* decorator pattern 装饰者模式client端代理{@link #typeName(${baseClass})}
* 代理对象必须有默认构造方法,必须是普通类型,不可以有@ThriftStruct注释
* 转发所有{@link #typeName(${baseClass})}get/set方法到{@link ${esc.hash}delegate()}指定的实例,
* $GENERAED_BY
* @author guyadong
*
*/
@ThriftStruct
public final class ${className} implements ThriftDecorator<#typeName(${baseClass})> {
private final #typeName(${baseClass}) delegate;
public ${className}(){
this(new #typeName(${baseClass})());
}
public ${className}(#typeName(${baseClass}) delegate) {
if(null == delegate){
throw new NullPointerException("delegate is null");
}
// 检查被装饰类是否有@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 int hashCode() {
return delegate().hashCode();
}
@Override
public boolean equals(Object obj) {
return delegate().equals(obj);
}
@Override
public String toString() {
return delegate().toString();
}
#foreach ($field in $fields)
#set($propertyType = $field.propertyType)
#if($propertyType.primitive)
#set ($requireness = 'Requiredness.REQUIRED' )
#else
#set ($requireness = 'Requiredness.OPTIONAL' )
#end
@ThriftField(value = $velocityCount,requiredness=$requireness)
#set ($readMethod = $field.readMethod)
public $sourceinfo.toClientThriftType($readMethod.genericReturnType) ${readMethod.name}(){
return #returnValue($readMethod.genericReturnType,"delegate().${readMethod.name}()");
}
@ThriftField
#set ($writeMethod = $field.writeMethod)
public void ${writeMethod.name}($sourceinfo.toClientThriftType($readMethod.genericReturnType) value){
delegate().${writeMethod.name}(#setValue($readMethod.genericReturnType,'value'));
}
#end##foreach
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy