Thrift 生成私有的成员变量
1 编译安装
./configure && make
./make install
2 测试是否安装正确
thrift –help
Usage: thrift [options] file
Options:
-version Print the compiler version
-o dir Set the output directory for gen-* packages
(default: current directory)
-out dir Set the ouput location for generated files.
(no gen-* folder will be created)
-I dir Add a directory to the list of directories
searched for include directives
-nowarn Suppress all compiler warnings (BAD!)
-strict Strict compiler warnings on
-v[erbose] Verbose mode
-r[ecurse] Also generate included files
-debug Parse debug trace to stdout
--allow-neg-keys Allow negative field keys (Used to preserve protocol
compatibility with older .thrift files)
--allow-64bit-consts Do not print warnings about using 64-bit constants
--gen STR Generate code with a dynamically-registered generator.
STR has the form language[:key1=val1[,key2[,key3=val3]]].
Keys and values are options passed to the generator.
Many options will not require values.
很长,就不全列出来了。
3 写一个test.thrift 文件
语法为 Thrift interface description language,具体的就不一一介绍了,可以看官网和http://thrift-tutorial.readthedocs.org/en/latest/intro.html 给的教程
struct UserProfile {
1: i32 uid,
2: string name,
3: string blurb
}
service UserStorage {
void store(1: UserProfile user),
UserProfile retrieve(1: i32 uid)
}
4 生成私有成员变量的source code
这是普通的写法,生成的是public的成员变量,可以thrift –hlep 查看具体的用法。其中:
java (Java):
beans: Members will be private, and setter methods will return void.
private-members: Members will be private, but setter methods will return 'this' like usual.
nocamel: Do not use CamelCase field accessors with beans.
fullcamel: Convert underscored_accessor_or_service_names to camelCase.
android: Generated structures are Parcelable.
android_legacy: Do not use java.io.IOException(throwable) (available for Android 2.3 and above).
option_type: Wrap optional fields in an Option type.
java5: Generate Java 1.5 compliant code (includes android_legacy flag).
reuse-objects: Data objects will not be allocated, but existing instances will be used (read and write).
sorted_containers:
Use TreeSet/TreeMap instead of HashSet/HashMap as a implementation of set/map.
generated_annotations=[undated|suppress]:
undated: suppress the date at @Generated annotations
suppress: suppress @Generated annotations entirely
下面这两种方法是在生成 java 的source code时,成员变量为private,并生成不同的getter 和 setter方法。
thrift –gen:beans java test.thrift
thrift –gen:private-members test.thrift
5 进入gen-* 可以看到生成的source code
可以这样,指定生成的位置和thrift文件的位置。
thrift --gen java:private-members -out outdir pathToFile
在outdir
下生成source code
如果在 .thrift
文件中指定了namespace,如
namespace java com.lxy.thrift
则在 outdir/com/lxy/thrift
中生成source code。