Skip to content

Commit e13fc40

Browse files
committed
fix(java): resolve issue with resolving classes in JavaTypeUtil
The `resolveByType` function in `JavaTypeUtil.kt` was not correctly resolving classes when the `outputType` is a `PsiClassReferenceType`. This was due to a bug where the `outputType.resolve()` was being called multiple times. This commit fixes the issue by storing the resolved class in a variable and using it for comparison and assignment. Additionally, the error message in `JavaWriteTestService.kt` was not correctly interpolating the `parentDirPath` variable. This commit fixes the issue by using string interpolation to correctly display the error message.
1 parent ea37212 commit e13fc40

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

java/src/main/kotlin/cc/unitmesh/idea/service/JavaTypeUtil.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ object JavaTypeUtil {
99
private fun resolveByType(outputType: PsiType?): Map<String, PsiClass> {
1010
val resolvedClasses = mutableMapOf<String, PsiClass>()
1111
if (outputType is PsiClassReferenceType) {
12-
if (outputType.parameters.isNotEmpty()) {
13-
outputType.parameters.forEach {
14-
if (it is PsiClassReferenceType && outputType.resolve() != null) {
15-
resolvedClasses[it.canonicalText] = outputType.resolve()!!
16-
}
12+
val resolveClz = outputType.resolve()
13+
outputType.parameters.filterIsInstance<PsiClassReferenceType>().forEach {
14+
if (resolveClz != null) {
15+
resolvedClasses[it.canonicalText] = resolveClz
1716
}
1817
}
1918

2019
val canonicalText = outputType.canonicalText
21-
if (outputType.resolve() != null) {
22-
resolvedClasses[canonicalText] = outputType.resolve()!!
20+
if (resolveClz != null) {
21+
resolvedClasses[canonicalText] = resolveClz
2322
}
2423
}
2524

java/src/main/kotlin/cc/unitmesh/idea/service/JavaWriteTestService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ class JavaWriteTestService : WriteTestService() {
4646

4747
// Check if the source file is in the src/main/java directory
4848
if (!parentDirPath?.contains("/src/main/java/")!!) {
49-
log.error("Source file is not in the src/main/java directory: ${parentDirPath}")
49+
log.error("Source file is not in the src/main/java directory: $parentDirPath")
5050
return null
5151
}
5252

5353
var isNewFile = false
5454

5555
// Find the test directory
56-
val testDirPath = parentDir.path.replace("/src/main/java/", "/src/test/java/")
56+
val testDirPath = parentDirPath.replace("/src/main/java/", "/src/test/java/")
5757
var testDir = LocalFileSystem.getInstance().findFileByPath(testDirPath)
5858

5959
if (testDir == null || !testDir.isDirectory) {

0 commit comments

Comments
 (0)