Thrift 生成私有的成員變量

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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章