1+ /*
2+ * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
3+ * that can be found in the license/LICENSE.txt file.
4+ */
5+
16package org.jetbrains.kotlin.tools
27
38import kotlinx.metadata.jvm.KotlinClassMetadata
@@ -6,43 +11,41 @@ import org.objectweb.asm.tree.*
611import kotlin.comparisons.*
712
813val ACCESS_NAMES = mapOf (
9- Opcodes .ACC_PUBLIC to " public" ,
10- Opcodes .ACC_PROTECTED to " protected" ,
11- Opcodes .ACC_PRIVATE to " private" ,
12- Opcodes .ACC_STATIC to " static" ,
13- Opcodes .ACC_FINAL to " final" ,
14- Opcodes .ACC_ABSTRACT to " abstract" ,
15- Opcodes .ACC_SYNTHETIC to " synthetic" ,
16- Opcodes .ACC_INTERFACE to " interface" ,
17- Opcodes .ACC_ANNOTATION to " annotation" )
14+ Opcodes .ACC_PUBLIC to " public" ,
15+ Opcodes .ACC_PROTECTED to " protected" ,
16+ Opcodes .ACC_PRIVATE to " private" ,
17+ Opcodes .ACC_STATIC to " static" ,
18+ Opcodes .ACC_FINAL to " final" ,
19+ Opcodes .ACC_ABSTRACT to " abstract" ,
20+ Opcodes .ACC_SYNTHETIC to " synthetic" ,
21+ Opcodes .ACC_INTERFACE to " interface" ,
22+ Opcodes .ACC_ANNOTATION to " annotation"
23+ )
1824
1925data class ClassBinarySignature (
20- val name : String ,
21- val superName : String ,
22- val outerName : String? ,
23- val supertypes : List <String >,
24- val memberSignatures : List <MemberBinarySignature >,
25- val access : AccessFlags ,
26- val isEffectivelyPublic : Boolean ,
27- val isNotUsedWhenEmpty : Boolean ) {
28-
26+ val name : String ,
27+ val superName : String ,
28+ val outerName : String? ,
29+ val supertypes : List <String >,
30+ val memberSignatures : List <MemberBinarySignature >,
31+ val access : AccessFlags ,
32+ val isEffectivelyPublic : Boolean ,
33+ val isNotUsedWhenEmpty : Boolean
34+ ) {
2935 val signature: String
3036 get() = " ${access.getModifierString()} class $name " + if (supertypes.isEmpty()) " " else " : ${supertypes.joinToString()} "
31-
3237}
3338
34- fun ClassVisibility.findMember (signature : MemberSignature ): MemberVisibility ? =
35- members[signature] ? : partVisibilities.mapNotNull { it.members[signature] }.firstOrNull()
3639
3740interface MemberBinarySignature {
3841 val name: String
3942 val desc: String
4043 val access: AccessFlags
4144 val isPublishedApi: Boolean
4245
43- fun isEffectivelyPublic (classAccess : AccessFlags , classVisibility : ClassVisibility ? )
44- = access.isPublic && ! (access.isProtected && classAccess.isFinal)
45- && (findMemberVisibility(classVisibility)?.isPublic(isPublishedApi) ? : true )
46+ fun isEffectivelyPublic (classAccess : AccessFlags , classVisibility : ClassVisibility ? ) =
47+ access.isPublic && ! (access.isProtected && classAccess.isFinal)
48+ && (findMemberVisibility(classVisibility)?.isPublic(isPublishedApi) ? : true )
4649
4750 fun findMemberVisibility (classVisibility : ClassVisibility ? ): MemberVisibility ? {
4851 return classVisibility?.findMember(MemberSignature (name, desc))
@@ -52,10 +55,11 @@ interface MemberBinarySignature {
5255}
5356
5457data class MethodBinarySignature (
55- override val name : String ,
56- override val desc : String ,
57- override val isPublishedApi : Boolean ,
58- override val access : AccessFlags ) : MemberBinarySignature {
58+ override val name : String ,
59+ override val desc : String ,
60+ override val isPublishedApi : Boolean ,
61+ override val access : AccessFlags
62+ ) : MemberBinarySignature {
5963 override val signature: String
6064 get() = " ${access.getModifierString()} fun $name $desc "
6165
@@ -92,35 +96,31 @@ data class MethodBinarySignature(
9296}
9397
9498data class FieldBinarySignature (
95- override val name : String ,
96- override val desc : String ,
97- override val isPublishedApi : Boolean ,
98- override val access : AccessFlags ) : MemberBinarySignature {
99+ override val name : String ,
100+ override val desc : String ,
101+ override val isPublishedApi : Boolean ,
102+ override val access : AccessFlags
103+ ) : MemberBinarySignature {
99104 override val signature: String
100105 get() = " ${access.getModifierString()} field $name $desc "
101106
102107 override fun findMemberVisibility (classVisibility : ClassVisibility ? ): MemberVisibility ? {
103- val fieldVisibility = super .findMemberVisibility(classVisibility)
108+ return super .findMemberVisibility(classVisibility)
104109 ? : takeIf { access.isStatic }?.let { super .findMemberVisibility(classVisibility?.companionVisibilities) }
105- ? : return null
106-
107- if (fieldVisibility.isLateInit()) {
108- classVisibility?.findSetterForProperty(fieldVisibility)?.let { return it }
109- }
110- return fieldVisibility
111110 }
112111}
113112
114- val MemberBinarySignature .kind: Int get() = when (this ) {
115- is FieldBinarySignature -> 1
116- is MethodBinarySignature -> 2
117- else -> error(" Unsupported $this " )
118- }
113+ private val MemberBinarySignature .kind: Int
114+ get() = when (this ) {
115+ is FieldBinarySignature -> 1
116+ is MethodBinarySignature -> 2
117+ else -> error(" Unsupported $this " )
118+ }
119119
120120val MEMBER_SORT_ORDER = compareBy<MemberBinarySignature >(
121- { it.kind },
122- { it.name },
123- { it.desc }
121+ { it.kind },
122+ { it.name },
123+ { it.desc }
124124)
125125
126126
@@ -143,10 +143,10 @@ fun isSynthetic(access: Int) = access and Opcodes.ACC_SYNTHETIC != 0
143143
144144
145145fun ClassNode.isEffectivelyPublic (classVisibility : ClassVisibility ? ) =
146- isPublic(access)
147- && ! isLocal()
148- && ! isWhenMappings()
149- && (classVisibility?.isPublic(isPublishedApi()) ? : true )
146+ isPublic(access)
147+ && ! isLocal()
148+ && ! isWhenMappings()
149+ && (classVisibility?.isPublic(isPublishedApi()) ? : true )
150150
151151
152152val ClassNode .innerClassNode: InnerClassNode ? get() = innerClasses.singleOrNull { it.name == name }
@@ -164,37 +164,25 @@ fun MethodNode.isPublishedApi() = findAnnotation(publishedApiAnnotationName, inc
164164fun FieldNode.isPublishedApi () = findAnnotation(publishedApiAnnotationName, includeInvisible = true ) != null
165165
166166
167- private object KotlinClassKind {
168- const val FILE = 2
169- const val SYNTHETIC_CLASS = 3
170- const val MULTIPART_FACADE = 4
171-
172- val FILE_OR_MULTIPART_FACADE_KINDS = listOf (FILE , MULTIPART_FACADE )
173- }
174-
175- fun ClassNode.isFileOrMultipartFacade () = kotlinClassKind.let { it != null && it in KotlinClassKind .FILE_OR_MULTIPART_FACADE_KINDS }
176167fun ClassNode.isDefaultImpls (metadata : KotlinClassMetadata ? ) = isInner() && name.endsWith(" \$ DefaultImpls" ) && metadata.isSyntheticClass()
177168
178169
179- val ClassNode .kotlinClassKind: Int?
180- get() = findAnnotation(" kotlin/Metadata" , false )?.get(" k" ) as Int?
181-
182170fun ClassNode.findAnnotation (annotationName : String , includeInvisible : Boolean = false) = findAnnotation(annotationName, visibleAnnotations, invisibleAnnotations, includeInvisible)
183171fun MethodNode.findAnnotation (annotationName : String , includeInvisible : Boolean = false) = findAnnotation(annotationName, visibleAnnotations, invisibleAnnotations, includeInvisible)
184172fun FieldNode.findAnnotation (annotationName : String , includeInvisible : Boolean = false) = findAnnotation(annotationName, visibleAnnotations, invisibleAnnotations, includeInvisible)
185173
186174operator fun AnnotationNode.get (key : String ): Any? = values.annotationValue(key)
187175
188176private fun List<Any>.annotationValue (key : String ): Any? {
189- for (index in (0 .. size / 2 - 1 )) {
190- if (this [index* 2 ] == key)
191- return this [index* 2 + 1 ]
177+ for (index in (0 until size / 2 )) {
178+ if (this [index * 2 ] == key)
179+ return this [index * 2 + 1 ]
192180 }
193181 return null
194182}
195183
196184private fun findAnnotation (annotationName : String , visibleAnnotations : List <AnnotationNode >? , invisibleAnnotations : List <AnnotationNode >? , includeInvisible : Boolean ): AnnotationNode ? =
197- visibleAnnotations?.firstOrNull { it.refersToName(annotationName) } ? :
198- if (includeInvisible) invisibleAnnotations?.firstOrNull { it.refersToName(annotationName) } else null
185+ visibleAnnotations?.firstOrNull { it.refersToName(annotationName) }
186+ ? : if (includeInvisible) invisibleAnnotations?.firstOrNull { it.refersToName(annotationName) } else null
199187
200188fun AnnotationNode.refersToName (name : String ) = desc.startsWith(' L' ) && desc.endsWith(' ;' ) && desc.regionMatches(1 , name, 0 , name.length)
0 commit comments