[FE] Properly parse java class name from sources if java class has annotations

^KT-56847 Fixed
This commit is contained in:
Dmitriy Novozhilov
2023-02-22 18:02:35 +02:00
committed by Space Team
parent 5455942859
commit 331cc1465a
6 changed files with 56 additions and 2 deletions
@@ -1386,6 +1386,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/annotations/invalidTypesInAnnotationConstructor.kt");
}
@Test
@TestMetadata("javaAnnotationAndJavaClassWithIt.kt")
public void testJavaAnnotationAndJavaClassWithIt() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/javaAnnotationAndJavaClassWithIt.kt");
}
@Test
@TestMetadata("JavaAnnotationConstructors.kt")
public void testJavaAnnotationConstructors() throws Exception {
@@ -58,6 +58,7 @@ class SingleJavaFileRootsIndex(private val roots: List<JavaRoot>) {
start(String(file.contentsToByteArray()))
}
private var braceBalance = 0
private var parenthesisBalance = 0
private fun at(type: IElementType): Boolean = lexer.tokenType == type
@@ -67,6 +68,8 @@ class SingleJavaFileRootsIndex(private val roots: List<JavaRoot>) {
when {
at(ElementType.LBRACE) -> braceBalance++
at(ElementType.RBRACE) -> braceBalance--
at(ElementType.LPARENTH) -> parenthesisBalance++
at(ElementType.RPARENTH) -> parenthesisBalance--
}
lexer.advance()
}
@@ -74,7 +77,7 @@ class SingleJavaFileRootsIndex(private val roots: List<JavaRoot>) {
private fun tokenText(): String = lexer.tokenText
private fun atClass(): Boolean =
braceBalance == 0 && (lexer.tokenType in CLASS_KEYWORDS || atRecord())
braceBalance == 0 && parenthesisBalance == 0 && (lexer.tokenType in CLASS_KEYWORDS || atRecord())
private fun atRecord(): Boolean {
// Note that the soft keyword "record" is lexed as IDENTIFIER instead of RECORD_KEYWORD.
@@ -112,7 +115,6 @@ class SingleJavaFileRootsIndex(private val roots: List<JavaRoot>) {
if (end()) break
result.add(ClassId(packageFqName, Name.identifier(tokenText())))
}
return result
}
@@ -1392,6 +1392,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/tests/annotations/invalidTypesInAnnotationConstructor.kt");
}
@Test
@TestMetadata("javaAnnotationAndJavaClassWithIt.kt")
public void testJavaAnnotationAndJavaClassWithIt() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/javaAnnotationAndJavaClassWithIt.kt");
}
@Test
@TestMetadata("JavaAnnotationConstructors.kt")
public void testJavaAnnotationConstructors() throws Exception {
@@ -1386,6 +1386,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
runTest("compiler/testData/diagnostics/tests/annotations/invalidTypesInAnnotationConstructor.kt");
}
@Test
@TestMetadata("javaAnnotationAndJavaClassWithIt.kt")
public void testJavaAnnotationAndJavaClassWithIt() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/javaAnnotationAndJavaClassWithIt.kt");
}
@Test
@TestMetadata("JavaAnnotationConstructors.kt")
public void testJavaAnnotationConstructors() throws Exception {
@@ -0,0 +1,28 @@
// FIR_IDENTICAL
// ISSUE: KT-56847
// FILE: foo/TestTarget.java
package foo;
@AnnotationWithArg(String.class)
@Ann
public final class TestTarget {}
// FILE: foo/Ann.java
package foo;
public @interface Ann {}
// FILE: foo/AnnotationWithArg.java
package foo;
public @interface AnnotationWithArg {
Class<?> value();
}
// FILE: foo/AnotherTarget.kt
package foo
@Ann
class AnotherTarget {
fun hello() {}
}
@@ -1392,6 +1392,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/annotations/invalidTypesInAnnotationConstructor.kt");
}
@Test
@TestMetadata("javaAnnotationAndJavaClassWithIt.kt")
public void testJavaAnnotationAndJavaClassWithIt() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/javaAnnotationAndJavaClassWithIt.kt");
}
@Test
@TestMetadata("JavaAnnotationConstructors.kt")
public void testJavaAnnotationConstructors() throws Exception {