Convert KotlinExceptionFilter to kotlin
This commit is contained in:
@@ -14,100 +14,75 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.idea.filters;
|
package org.jetbrains.kotlin.idea.filters
|
||||||
|
|
||||||
import com.intellij.execution.filters.ExceptionFilter;
|
import com.intellij.execution.filters.ExceptionFilter
|
||||||
import com.intellij.execution.filters.Filter;
|
import com.intellij.execution.filters.Filter
|
||||||
import com.intellij.execution.filters.HyperlinkInfo;
|
import com.intellij.execution.filters.HyperlinkInfo
|
||||||
import com.intellij.execution.filters.OpenFileHyperlinkInfo;
|
import com.intellij.execution.filters.OpenFileHyperlinkInfo
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import org.jetbrains.kotlin.idea.util.DebuggerUtils
|
||||||
import com.intellij.psi.search.GlobalSearchScope;
|
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
|
||||||
import kotlin.CollectionsKt;
|
import java.util.regex.Pattern
|
||||||
import kotlin.jvm.functions.Function1;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.kotlin.idea.util.DebuggerUtils;
|
|
||||||
import org.jetbrains.kotlin.psi.KtFile;
|
|
||||||
import org.jetbrains.kotlin.resolve.jvm.JvmClassName;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
class KotlinExceptionFilter(private val searchScope: GlobalSearchScope) : Filter {
|
||||||
import java.util.regex.Pattern;
|
private val exceptionFilter = ExceptionFilter(searchScope)
|
||||||
|
|
||||||
public class KotlinExceptionFilter implements Filter {
|
private fun createHyperlinkInfo(line: String): HyperlinkInfo? {
|
||||||
private final ExceptionFilter exceptionFilter;
|
val project = searchScope.project ?: return null
|
||||||
private final GlobalSearchScope searchScope;
|
|
||||||
|
|
||||||
public KotlinExceptionFilter(@NotNull GlobalSearchScope searchScope) {
|
val element = parseStackTraceLine(line) ?: return null
|
||||||
this.exceptionFilter = new ExceptionFilter(searchScope);
|
|
||||||
this.searchScope = searchScope;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private HyperlinkInfo createHyperlinkInfo(@NotNull String line) {
|
|
||||||
Project project = searchScope.getProject();
|
|
||||||
if (project == null) return null;
|
|
||||||
|
|
||||||
StackTraceElement element = parseStackTraceLine(line);
|
|
||||||
if (element == null) return null;
|
|
||||||
|
|
||||||
// All true classes should be handled correctly in the default ExceptionFilter. Special cases:
|
// All true classes should be handled correctly in the default ExceptionFilter. Special cases:
|
||||||
// - static facades;
|
// - static facades;
|
||||||
// - package facades / package parts (generated by pre-M13 compiled);
|
// - package facades / package parts (generated by pre-M13 compiled);
|
||||||
// - local classes (and closures) in top-level function and property declarations.
|
// - local classes (and closures) in top-level function and property declarations.
|
||||||
String fileName = element.getFileName();
|
val fileName = element.fileName
|
||||||
// fullyQualifiedName is of format "package.Class$Inner"
|
// fullyQualifiedName is of format "package.Class$Inner"
|
||||||
String fullyQualifiedName = element.getClassName();
|
val fullyQualifiedName = element.className
|
||||||
int lineNumber = element.getLineNumber() - 1;
|
val lineNumber = element.lineNumber - 1
|
||||||
|
|
||||||
String internalName = fullyQualifiedName.replace('.', '/');
|
val internalName = fullyQualifiedName.replace('.', '/')
|
||||||
JvmClassName jvmClassName = JvmClassName.byInternalName(internalName);
|
val jvmClassName = JvmClassName.byInternalName(internalName)
|
||||||
|
|
||||||
KtFile file = DebuggerUtils.findSourceFileForClass(project, searchScope, jvmClassName, fileName);
|
val file = DebuggerUtils.findSourceFileForClass(project, searchScope, jvmClassName, fileName) ?: return null
|
||||||
|
|
||||||
if (file == null) return null;
|
val virtualFile = file.virtualFile ?: return null
|
||||||
VirtualFile virtualFile = file.getVirtualFile();
|
|
||||||
if (virtualFile == null) return null;
|
|
||||||
|
|
||||||
return new OpenFileHyperlinkInfo(project, virtualFile, lineNumber);
|
return OpenFileHyperlinkInfo(project, virtualFile, lineNumber)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matches strings like "\tat test.TestPackage$foo$f$1.invoke(a.kt:3)\n"
|
private fun patchResult(result: Filter.Result, line: String): Filter.Result {
|
||||||
// or "\tBreakpoint reached at test.TestPackage$foo$f$1.invoke(a.kt:3)\n"
|
val newHyperlinkInfo = createHyperlinkInfo(line) ?: return result
|
||||||
private static final Pattern STACK_TRACE_ELEMENT_PATTERN = Pattern.compile("^[\\w|\\s]*at\\s+(.+)\\.(.+)\\((.+):(\\d+)\\)\\s*$");
|
|
||||||
|
return Filter.Result(result.resultItems.map {
|
||||||
|
Filter.ResultItem(it.getHighlightStartOffset(), it.getHighlightEndOffset(), newHyperlinkInfo, it.getHighlightAttributes())
|
||||||
|
})
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private static StackTraceElement parseStackTraceLine(@NotNull String line) {
|
|
||||||
Matcher matcher = STACK_TRACE_ELEMENT_PATTERN.matcher(line);
|
|
||||||
if (matcher.matches()) {
|
|
||||||
String declaringClass = matcher.group(1);
|
|
||||||
String methodName = matcher.group(2);
|
|
||||||
String fileName = matcher.group(3);
|
|
||||||
String lineNumber = matcher.group(4);
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
return new StackTraceElement(declaringClass, methodName, fileName, Integer.parseInt(lineNumber));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
override fun applyFilter(line: String, entireLength: Int): Filter.Result? {
|
||||||
private Result patchResult(@NotNull Result result, @NotNull String line) {
|
val result = exceptionFilter.applyFilter(line, entireLength)
|
||||||
final HyperlinkInfo newHyperlinkInfo = createHyperlinkInfo(line);
|
return if (result == null) null else patchResult(result, line)
|
||||||
if (newHyperlinkInfo == null) return result;
|
}
|
||||||
|
|
||||||
return new Result(CollectionsKt.map(result.getResultItems(), new Function1<ResultItem, ResultItem>() {
|
companion object {
|
||||||
@Override
|
|
||||||
public ResultItem invoke(ResultItem item) {
|
// Matches strings like "\tat test.TestPackage$foo$f$1.invoke(a.kt:3)\n"
|
||||||
return new ResultItem(item.getHighlightStartOffset(), item.getHighlightEndOffset(), newHyperlinkInfo,
|
// or "\tBreakpoint reached at test.TestPackage$foo$f$1.invoke(a.kt:3)\n"
|
||||||
item.getHighlightAttributes());
|
private val STACK_TRACE_ELEMENT_PATTERN = Pattern.compile("^[\\w|\\s]*at\\s+(.+)\\.(.+)\\((.+):(\\d+)\\)\\s*$")
|
||||||
|
|
||||||
|
private fun parseStackTraceLine(line: String): StackTraceElement? {
|
||||||
|
val matcher = STACK_TRACE_ELEMENT_PATTERN.matcher(line)
|
||||||
|
if (matcher.matches()) {
|
||||||
|
val declaringClass = matcher.group(1)
|
||||||
|
val methodName = matcher.group(2)
|
||||||
|
val fileName = matcher.group(3)
|
||||||
|
val lineNumber = matcher.group(4)
|
||||||
|
//noinspection ConstantConditions
|
||||||
|
return StackTraceElement(declaringClass, methodName, fileName, Integer.parseInt(lineNumber))
|
||||||
}
|
}
|
||||||
}));
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public Result applyFilter(String line, int entireLength) {
|
|
||||||
Result result = exceptionFilter.applyFilter(line, entireLength);
|
|
||||||
return result == null ? null : patchResult(result, line);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user