@@ -241,7 +241,13 @@ def emit_header(output):
241241
242242
243243def javadoc_sanitize (s ):
244- return s .replace ("<" , "<" ).replace (">" , ">" ).replace ("@" , "@" )
244+ return (
245+ s .replace ("<" , "<" )
246+ .replace (">" , ">" )
247+ .replace ("@" , "@" )
248+ # Kotlin supports nested comments, so change anything that looks like the start of a block comment.
249+ .replace ("/*" , "/*" )
250+ )
245251
246252
247253def generate_class (template , package , klass , annotations ):
@@ -261,19 +267,24 @@ def generate_class(template, package, klass, annotations):
261267
262268
263269def derive_package_and_class (file_path ):
264- """Determine the appropriate package and class name for a java file path."""
270+ """
271+ Determine the appropriate package and class name for a file path, and
272+ return whether a kotlin source should be generated rather than java.
273+ """
265274 path = file_path .split ("src/main/java/" , 1 )[1 ]
266275 package_path , klass_path = path .rsplit ("/" , 1 )
267276 package = package_path .replace ("/" , "." )
268- klass = klass_path .removesuffix (".java" )
269- return package , klass
277+ is_kotlin = klass_path .endswith (".kt" )
278+ klass = klass_path .removesuffix (".kt" ).removesuffix (".java" )
279+ return package , klass , is_kotlin
270280
271281
272282def emit_java (output ):
273- """Generate the CrashReporter java file."""
283+ """Generate the CrashReporter Java/Kotlin file."""
284+
285+ package , klass , is_kotlin = derive_package_and_class (output .name )
274286
275- package , klass = derive_package_and_class (output .name )
276- template = textwrap .dedent (
287+ java_template = textwrap .dedent (
277288 """\
278289 package ${package};
279290
@@ -315,6 +326,36 @@ def emit_java(output):
315326 """
316327 )
317328
329+ kotlin_template = textwrap .dedent (
330+ """\
331+ package ${package}
332+
333+ import androidx.annotation.AnyThread
334+
335+ /**
336+ * Constants used by the crash reporter. These are generated so that they
337+ * are kept in sync with the other C++ and JS users.
338+ */
339+ internal class ${class} {
340+ /** Crash Annotations */
341+ @AnyThread
342+ enum class Annotation private constructor (private val s: String, private val scope: String) {
343+ ${enum};
344+
345+ public override fun toString() = s
346+
347+ /** @return Whether the annotation should be included in crash pings. */
348+ public fun allowedInPing() = scope.equals("ping")
349+
350+ /** @return Whether the annotation should be included in crash reports. */
351+ public fun allowedInReport() = allowedInPing() || scope.equals("report")
352+ }
353+ }
354+ """
355+ )
356+
357+ template = kotlin_template if is_kotlin else java_template
358+
318359 annotations = read_annotations ()
319360 generated_class = generate_class (template , package , klass , annotations )
320361
0 commit comments