Skip to content

Commit

Permalink
Optional getters
Browse files Browse the repository at this point in the history
Option to generate public static Getters and private static final fields instead of public static final fields mfuerstenau#15
  • Loading branch information
jfcameron committed Jul 7, 2018
1 parent 5df4ced commit 82921ac
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ buildConfigField 'String', 'MY_STR_FIELD', 'my message to the app'
buildConfigField 'char', 'MY_CHAR_FIELD', 'x'
```

If you are concerned about mutability of your referencetypes you may specify ```useGetters = true``` in buildConfig in order to hide your data behind getters.

### Per-SourceSet-Configuration
It's possible to configure per ```SourceSet```. Without per-SourceSet-configuration the ```BuildConfig``` is generated for the _default_ ```SourceSet``` which is ```main```. Th configuration closure provides a method ```sourceSets (Closure sourceSetsClosure)``` which can be used to accomplish this. The parameter ```sourceSetsClosure``` contains the names (case-sensitive) of ```SourceSet``` instances followed by a configuration closure which has the same properties and methods as the ```buildConfig``` configuration closure, minus a ```sourceSets```-method because we do not want endless recursion.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class BuildConfigPlugin implements Plugin<Project>
clsName = cfg.clsName ?: DEFAULT_CLASS_NAME
appName = cfg.appName ?: p.name
version = cfg.version ?: p.version
useGetters = cfg.useGetters ?: false
if (cfg.charset != null)
charset = cfg.charset
cfg.classFields.values ().each { ClassField cf ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class BuildConfigSourceSet implements Named
* Package for BuildConfig class.
*/
String packageName
/**
* Whether or not to hide buildinfo data behind getters
*/
boolean useGetters

Map<String, ClassField> classFields = new LinkedHashMap<>()

Expand Down Expand Up @@ -136,6 +140,7 @@ class BuildConfigSourceSet implements Named
result.packageName = other.packageName ?: this.packageName
result.version = other.version ?: this.version
result.clsName = other.clsName ?: this.clsName
result.useGetters = other.useGetters ?: this.useGetters
classFields.each {
if (it.value != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ class ClassWriter extends Writer

private final Writer delegate
private boolean isClass
private boolean useGetters

ClassWriter (Writer delegate)
ClassWriter (Writer delegate, boolean useGetters)
{
this.delegate = delegate
this.useGetters = useGetters
}

ClassWriter writePackage (String pkg) throws IOException
Expand Down Expand Up @@ -65,7 +67,7 @@ class ClassWriter extends Writer
if (cf.getDocumentation () != null && !cf.getDocumentation ().isEmpty ())
sb << " ${cf.documentation}\n"
cf.annotations.each { annot -> sb << " ${annot}\n" }
sb << " public static final ${cf.type} ${cf.name} = "
sb << " ${useGetters ? "private" : "public"} static final ${cf.type} ${cf.name} = "
switch (cf.type)
{
case "String":
Expand All @@ -78,6 +80,8 @@ class ClassWriter extends Writer
sb << cf.value
}
sb << ";\n\n"
if (useGetters)
sb << " public static final ${cf.type} get${cf.name}(){ return ${cf.name};} \n\n"
delegate.write (sb.toString ())
this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,19 @@ class GenerateBuildConfigTask extends DefaultTask
*/
@Input
String appName

/**
* Whether or not to hide buildinfo data behind getters
*/
@Input
boolean useGetters

/**
* Target directory of generated class.
*/
@OutputDirectory
File outputDir

/**
* Fields of generated class.
*/
Expand All @@ -118,6 +124,7 @@ class GenerateBuildConfigTask extends DefaultTask
appName = project.name
sourceSet = DEFAULT_SOURCESET
charset = DEFAULT_CHARSET
useGetters = false

LOG.debug "{}: GenerateBuildConfigTask created", name
}
Expand Down Expand Up @@ -237,7 +244,7 @@ class GenerateBuildConfigTask extends DefaultTask

new ClassWriter (
Files.newBufferedWriter (outputFile, Charset.forName (charset),
StandardOpenOption.CREATE)).withCloseable { w ->
StandardOpenOption.CREATE), useGetters).withCloseable { w ->
w.writePackage (packageName).writeClass (clsName)

mergedClassFields.values ().each { cf ->
Expand Down

0 comments on commit 82921ac

Please sign in to comment.