fix(lsp/java): avoid crash during Java source code analysis

This issue was reported in #1279 (https://github.com/AndroidIDEOfficial/AndroidIDE/issues/1279\#issuecomment-1826706454).
This commit is contained in:
Akash Yadav
2023-12-02 23:52:26 +05:30
parent 5d1faba441
commit 328ff93b1a
3 changed files with 17 additions and 5 deletions
+1 -1
View File
@@ -18,7 +18,7 @@
<entry name="$USER_HOME$/.gradle/caches/modules-2/files-2.1/com.google.errorprone/error_prone_annotations/2.18.0/89b684257096f548fa39a7df9fdaa409d4d4df91/error_prone_annotations-2.18.0.jar" />
<entry name="$USER_HOME$/.gradle/caches/modules-2/files-2.1/com.google.j2objc/j2objc-annotations/2.8/c85270e307e7b822f1086b93689124b89768e273/j2objc-annotations-2.8.jar" />
</processorPath>
<module name="ide.AndroidIDE.annotation-processors.main" />
<module name="AndroidIDE.annotation-processors.main" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel target="1.8">
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinJpsPluginSettings">
<option name="version" value="1.9.10" />
<option name="version" value="1.9.21" />
</component>
</project>
@@ -43,6 +43,7 @@ import java.util.regex.Pattern
import jdkx.lang.model.element.Element
import jdkx.tools.Diagnostic
import jdkx.tools.JavaFileObject
import java.lang.IndexOutOfBoundsException
/**
* Finds errors and warnings from a compilation task.
@@ -98,7 +99,7 @@ object DiagnosticsProvider {
val scanner = DiagnosticVisitor(task.task)
scanner.scan(root, notThrown)
for (unusedEl in scanner.notUsed()) {
result.add(warnUnused(task, unusedEl))
warnUnused(task, unusedEl)?.also { result.add(it) }
}
for (location in notThrown.keys) {
@@ -169,7 +170,7 @@ object DiagnosticsProvider {
)
}
private fun warnUnused(task: CompileTask, unusedEl: Element): DiagnosticItem {
private fun warnUnused(task: CompileTask, unusedEl: Element): DiagnosticItem? {
val trees = Trees.instance(task.task)
val path = trees.getPath(unusedEl) ?: throw RuntimeException("$unusedEl has no path")
val root = path.compilationUnit
@@ -177,24 +178,34 @@ object DiagnosticsProvider {
val pos = trees.sourcePositions
var start = pos.getStartPosition(root, leaf).toInt()
var end = pos.getEndPosition(root, leaf).toInt()
if (leaf is VariableTree) {
val offset = pos.getEndPosition(root, leaf.type).toInt()
if (offset != -1) {
start = offset
}
}
val file = Paths.get(root.sourceFile.toUri())
val contents = FileManager.getDocumentContents(file)
var name = unusedEl.simpleName
if (name.contentEquals("<init>")) {
name = unusedEl.enclosingElement.simpleName
}
val region = contents.subSequence(start, end)
val region = try {
contents.subSequence(start, end)
} catch (err: IndexOutOfBoundsException) {
// might happen if the file contents were changed after the file was compiled for analysis
return null
}
val matcher = Pattern.compile("\\b$name\\b").matcher(region)
if (matcher.find()) {
start += matcher.start()
end = start + name.length
}
val message = String.format("'%s' is not used", name)
val code: DiagnosticCode
val severity: DiagnosticSeverity
@@ -232,6 +243,7 @@ object DiagnosticsProvider {
severity = DiagnosticSeverity.INFO
}
}
return asDiagnosticItem(severity, code.id, message, start.toLong(), end.toLong(), root)
}