Swift2Thrift generates .thrift files from Swift-annotated Java files. The generated .thrift files can be fed to a Thrift compiler to generate client and/or server code for other languages.
$ java -jar swift2thrift-generator-cli-0.9.0-SNAPSHOT-standalone.jar
Usage: Swift2ThriftGenerator [options] <Swift-class-name...>
Options:
-allow_multiple_packages
Allow input classes to reside in different packages. The value of this
flag defines the generated java.swift namespace. Note that Swift classes
generated from the resultant Thrift file will all reside in one Java package
-package, -default_package
Default package for unqualified classes
Default: <empty string>
-map
Map of external type or service to include file
-namespace
Namespace for a particular language to include
-out
Thrift IDL output file, defaults to stdout
-v, -verbose
Show verbose messages
Default: false
Important notes:
- You should pass (fully-qualified) class names, not
.java(or.class) filenames. - Java ignores the classpath (set via
-classpathor$CLASSPATH) if run with-jar. Therefore, run Swift2Thrift with-classpath(or-cpor$CLASSPATH) that contains your Swift classes, but not with-jar.
- Use
-outto save the generated file. Omit-outto see the result onstdoutwhile tweaking options. - Use
-packageto save typing. Comparejava -cp ..... -package my.long.package.name Class1 Class2 Class3tojava -cp ..... my.long.package.name.Class1 my.long.package.name.Class2 my.long.package.name.Class3. - Use
-mapto haveincludelines in the generated.thriftfile for types/services you already have.thriftfiles for. Example:-map other.package.OtherThriftStruct path/to/file.thrift. Can be used multiple times. - Use
-namespaceto generate Thrift namespace declarations for other languages. Useful if you use the generated.thriftfiles to generate code (clients/servers) for other languages. Example:-namespace cpp mynamespace. Can be used multiple times. - To include nested classes on the command line, use
'MyTopLevelClass$MyNestedClass'. If running from the shell, be sure to use single quotes or the shell will eat the$MyNestedClass. - Swift2Thrift checks that all input classes are in the same Java package (whether
-packageis used or not) and errors if not. You can run Swift2Thrift several times to generate multiple.thriftfiles if your input classes are in different packages. - JavaDoc and method ordering can be preserved in the generated
.thriftfiles by compiling your classes withjavac -cp swift-javadoc-0.9.0-SNAPSHOT.jar <rest-of-the-command-line>before feeding them to Swift2Thrift. Alternatively, you can use@ThriftDocumentationand@ThriftOrderin your Java sources (but this clutters them, thus not recommended).
MY_CLASSES=swift-service/target/test-classes # just an example
java -cp swift2thrift-generator-cli/target/swift2thrift-generator-cli-0.9.0-SNAPSHOT-standalone.jar:$MY_CLASSES \
com.facebook.swift.generator.swift2thrift.Main -package com.facebook.swift.service.annotations \
DerivedServiceOne -map BaseService path/to/base.thrift \
-namespace py mycompany.thrift -namespace java com.mycompany -namespace cpp mycompanyInput: The file below compiled into swift-service/target/test-classes/com/facebook/swift/service/annotations/DerivedServiceOne.class
package com.facebook.swift.service.annotations;
import com.facebook.swift.service.ThriftMethod;
import com.facebook.swift.service.ThriftService;
@ThriftService("DerivedServiceOne")
public interface DerivedServiceOne extends BaseService
{
@ThriftMethod
public void fooOne();
}Output:
namespace java.swift com.facebook.swift.service.annotations
namespace py mycompany.thrift
namespace java com.mycompany
namespace cpp mycompany
include "path/to/base.thrift"
service DerivedServiceOne extends base.BaseService {
void fooOne();
}