1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
package com.android.codegen
import com.github.javaparser.ast.Modifier
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration
import com.github.javaparser.ast.body.TypeDeclaration
import com.github.javaparser.ast.expr.*
import com.github.javaparser.ast.type.ClassOrInterfaceType
/**
* [ClassInfo] + utilities for printing out new class code with proper indentation and imports
*/
class ClassPrinter(
source: List<String>,
private val stringBuilder: StringBuilder,
var cliArgs: Array<String>
) : ClassInfo(source) {
val GENERATED_MEMBER_HEADER by lazy { "@$GeneratedMember" }
// Imports
val NonNull by lazy { classRef("android.annotation.NonNull") }
val NonEmpty by lazy { classRef("android.annotation.NonEmpty") }
val Nullable by lazy { classRef("android.annotation.Nullable") }
val TextUtils by lazy { classRef("android.text.TextUtils") }
val LinkedHashMap by lazy { classRef("java.util.LinkedHashMap") }
val Collections by lazy { classRef("java.util.Collections") }
val Preconditions by lazy { classRef("com.android.internal.util.Preconditions") }
val ArrayList by lazy { classRef("java.util.ArrayList") }
val DataClass by lazy { classRef("com.android.internal.util.DataClass") }
val DataClassEnum by lazy { classRef("com.android.internal.util.DataClass.Enum") }
val ParcelWith by lazy { classRef("com.android.internal.util.DataClass.ParcelWith") }
val PluralOf by lazy { classRef("com.android.internal.util.DataClass.PluralOf") }
val Each by lazy { classRef("com.android.internal.util.DataClass.Each") }
val DataClassGenerated by lazy { classRef("com.android.internal.util.DataClass.Generated") }
val DataClassSuppressConstDefs by lazy { classRef("com.android.internal.util.DataClass.SuppressConstDefsGeneration") }
val DataClassSuppress by lazy { classRef("com.android.internal.util.DataClass.Suppress") }
val GeneratedMember by lazy { classRef("com.android.internal.util.DataClass.Generated.Member") }
val Parcelling by lazy { classRef("com.android.internal.util.Parcelling") }
val Parcelable by lazy { classRef("android.os.Parcelable") }
val UnsupportedAppUsage by lazy { classRef("android.annotation.UnsupportedAppUsage") }
init {
val fieldsWithMissingNullablity = fields.filter { field ->
!field.isPrimitive
&& field.fieldAst.modifiers.none { it.keyword == Modifier.Keyword.TRANSIENT }
&& "@$Nullable" !in field.annotations
&& "@$NonNull" !in field.annotations
}
if (fieldsWithMissingNullablity.isNotEmpty()) {
abort("Non-primitive fields must have @$Nullable or @$NonNull annotation.\n" +
"Missing nullability annotations on: "
+ fieldsWithMissingNullablity.joinToString(", ") { it.name })
}
if (!classAst.isFinal &&
classAst.extendedTypes.any { it.nameAsString == Parcelable }) {
abort("Parcelable classes must be final")
}
}
/**
* Optionally shortens a class reference if there's a corresponding import present
*/
fun classRef(fullName: String): String {
if (cliArgs.contains(FLAG_NO_FULL_QUALIFIERS)) {
return fullName.split(".").dropWhile { it[0].isLowerCase() }.joinToString(".")
}
val pkg = fullName.substringBeforeLast(".")
val simpleName = fullName.substringAfterLast(".")
if (fileAst.imports.any { imprt ->
imprt.nameAsString == fullName
|| (imprt.isAsterisk && imprt.nameAsString == pkg)
}) {
return simpleName
} else {
val outerClass = pkg.substringAfterLast(".", "")
if (outerClass.firstOrNull()?.isUpperCase() == true) {
return classRef(pkg) + "." + simpleName
}
}
return fullName
}
/** @see classRef */
inline fun <reified T : Any> classRef(): String {
return classRef(T::class.java.name)
}
/** @see classRef */
fun memberRef(fullName: String): String {
val className = fullName.substringBeforeLast(".")
val methodName = fullName.substringAfterLast(".")
return if (fileAst.imports.any {
it.isStatic
&& (it.nameAsString == fullName
|| (it.isAsterisk && it.nameAsString == className))
}) {
className.substringAfterLast(".") + "." + methodName
} else {
classRef(className) + "." + methodName
}
}
val dataClassAnnotationFeatures = classAst.annotations
.find { it.nameAsString == DataClass }
?.let { it as? NormalAnnotationExpr }
?.pairs
?.map { pair -> pair.nameAsString to (pair.value as BooleanLiteralExpr).value }
?.toMap()
?: emptyMap()
val internalAnnotations = setOf(ParcelWith, DataClassEnum, PluralOf, UnsupportedAppUsage,
DataClassSuppressConstDefs)
val knownNonValidationAnnotations = internalAnnotations + Each + Nullable
/**
* @return whether the given feature is enabled
*/
operator fun FeatureFlag.invoke(): Boolean {
if (cliArgs.contains("--no-$kebabCase")) return false
if (cliArgs.contains("--$kebabCase")) return true
val annotationKey = "gen$upperCamelCase"
if (dataClassAnnotationFeatures.containsKey(annotationKey)) {
return dataClassAnnotationFeatures[annotationKey]!!
}
if (cliArgs.contains("--all")) return true
if (hidden) return true
return when (this) {
FeatureFlag.SETTERS ->
!FeatureFlag.CONSTRUCTOR() && !FeatureFlag.BUILDER() && fields.any { !it.isFinal }
FeatureFlag.BUILDER -> cliArgs.contains(FLAG_BUILDER_PROTECTED_SETTERS)
|| fields.any { it.hasDefault }
|| onByDefault
FeatureFlag.CONSTRUCTOR -> !FeatureFlag.BUILDER()
FeatureFlag.PARCELABLE -> "Parcelable" in superInterfaces
FeatureFlag.AIDL -> FeatureFlag.PARCELABLE()
FeatureFlag.IMPLICIT_NONNULL -> fields.any { it.isNullable }
&& fields.none { "@$NonNull" in it.annotations }
else -> onByDefault
}
}
val FeatureFlag.hidden
get(): Boolean = when {
cliArgs.contains("--hidden-$kebabCase") -> true
this == FeatureFlag.BUILD_UPON -> FeatureFlag.BUILDER.hidden
else -> false
}
var currentIndent = INDENT_SINGLE
private set
fun pushIndent() {
currentIndent += INDENT_SINGLE
}
fun popIndent() {
currentIndent = currentIndent.substring(0, currentIndent.length - INDENT_SINGLE.length)
}
fun backspace() = stringBuilder.setLength(stringBuilder.length - 1)
val lastChar get() = stringBuilder[stringBuilder.length - 1]
private fun appendRaw(s: String) {
stringBuilder.append(s)
}
fun append(s: String) {
if (s.isBlank() && s != "\n") {
appendRaw(s)
} else {
appendRaw(s.lines().map { line ->
if (line.startsWith(" *")) line else line.trimStart()
}.joinToString("\n$currentIndent"))
}
}
fun appendSameLine(s: String) {
while (lastChar.isWhitespace() || lastChar.isNewline()) {
backspace()
}
appendRaw(s)
}
fun rmEmptyLine() {
while (lastChar.isWhitespaceNonNewline()) backspace()
if (lastChar.isNewline()) backspace()
}
/**
* Syntactic sugar for:
* ```
* +"code()";
* ```
* to append the given string plus a newline
*/
operator fun String.unaryPlus() = append("$this\n")
/**
* Syntactic sugar for:
* ```
* !"code()";
* ```
* to append the given string without a newline
*/
operator fun String.not() = append(this)
/**
* Syntactic sugar for:
* ```
* ... {
* ...
* }+";"
* ```
* to append a ';' on same line after a block, and a newline afterwards
*/
operator fun Unit.plus(s: String) {
appendSameLine(s)
+""
}
/**
* A multi-purpose syntactic sugar for appending the given string plus anything generated in
* the given [block], the latter with the appropriate deeper indent,
* and resetting the indent back to original at the end
*
* Usage examples:
*
* ```
* "if (...)" {
* ...
* }
* ```
* to append a corresponding if block appropriate indentation
*
* ```
* "void foo(...)" {
* ...
* }
* ```
* similar to the previous one, plus an extra empty line after the function body
*
* ```
* "void foo(" {
* <args code>
* }
* ```
* to use proper indentation for args code and close the bracket on same line at end
*
* ```
* "..." {
* ...
* }
* to use the correct indentation for inner code, resetting it at the end
*/
inline operator fun String.invoke(block: ClassPrinter.() -> Unit) {
if (this == " {") {
appendSameLine(this)
} else {
append(this)
}
when {
endsWith("(") -> {
indentedBy(2, block)
appendSameLine(")")
}
endsWith("{") || endsWith(")") -> {
if (!endsWith("{")) appendSameLine(" {")
indentedBy(1, block)
+"}"
if ((endsWith(") {") || endsWith(")") || this == " {")
&& !startsWith("synchronized")
&& !startsWith("switch")
&& !startsWith("if ")
&& !contains(" else ")
&& !contains("new ")
&& !contains("return ")) {
+"" // extra line after function definitions
}
}
else -> indentedBy(2, block)
}
}
inline fun indentedBy(level: Int, block: ClassPrinter.() -> Unit) {
append("\n")
level times {
append(INDENT_SINGLE)
pushIndent()
}
block()
level times { popIndent() }
rmEmptyLine()
+""
}
inline fun Iterable<FieldInfo>.forEachTrimmingTrailingComma(b: FieldInfo.() -> Unit) {
forEachApply {
b()
if (isLast) {
while (lastChar == ' ' || lastChar == '\n') backspace()
if (lastChar == ',') backspace()
}
}
}
inline operator fun <R> invoke(f: ClassPrinter.() -> R): R = run(f)
var BuilderClass = CANONICAL_BUILDER_CLASS
var BuilderType = BuilderClass + genericArgs
val customBaseBuilderAst: ClassOrInterfaceDeclaration? by lazy {
nestedClasses.find { it.nameAsString == BASE_BUILDER_CLASS }
}
val suppressedMembers by lazy {
getSuppressedMembers(classAst)
}
val builderSuppressedMembers by lazy {
getSuppressedMembers(customBaseBuilderAst)
}
private fun getSuppressedMembers(clazz: ClassOrInterfaceDeclaration?): List<String> {
return clazz
?.annotations
?.find { it.nameAsString == DataClassSuppress }
?.as_<SingleMemberAnnotationExpr>()
?.memberValue
?.run {
when (this) {
is ArrayInitializerExpr -> values.map { it.asLiteralStringValueExpr().value }
is StringLiteralExpr -> listOf(value)
else -> abort("Can't parse annotation arg: $this")
}
}
?: emptyList()
}
fun isMethodGenerationSuppressed(name: String, vararg argTypes: String): Boolean {
return name in suppressedMembers || hasMethod(name, *argTypes)
}
fun hasMethod(name: String, vararg argTypes: String): Boolean {
return classAst.methods.any {
it.name.asString() == name &&
it.parameters.map { it.type.asString() } == argTypes.toList()
}
}
val lazyTransientFields = classAst.fields
.filter { it.isTransient && !it.isStatic }
.mapIndexed { i, node -> FieldInfo(index = i, fieldAst = node, classInfo = this) }
.filter { hasMethod("lazyInit${it.NameUpperCamel}") }
init {
val builderFactoryOverride = classAst.methods.find {
it.isStatic && it.nameAsString == "builder"
}
if (builderFactoryOverride != null) {
BuilderClass = (builderFactoryOverride.type as ClassOrInterfaceType).nameAsString
BuilderType = builderFactoryOverride.type.asString()
} else {
val builderExtension = (fileAst.types
+ classAst.childNodes.filterIsInstance(TypeDeclaration::class.java)).find {
it.nameAsString == CANONICAL_BUILDER_CLASS
}
if (builderExtension != null) {
BuilderClass = BASE_BUILDER_CLASS
val tp = (builderExtension as ClassOrInterfaceDeclaration).typeParameters
BuilderType = if (tp.isEmpty()) BuilderClass
else "$BuilderClass<${tp.map { it.nameAsString }.joinToString(", ")}>"
}
}
}
}
|