Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to generate Java private setters #212

Merged
merged 1 commit into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/Core/FileGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum GenerationParameterType {
case indent
case packageName
case javaNullabilityAnnotationType
case javaGeneratePackagePrivateSetters
}

// Most of these are derived from https://www.binpress.com/tutorial/objective-c-reserved-keywords/43
Expand Down
17 changes: 17 additions & 0 deletions Sources/Core/JavaModelRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ public struct JavaModelRenderer: JavaFileRenderer {
return getters
}

// Package-private setters are generated with if the flag --java_generate_package_private_setters is set
func renderModelPropertySetters() -> [JavaIR.Method] {
if params[.javaGeneratePackagePrivateSetters] == nil {
return []
}

let setters = transitiveProperties.map { param, schemaObj in
JavaIR.method([], "void set\(Languages.java.snakeCaseToCapitalizedPropertyName(param))(\(self.typeFromSchema(param, schemaObj)) value)") { [
"this." + Languages.java.snakeCaseToPropertyName(param) + " = value;",
] }
}
return setters + [JavaIR.method([], "set_bits(int bits)") { [
"this._bits = bits;",
] }]
}

func renderModelIsSetCheckers(modifiers: JavaModifier = [.public]) -> [JavaIR.Method] {
let getters = transitiveProperties.map { param, _ in
JavaIR.method(modifiers, "boolean get" + Languages.java.snakeCaseToCapitalizedPropertyName(param) + "IsSet()") { [
Expand Down Expand Up @@ -440,6 +456,7 @@ public struct JavaModelRenderer: JavaFileRenderer {
self.renderModelHashCode(),
] +
renderModelPropertyGetters() +
renderModelPropertySetters() +
renderModelIsSetCheckers(),
enums: enumProps,
innerClasses: [
Expand Down
5 changes: 5 additions & 0 deletions Sources/plank/Cli.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum FlagOptions: String {
case objectiveCClassPrefix = "objc_class_prefix"
case javaPackageName = "java_package_name"
case javaNullabilityAnnotationType = "java_nullability_annotation_type"
case javaGeneratePackagePrivateSetters = "java_generate_package_private_setters_beta"
case printDeps = "print_deps"
case noRecursive = "no_recursive"
case noRuntime = "no_runtime"
Expand All @@ -41,6 +42,7 @@ enum FlagOptions: String {
case .version: return false
case .javaPackageName: return true
case .javaNullabilityAnnotationType: return true
case .javaGeneratePackagePrivateSetters: return false
}
}
}
Expand All @@ -64,6 +66,7 @@ extension FlagOptions: HelpCommandOutput {
" Java:",
" --\(FlagOptions.javaPackageName.rawValue) - The package name to associate with generated Java sources",
" --\(FlagOptions.javaNullabilityAnnotationType.rawValue) - The type of nullability annotations to use. Can be either \"android-support\" (default) or \"androidx\".",
" --\(FlagOptions.javaGeneratePackagePrivateSetters.rawValue) - Generate package-private setter methods (beta)",
].joined(separator: "\n")
}
}
Expand Down Expand Up @@ -148,6 +151,7 @@ func handleGenerateCommand(withArguments arguments: [String]) {
let indent: String? = flags[.indent]
let packageName: String? = flags[.javaPackageName]
let javaNullabilityAnnotationType: String? = flags[.javaNullabilityAnnotationType]
let javaGeneratePackagePrivateSetters: String? = (flags[.javaGeneratePackagePrivateSetters] == nil) ? .none : .some("")

let generationParameters: GenerationParameters = [
(.recursive, recursive),
Expand All @@ -156,6 +160,7 @@ func handleGenerateCommand(withArguments arguments: [String]) {
(.indent, indent),
(.packageName, packageName),
(.javaNullabilityAnnotationType, javaNullabilityAnnotationType),
(.javaGeneratePackagePrivateSetters, javaGeneratePackagePrivateSetters),
].reduce([:]) { (dict: GenerationParameters, tuple: (GenerationParameterType, String?)) in
var mutableDict = dict
if let val = tuple.1 {
Expand Down