Skip to content

Commit

Permalink
KAPT: Use reflection to access code, changed in JDK 21
Browse files Browse the repository at this point in the history
 #KT-57389 Fixed
  • Loading branch information
ilmirus committed Nov 22, 2023
1 parent e459a6d commit 8293c8f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.jetbrains.kotlin.kapt3.base.javac

import com.sun.tools.javac.tree.JCTree
import com.sun.tools.javac.tree.JCTree.JCImport
import com.sun.tools.javac.util.*
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType
import org.jetbrains.kotlin.kapt3.base.KaptContext
Expand Down Expand Up @@ -55,6 +56,8 @@ class KaptJavaLog(

private val _reportedDiagnostics = mutableListOf<JCDiagnostic>()

private val jcImportQualidField = JCImport::class.java.declaredFields.single { it.name == "qualid" }

override fun flush(kind: WriterKind?) {
super.flush(kind)

Expand Down Expand Up @@ -197,7 +200,7 @@ class KaptJavaLog(
val visitor = object : JCTree.Visitor() {
override fun visitImport(that: JCTree.JCImport) {
super.visitImport(that)
if (!found) that.qualid.accept(this)
if (!found) (jcImportQualidField.get(that) as JCTree).accept(this)
}

override fun visitSelect(that: JCTree.JCFieldAccess) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati

private var done = false

private val treeMakerImportMethod = TreeMaker::class.java.declaredMethods.single { it.name == "Import" }

fun convert(): List<KaptStub> {
if (kaptContext.logger.isVerbose) {
dumpDeclarationOrigins()
Expand Down Expand Up @@ -315,13 +317,15 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
val importedExpr = treeMaker.FqName(importedFqName.asString())

imports += if (importDirective.isAllUnder) {
treeMaker.Import(treeMaker.Select(importedExpr, treeMaker.nameTable.names.asterisk), false)
treeMakerImportMethod.invoke(
treeMaker, treeMaker.Select(importedExpr, treeMaker.nameTable.names.asterisk), false
) as JCImport
} else {
if (!importedShortNames.add(importedFqName.shortName().asString())) {
continue
}

treeMaker.Import(importedExpr, false)
treeMakerImportMethod.invoke(treeMaker, importedExpr, false) as JCImport
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.sun.tools.javac.code.TypeTag
import com.sun.tools.javac.parser.Tokens
import com.sun.tools.javac.tree.JCTree
import com.sun.tools.javac.tree.JCTree.*
import com.sun.tools.javac.tree.TreeMaker
import kotlinx.kapt.KaptIgnored
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol
Expand Down Expand Up @@ -84,6 +85,8 @@ internal class Kapt4StubGenerator {
private val JAVA_KEYWORDS = Tokens.TokenKind.values()
.filter { JAVA_KEYWORD_FILTER_REGEX.matches(it.toString().orEmpty()) }
.mapTo(hashSetOf(), Any::toString)

private val treeMakerImportMethod = TreeMaker::class.java.declaredMethods.single { it.name == "Import" }
}

private val strictMode = options[KaptFlag.STRICT]
Expand Down Expand Up @@ -260,13 +263,15 @@ internal class Kapt4StubGenerator {
if (!isValidQualifiedName(importedFqName)) continue
val importedExpr = treeMaker.FqName(importedFqName.asString())
imports += if (importDirective.isAllUnder) {
treeMaker.Import(treeMaker.Select(importedExpr, treeMaker.nameTable.names.asterisk), false)
treeMakerImportMethod.invoke(
treeMaker, treeMaker.Select(importedExpr, treeMaker.nameTable.names.asterisk), false
) as JCImport
} else {
if (!importedShortNames.add(importedFqName.shortName().asString())) {
continue
}

treeMaker.Import(importedExpr, false)
treeMakerImportMethod.invoke(treeMaker, importedExpr, false) as JCImport
}
}

Expand Down

0 comments on commit 8293c8f

Please sign in to comment.