Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private fun resolveJavaTypeInfo(javaFile: PsiJavaFile, fallbackName: String): Ja
val text = javaFile.text
return JavaTypeInfo(
className = cls.name ?: fallbackName,
annotations = cls.annotations.mapNotNull { it.qualifiedName?.substringAfterLast('.') },
annotations = cls.annotations.mapNotNull { it.nameReferenceElement?.referenceName },
supertypes = superList,
isInterface = cls.isInterface,
isAbstract = cls.hasModifierProperty(PsiModifier.ABSTRACT),
Expand All @@ -237,38 +237,46 @@ private fun resolveJavaTypeInfo(javaFile: PsiJavaFile, fallbackName: String): Ja
)
}

private val javaParseLogger =
org.gradle.api.logging.Logging
.getLogger("zone.clanker.gradle.srcx.analysis.SourceFileMetadata")

private fun parseJavaFile(file: File, psiManager: PsiManager): SourceFileMetadata? {
val vf = LightVirtualFile(file.name, JavaFileType.INSTANCE, file.readText())
val javaFile = psiManager.findFile(vf) as? PsiJavaFile ?: return null

val packageName = javaFile.packageName
val imports =
javaFile.importList
?.importStatements
?.mapNotNull { it.qualifiedName }
?.filter { fqn -> PLATFORM_PREFIXES.none { fqn.startsWith(it) } }
?: emptyList()
return runCatching {
val packageName = javaFile.packageName
val imports =
javaFile.importList
?.importStatements
?.mapNotNull { it.qualifiedName }
?.filter { fqn -> PLATFORM_PREFIXES.none { fqn.startsWith(it) } }
?: emptyList()

val info = resolveJavaTypeInfo(javaFile, file.nameWithoutExtension)
val qualifiedName = if (packageName.isNotEmpty()) "$packageName.${info.className}" else info.className
val info = resolveJavaTypeInfo(javaFile, file.nameWithoutExtension)
val qualifiedName = if (packageName.isNotEmpty()) "$packageName.${info.className}" else info.className

return SourceFileMetadata(
file = file,
packageName = packageName,
qualifiedName = qualifiedName,
simpleName = info.className,
imports = imports,
annotations = info.annotations,
supertypes = info.supertypes,
isInterface = info.isInterface,
isAbstract = info.isAbstract,
isObject = false,
isDataClass = false,
language = SourceFileMetadata.Language.JAVA,
lineCount = countCodeLines(file.readLines()),
methods = info.methods,
declarationLine = info.declarationLine,
)
SourceFileMetadata(
file = file,
packageName = packageName,
qualifiedName = qualifiedName,
simpleName = info.className,
imports = imports,
annotations = info.annotations,
supertypes = info.supertypes,
isInterface = info.isInterface,
isAbstract = info.isAbstract,
isObject = false,
isDataClass = false,
language = SourceFileMetadata.Language.JAVA,
lineCount = countCodeLines(file.readLines()),
methods = info.methods,
declarationLine = info.declarationLine,
)
}.onFailure { e ->
javaParseLogger.warn("srcx: Failed to parse Java file '${file.name}': ${e.message}", e)
}.getOrNull()
Comment on lines 244 to +279
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# First, let's locate and inspect the file
cd /tmp/repo 2>/dev/null || cd / || true
find . -name "SourceFileMetadata.kt" -type f 2>/dev/null | head -5

Repository: ClankerGuru/srcx

Length of output: 151


🏁 Script executed:

# Let's check the actual code at the specified lines
if [ -f "src/main/kotlin/zone/clanker/gradle/srcx/analysis/SourceFileMetadata.kt" ]; then
  wc -l "src/main/kotlin/zone/clanker/gradle/srcx/analysis/SourceFileMetadata.kt"
  sed -n '244,279p' "src/main/kotlin/zone/clanker/gradle/srcx/analysis/SourceFileMetadata.kt"
fi

Repository: ClankerGuru/srcx

Length of output: 1681


🌐 Web query:

Kotlin stdlib runCatching catches Throwable or Exception documentation

💡 Result:

Kotlin stdlib's runCatching catches Throwable, not just Exception. It calls the specified function block and returns its encapsulated result if successful, catching any Throwable thrown from the block and encapsulating it as a Result.Failure. This has been the case since Kotlin 1.3 and remains current in the latest documentation.

Citations:


🏁 Script executed:

# Check for similar parsing patterns and usage context
rg "parseJavaFile|parseKotlinFile|runCatching" src/main/kotlin/zone/clanker/gradle/srcx/analysis/ -B 2 -A 2

Repository: ClankerGuru/srcx

Length of output: 2957


🏁 Script executed:

# Check if there's a corresponding parseKotlinFile to understand the pattern
grep -n "fun parse" src/main/kotlin/zone/clanker/gradle/srcx/analysis/SourceFileMetadata.kt | head -10

Repository: ClankerGuru/srcx

Length of output: 380


Move file setup inside the try block and use catch (Exception) instead of runCatching.

The file.readText() and psiManager.findFile() calls at lines 245–246 are outside the error handler, so I/O or charset failures will crash the analysis before the fallback runs. Additionally, runCatching catches Throwable, which suppresses fatal JVM errors (OutOfMemoryError, StackOverflowError) that should never be caught.

Proposed fix
 private fun parseJavaFile(file: File, psiManager: PsiManager): SourceFileMetadata? {
-    val vf = LightVirtualFile(file.name, JavaFileType.INSTANCE, file.readText())
-    val javaFile = psiManager.findFile(vf) as? PsiJavaFile ?: return null
-
-    return runCatching {
+    return try {
+        val vf = LightVirtualFile(file.name, JavaFileType.INSTANCE, file.readText())
+        val javaFile = psiManager.findFile(vf) as? PsiJavaFile ?: return null
-    }.onFailure { e ->
-        javaParseLogger.warn("srcx: Failed to parse Java file '${file.name}': ${e.message}", e)
-    }.getOrNull()
+    } catch (e: Exception) {
+        javaParseLogger.warn("srcx: Failed to parse Java file '${file.path}': ${e.message}", e)
+        null
+    }
 }

Apply the same fix to parseKotlinFile (line 158).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/kotlin/zone/clanker/gradle/srcx/analysis/SourceFileMetadata.kt`
around lines 244 - 279, Move the LightVirtualFile creation and
psiManager.findFile(...) calls inside a try block within parseJavaFile and
replace the runCatching usage with an explicit try { ... } catch (e: Exception)
{ ... } so I/O or charset errors are handled by the fallback and fatal
Throwables are not swallowed; apply the same change to parseKotlinFile (ensure
you catch Exception, log via javaParseLogger.warn with the exception, and return
null on failure).

}

private fun countCodeLines(lines: List<String>): Int {
Expand Down
Loading