Skip to content

Commit

Permalink
fix visibility check for public override
Browse files Browse the repository at this point in the history
(cherry picked from commit dac1adc)
  • Loading branch information
neetopia authored and KSP Auto Pick committed Sep 1, 2021
1 parent df04f42 commit b612477
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/src/main/kotlin/com/google/devtools/ksp/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ fun KSTypeAlias.findActualType(): KSClassDeclaration {
*/
fun KSDeclaration.getVisibility(): Visibility {
return when {
this.modifiers.contains(Modifier.PUBLIC) -> Visibility.PUBLIC
this.modifiers.contains(Modifier.OVERRIDE) -> {
when (this) {
is KSFunctionDeclaration -> this.findOverridee()?.getVisibility()
Expand All @@ -149,7 +150,6 @@ fun KSDeclaration.getVisibility(): Visibility {
this.modifiers.contains(Modifier.PROTECTED) ||
this.modifiers.contains(Modifier.OVERRIDE) -> Visibility.PROTECTED
this.modifiers.contains(Modifier.INTERNAL) -> Visibility.INTERNAL
this.modifiers.contains(Modifier.PUBLIC) -> Visibility.PUBLIC
else -> if (this.origin != Origin.JAVA && this.origin != Origin.JAVA_LIB)
Visibility.PUBLIC else Visibility.JAVA_PACKAGE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class VisibilityProcessor : AbstractTestProcessor() {
}.forEach { results.add(it) }
val javaClass = resolver.getClassDeclarationByName("JavaClass")!!
val kotlinClass = resolver.getClassDeclarationByName("KotlinClass")!!
val kotlinSubClass = resolver.getClassDeclarationByName("KotlinSubClass")!!
javaClass.declarations.filterIsInstance<KSPropertyDeclaration>().map {
"${it.simpleName.asString()}: ${it.getVisibility()},visible in A, B, D: " +
"${it.isVisibleFrom(symbolA)}, ${it.isVisibleFrom(symbolB)}, ${it.isVisibleFrom(symbolD)}"
Expand All @@ -53,6 +54,10 @@ class VisibilityProcessor : AbstractTestProcessor() {
"${it.simpleName.asString()}: ${it.getVisibility()},visible in A, B, D: " +
"${it.isVisibleFrom(symbolA)}, ${it.isVisibleFrom(symbolB)}, ${it.isVisibleFrom(symbolD)}"
}.forEach { results.add(it) }
kotlinSubClass.declarations.filterIsInstance<KSPropertyDeclaration>().map {
"${it.simpleName.asString()}: ${it.getVisibility()},visible in A, B, D: " +
"${it.isVisibleFrom(symbolA)}, ${it.isVisibleFrom(symbolB)}, ${it.isVisibleFrom(symbolD)}"
}.forEach { results.add(it) }
return emptyList()
}
}
10 changes: 8 additions & 2 deletions compiler-plugin/testData/api/visibilities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// <init>: PUBLIC,visible in A, B, D: true, true, true
// javaPackageField: JAVA_PACKAGE,visible in A, B, D: true, false, true
// x: INTERNAL,visible in A, B, D: false, false, false
// y: PROTECTED,visible in A, B, D: true, false, true
// y: PUBLIC,visible in A, B, D: true, true, true
// END

// MODULE: lib
Expand All @@ -33,8 +35,9 @@ public class JavaClass {
}

// FILE: lib.kt
class KotlinClass {
internal val x: Int = 0
open class KotlinClass {
open internal val x: Int = 0
open protected val y: Int = 0
}

// MODULE: main(lib)
Expand All @@ -50,6 +53,9 @@ class A : C() {
@TestD
class D {}

class KotlinSubClass : KotlinClass() {
public override val y = 1
}

// FILE: C.java
class C {
Expand Down

0 comments on commit b612477

Please sign in to comment.