Skip to content

Commit

Permalink
Force resolve outermost class for Java nested classes.
Browse files Browse the repository at this point in the history
This is a temporary workaround for #1034

(cherry picked from commit 5f5977e)
  • Loading branch information
neetopia authored and KSP Auto Pick committed Jul 29, 2022
1 parent 31ae1b7 commit 2e00149
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 0 deletions.
Expand Up @@ -64,6 +64,9 @@ import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaTypeParameterDesc
import org.jetbrains.kotlin.load.java.lazy.types.JavaTypeResolver
import org.jetbrains.kotlin.load.java.lazy.types.toAttributes
import org.jetbrains.kotlin.load.java.sources.JavaSourceElement
import org.jetbrains.kotlin.load.java.structure.JavaClass
import org.jetbrains.kotlin.load.java.structure.JavaClassifierType
import org.jetbrains.kotlin.load.java.structure.classId
import org.jetbrains.kotlin.load.java.structure.impl.JavaArrayTypeImpl
import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl
import org.jetbrains.kotlin.load.java.structure.impl.JavaConstructorImpl
Expand Down Expand Up @@ -624,6 +627,17 @@ class ResolverImpl(
}
// Construct resolver context for the PsiType
var resolverContext = lazyJavaResolverContext
// TODO: fix in compiler.
// Temporary work around for https://github.com/google/ksp/issues/1034
// Force resolve outer most class for Java nested classes.
javaType.safeAs<JavaClassifierType>()?.classifier.safeAs<JavaClass>()?.let {
var outerMost = it.outerClass
while (outerMost?.outerClass != null) {
outerMost = outerMost.outerClass
}
outerMost?.classId?.let { lazyJavaResolverContext.components.finder.findClass(it) }
}

for (e in stack) {
when (e) {
is KSFunctionDeclarationJavaImpl -> {
Expand Down
@@ -0,0 +1,22 @@
package com.google.devtools.ksp.test

import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Assert
import org.junit.Rule
import org.junit.Test

class JavaNestedClassIT {
@Rule
@JvmField
val project: TemporaryTestProject = TemporaryTestProject("javaNestedClass")

@Test
fun testJavaNestedClass() {

val gradleRunner = GradleRunner.create().withProjectDir(project.root)

val resultCleanBuild = gradleRunner.withArguments("clean", "build").build()
Assert.assertEquals(TaskOutcome.SUCCESS, resultCleanBuild.task(":workload:build")?.outcome)
}
}
@@ -0,0 +1,8 @@
plugins {
kotlin("jvm")
}

repositories {
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
}
@@ -0,0 +1,19 @@
pluginManagement {
val kotlinVersion: String by settings
val kspVersion: String by settings
val testRepo: String by settings
plugins {
id("com.google.devtools.ksp") version kspVersion
kotlin("jvm") version kotlinVersion
}
repositories {
maven(testRepo)
gradlePluginPortal()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
}
}

rootProject.name = "javaNestedClass"

include(":workload")
include(":test-processor")
@@ -0,0 +1,25 @@
val kspVersion: String by project
val testRepo: String by project

plugins {
kotlin("jvm")
}

group = "com.example"
version = "1.0-SNAPSHOT"

repositories {
maven(testRepo)
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
}

dependencies {
implementation(kotlin("stdlib"))
implementation("com.squareup:javapoet:1.12.1")
implementation("com.google.devtools.ksp:symbol-processing-api:$kspVersion")
}

sourceSets.main {
java.srcDirs("src/main/kotlin")
}
@@ -0,0 +1,24 @@
import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.validate

class ValidateProcessor(env: SymbolProcessorEnvironment) : SymbolProcessor {
var logger: KSPLogger = env.logger

override fun process(resolver: Resolver): List<KSAnnotated> {
val javaClass = resolver.getClassDeclarationByName("com.example.JavaClass")!!
if (!javaClass.validate()) {
logger.error("Failed to validate $javaClass")
}
return emptyList()
}
}

class ValidateProcessorProvider : SymbolProcessorProvider {
override fun create(
env: SymbolProcessorEnvironment
): SymbolProcessor {
return ValidateProcessor(env)
}
}
@@ -0,0 +1 @@
ValidateProcessorProvider
@@ -0,0 +1,20 @@
val testRepo: String by project

plugins {
id("com.google.devtools.ksp")
kotlin("jvm")
}

version = "1.0-SNAPSHOT"

repositories {
maven(testRepo)
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
}

dependencies {
implementation(kotlin("stdlib"))
implementation(project(":test-processor"))
ksp(project(":test-processor"))
}
@@ -0,0 +1,4 @@
package com.example

fun main() {
}
@@ -0,0 +1,12 @@
package com.example;

public class JavaClass {

public int b2;

public ENUM e;

public enum ENUM {
R,G,B
}
}

0 comments on commit 2e00149

Please sign in to comment.