1 /*
<lambda>null2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
5 package kotlinx.coroutines.tools
6
7 import com.google.gson.internal.*
8 import com.google.gson.stream.*
9 import java.io.*
10
11 data class ClassVisibility(val name: String, val visibility: String?, val members: Map<MemberSignature, MemberVisibility>)
12 data class MemberVisibility(val member: MemberSignature, val declaration: String?, val visibility: String?)
13 data class MemberSignature(val name: String, val desc: String)
14
15 private fun isPublic(visibility: String?, isPublishedApi: Boolean) = visibility == null || visibility == "public" || visibility == "protected" || (isPublishedApi && visibility == "internal")
16 fun ClassVisibility.isPublic(isPublishedApi: Boolean) = isPublic(visibility, isPublishedApi)
17 fun MemberVisibility.isPublic(isPublishedApi: Boolean) = isPublic(visibility, isPublishedApi)
18
19 fun MemberVisibility.isLateInit() = declaration != null && "lateinit var " in declaration
20
21 private val varValPrefix = Regex("va[lr]\\s+")
22 fun ClassVisibility.findSetterForProperty(property: MemberVisibility): MemberVisibility? {
23 // ad-hoc solution:
24 val declaration = property.declaration ?: return null
25 val match = varValPrefix.find(declaration) ?: return null
26 val name = declaration.substring(match.range.endInclusive + 1).substringBefore(':')
27 val setterName = "<set-$name>"
28 return members.values.find { it.declaration?.contains(setterName) ?: false }
29 }
30
readKotlinVisibilitiesnull31 fun readKotlinVisibilities(declarationFile: File): Map<String, ClassVisibility> {
32 val result = mutableListOf<ClassVisibility>()
33 declarationFile.bufferedReader().use { reader ->
34 val jsonReader = JsonReader(reader)
35 jsonReader.beginArray()
36 while (jsonReader.hasNext()) {
37 val classObject = Streams.parse(jsonReader).asJsonObject
38 result += with (classObject) {
39 val name = getAsJsonPrimitive("class").asString
40 val visibility = getAsJsonPrimitive("visibility")?.asString
41 val members = getAsJsonArray("members").map { it ->
42 with(it.asJsonObject) {
43 val name = getAsJsonPrimitive("name").asString
44 val desc = getAsJsonPrimitive("desc").asString
45 val declaration = getAsJsonPrimitive("declaration")?.asString
46 val visibility = getAsJsonPrimitive("visibility")?.asString
47 MemberVisibility(MemberSignature(name, desc), declaration, visibility)
48 }
49 }
50 ClassVisibility(name, visibility, members.associateByTo(hashMapOf()) { it.member })
51 }
52 }
53 jsonReader.endArray()
54 }
55
56 return result.associateByTo(hashMapOf()) { it.name }
57 }
58