Can I construct a java.lang.StackTraceElement that references a non-Java-file?

330 Views Asked by At

I'm writing a library that parses text files that are not written in Java. When I encounter a syntax error in these text files, I would like to construct a clickable stack trace with the relevant file name and line number of these files. I already have an exception with this information in the exception name, but not the stack trace.

Ideally, my code would look something like this:

try {

   // Try to parse the file
} catch (MySyntaxException e) {
    StackTraceElement[] stackTrace = e.getStackTrace();
    StackTraceElement[] newStackTrace = new StackTraceElement[stackTrace.length+1];
    newStackTrace[0] = new StackTraceElement(e.getClassNameEquivalent(), "", e.getFile(), e.getLineNumber());
    System.arraycopy(stackTrace, 0, newStackTrace, 1, stackTrace.length);
    e.setStackTrace(newStackTrace);
    throw e;
}

When I do this, I get a stack trace that looks something like this:

org.example.MySyntaxException: Something went wrong
    at Filename.yaml.(config/files/Filename.yaml:21)
    at org.example.MyParser.parse(MyParser.java:41)
    // ...

However, IntelliJ doesn't treat the line Filename.yaml.(config/files/Filename.yaml:21) as clickable. Is there some way to convince IDEs that I can click this line?

0

There are 0 best solutions below