Permalink
Browse files

[generator] Add a switch to gate usage of plain java namespace

Instead of always looking for a swift-specific namespace in a .thrift file, and always falling back to the java namespace in case there is no swift-specific namespace, after this change we'll only look for one or the other (by default we'll use the swift-specific namespace, but we'll use java if '-use_java_namepsace' is passed on the command-line)
  • Loading branch information...
1 parent 225bc8c commit 5b5614bead85fa2fcdbf395f4a740fbdeb04c4f2 @andrewcox andrewcox committed Feb 15, 2013
@@ -56,7 +56,8 @@ public static void main(final String ... args) throws Exception
.overridePackage(cliConfig.overridePackage)
.defaultPackage(cliConfig.defaultPackage)
.generateIncludedCode(cliConfig.generateIncludedCode)
- .codeFlavor(cliConfig.generateBeans ? "java-regular" : "java-immutable");
+ .codeFlavor(cliConfig.generateBeans ? "java-regular" : "java-immutable")
+ .usePlainJavaNamespace(cliConfig.usePlainJavaNamespace);
for (SwiftGeneratorTweak tweak : cliConfig.tweaks) {
configBuilder.addTweak(tweak);
@@ -59,4 +59,10 @@
description = "Enable specific code generation tweaks"
)
public Set<SwiftGeneratorTweak> tweaks = Sets.newHashSet();
+
+ @Parameter(
+ names = "-use_java_namespace",
+ description = "Use 'java' namespace instead of 'java.swift' namespace"
+ )
+ public boolean usePlainJavaNamespace;
}
@@ -136,22 +136,24 @@ private void parseDocument(final URI thriftUri,
final Document document = context.getDocument();
final Header header = document.getHeader();
+ String effectiveJavaNamespace = "java.swift";
+ if (swiftGeneratorConfig.usePlainJavaNamespace()) {
+ effectiveJavaNamespace = "java";
+ }
+
// Override takes precedence
String javaPackage = swiftGeneratorConfig.getOverridePackage();
// Otherwise fallback on package specified in .thrift file
if (javaPackage == null) {
- javaPackage = header.getNamespace("java.swift");
- }
- if (javaPackage == null) {
- javaPackage = header.getNamespace("java");
+ javaPackage = header.getNamespace(effectiveJavaNamespace);
}
// Or the default if we don't have an override package or a package in the .thrift file
if (javaPackage == null) {
javaPackage = swiftGeneratorConfig.getDefaultPackage();
}
// If none of the above options get us a package to use, fail
- Preconditions.checkState(javaPackage != null, "thrift uri %s does not declare a swift namespace!", thriftUri);
+ Preconditions.checkState(javaPackage != null, "thrift uri %s does not declare a '%s' namespace!", thriftUri, effectiveJavaNamespace);
// Make a note that this document is a parent of all the documents included, directly or recursively
parentDocuments.push(thriftUri);
@@ -31,14 +31,17 @@
private final Set<SwiftGeneratorTweak> generatorTweaks;
private final boolean generateIncludedCode;
private final String codeFlavor;
-
- private SwiftGeneratorConfig(final URI inputBase,
- final File outputFolder,
- final String overridePackage,
- final String defaultPackage,
- final Set<SwiftGeneratorTweak> generatorTweaks,
- final boolean generateIncludedCode,
- final String codeFlavor)
+ private boolean usePlainJavaNamespace;
+
+ private SwiftGeneratorConfig(
+ final URI inputBase,
+ final File outputFolder,
+ final String overridePackage,
+ final String defaultPackage,
+ final Set<SwiftGeneratorTweak> generatorTweaks,
+ final boolean generateIncludedCode,
+ final String codeFlavor,
+ boolean usePlainJavaNamespace)
{
this.inputBase = inputBase;
this.outputFolder = outputFolder;
@@ -47,6 +50,7 @@ private SwiftGeneratorConfig(final URI inputBase,
this.generatorTweaks = generatorTweaks;
this.generateIncludedCode = generateIncludedCode;
this.codeFlavor = codeFlavor;
+ this.usePlainJavaNamespace = usePlainJavaNamespace;
}
public static Builder builder()
@@ -111,6 +115,14 @@ public String getCodeFlavor()
return codeFlavor;
}
+ /**
+ * Use namespace from 'namespace java ...' directive instead of from 'namespace java.swift ...'
+ */
+ public boolean usePlainJavaNamespace()
+ {
+ return usePlainJavaNamespace;
+ }
+
public static class Builder
{
private URI inputBase = null;
@@ -120,6 +132,7 @@ public String getCodeFlavor()
private Set<SwiftGeneratorTweak> generatorTweaks = EnumSet.noneOf(SwiftGeneratorTweak.class);
private boolean generateIncludedCode = false;
private String codeFlavor = null;
+ private boolean usePlainJavaNamespace = false;
private Builder()
{
@@ -139,7 +152,8 @@ public SwiftGeneratorConfig build()
defaultPackage,
generatorTweaks,
generateIncludedCode,
- codeFlavor);
+ codeFlavor,
+ usePlainJavaNamespace);
}
public Builder inputBase(final URI inputBase)
@@ -183,5 +197,11 @@ public Builder codeFlavor(final String codeFlavor)
this.codeFlavor = codeFlavor;
return this;
}
+
+ public Builder usePlainJavaNamespace(final boolean usePlainJavaNamespace)
+ {
+ this.usePlainJavaNamespace = usePlainJavaNamespace;
+ return this;
+ }
}
}

0 comments on commit 5b5614b

Please sign in to comment.