diff options
Diffstat (limited to 'tools/codegen/src')
6 files changed, 30 insertions, 14 deletions
diff --git a/tools/codegen/src/com/android/codegen/ClassInfo.kt b/tools/codegen/src/com/android/codegen/ClassInfo.kt index 578fb2898480..5061be2091e5 100644 --- a/tools/codegen/src/com/android/codegen/ClassInfo.kt +++ b/tools/codegen/src/com/android/codegen/ClassInfo.kt @@ -1,22 +1,37 @@ package com.android.codegen -import com.github.javaparser.JavaParser import com.github.javaparser.ParseProblemException +import com.github.javaparser.ParseResult +import com.github.javaparser.ast.CompilationUnit import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration open class ClassInfo(val sourceLines: List<String>) { private val userSourceCode = (sourceLines + "}").joinToString("\n") - val fileAst = try { - JavaParser.parse(userSourceCode)!! + val fileAst: CompilationUnit = try { + JAVA_PARSER.parse(userSourceCode).throwIfFailed() } catch (e: ParseProblemException) { - throw RuntimeException("Failed to parse code:\n" + + throw parseFailed(cause = e) + } + + fun <T> ParseResult<T>.throwIfFailed(): T { + if (problems.isNotEmpty()) { + throw parseFailed( + desc = this@throwIfFailed.problems.joinToString("\n"), + cause = this@throwIfFailed.problems.mapNotNull { it.cause.orElse(null) }.firstOrNull()) + } + return result.get() + } + + private fun parseFailed(cause: Throwable? = null, desc: String = ""): RuntimeException { + return RuntimeException("Failed to parse code:\n" + userSourceCode .lines() .mapIndexed { lnNum, ln -> "/*$lnNum*/$ln" } - .joinToString("\n"), - e) + .joinToString("\n") + "\n$desc", + cause) } + val classAst = fileAst.types[0] as ClassOrInterfaceDeclaration val nestedClasses = classAst.members.filterIsInstance<ClassOrInterfaceDeclaration>() diff --git a/tools/codegen/src/com/android/codegen/ClassPrinter.kt b/tools/codegen/src/com/android/codegen/ClassPrinter.kt index f1645ea9a3bb..22b5d8815e7d 100644 --- a/tools/codegen/src/com/android/codegen/ClassPrinter.kt +++ b/tools/codegen/src/com/android/codegen/ClassPrinter.kt @@ -42,7 +42,7 @@ class ClassPrinter( init { val fieldsWithMissingNullablity = fields.filter { field -> !field.isPrimitive - && Modifier.TRANSIENT !in field.fieldAst.modifiers + && field.fieldAst.modifiers.none { it.keyword == Modifier.Keyword.TRANSIENT } && "@$Nullable" !in field.annotations && "@$NonNull" !in field.annotations } diff --git a/tools/codegen/src/com/android/codegen/FieldInfo.kt b/tools/codegen/src/com/android/codegen/FieldInfo.kt index 74e79489ad7d..6b0009ccff76 100644 --- a/tools/codegen/src/com/android/codegen/FieldInfo.kt +++ b/tools/codegen/src/com/android/codegen/FieldInfo.kt @@ -1,6 +1,5 @@ package com.android.codegen -import com.github.javaparser.JavaParser import com.github.javaparser.ast.body.FieldDeclaration import com.github.javaparser.ast.expr.ClassExpr import com.github.javaparser.ast.expr.Name @@ -115,8 +114,9 @@ data class FieldInfo( classPrinter { fieldAst.addAnnotation(SingleMemberAnnotationExpr( Name(ParcelWith), - ClassExpr(JavaParser.parseClassOrInterfaceType( - "$Parcelling.BuiltIn.For$FieldClass")))) + ClassExpr(JAVA_PARSER + .parseClassOrInterfaceType("$Parcelling.BuiltIn.For$FieldClass") + .throwIfFailed()))) } } fieldAst.annotations.map { it.removeComment().toString() } diff --git a/tools/codegen/src/com/android/codegen/InputSignaturesComputation.kt b/tools/codegen/src/com/android/codegen/InputSignaturesComputation.kt index 1e7a2674006b..1b514d7a74a0 100644 --- a/tools/codegen/src/com/android/codegen/InputSignaturesComputation.kt +++ b/tools/codegen/src/com/android/codegen/InputSignaturesComputation.kt @@ -18,7 +18,7 @@ private fun ClassPrinter.generateInputSignaturesForClass(classAst: ClassOrInterf return classAst.fields.map { fieldAst -> buildString { - append(fieldAst.modifiers.joinToString(" ") { it.asString() }) + append(fieldAst.modifiers.joinToString(" ") { it.keyword.asString() }) append(" ") append(annotationsToString(fieldAst)) append(" ") @@ -28,7 +28,7 @@ private fun ClassPrinter.generateInputSignaturesForClass(classAst: ClassOrInterf } } + classAst.methods.map { methodAst -> buildString { - append(methodAst.modifiers.joinToString(" ") { it.asString() }) + append(methodAst.modifiers.joinToString(" ") { it.keyword.asString() }) append(" ") append(annotationsToString(methodAst)) append(" ") diff --git a/tools/codegen/src/com/android/codegen/Main.kt b/tools/codegen/src/com/android/codegen/Main.kt index f71bfd302d2e..0f932f3c34e1 100755 --- a/tools/codegen/src/com/android/codegen/Main.kt +++ b/tools/codegen/src/com/android/codegen/Main.kt @@ -1,5 +1,6 @@ package com.android.codegen +import com.github.javaparser.JavaParser import java.io.File @@ -14,6 +15,7 @@ val BUILTIN_SPECIAL_PARCELLINGS = listOf("Pattern") const val FLAG_BUILDER_PROTECTED_SETTERS = "--builder-protected-setters" const val FLAG_NO_FULL_QUALIFIERS = "--no-full-qualifiers" +val JAVA_PARSER = JavaParser() /** @see [FeatureFlag] */ val USAGE = """ diff --git a/tools/codegen/src/com/android/codegen/Utils.kt b/tools/codegen/src/com/android/codegen/Utils.kt index 73ceac41682e..a1f068afa29a 100644 --- a/tools/codegen/src/com/android/codegen/Utils.kt +++ b/tools/codegen/src/com/android/codegen/Utils.kt @@ -79,7 +79,7 @@ fun currentTimestamp() = DateTimeFormatter .withZone(ZoneId.systemDefault()) .format(Instant.now()) -val NodeWithModifiers<*>.visibility get() = Modifier.getAccessSpecifier(modifiers) +val NodeWithModifiers<*>.visibility get() = accessSpecifier fun abort(msg: String): Nothing { System.err.println("ERROR: $msg") @@ -88,4 +88,3 @@ fun abort(msg: String): Nothing { } fun bitAtExpr(bitIndex: Int) = "0x${java.lang.Long.toHexString(1L shl bitIndex)}" - |