How to get Eclipse Console to hyperlink text to source code files?

前端 未结 7 1778
生来不讨喜
生来不讨喜 2020-12-09 10:37

In Code: System.out.println(\"myPackage.MyClass\");

In Eclipse Console: myPackage.MyClass.myMethod

I want to click on the output (myPackage.MyClass.myM

相关标签:
7条回答
  • 2020-12-09 11:16

    The hyperlinking for exception stack traces is based on the file name and line number given at the end of the line. E.g.

    Stack trace:
    
    org.eclipse.jface.internal.databinding.provisional.BindingException: string property does not have a read method.
    at org.eclipse.jface.internal.databinding.internal.beans.JavaBeanObservableValue.doGetValue(JavaBeanObservableValue.java:102)
    at org.eclipse.jface.internal.databinding.internal.beans.JavaBeanObservableValue.setValue(JavaBeanObservableValue.java:83)
    

    For the first stack trace, it is at line 102 in the file JavaBeanObservableValue.java. The file is searched after in the current class path so if you have multiple classes with the same name, the first is always found...

    With other words, if you want to add extended hyperlinking based on your example, you need to extend the console view a bit...

    ...which can be done with the org.eclipse.ui.console.consolePatternMatchListeners extension point. It is pretty easy to use this extension point and by looking at the example from JDT, you should be able to get your example to work without too much work...

    0 讨论(0)
  • 2020-12-09 11:16

    I use a custom console view and I add an IPatternMatchListener to the MessageConsole with

    console.addPatternMatchListener(...)
    

    An important advantage of using the listener is that the event provides the offset that is required for creating the hyperlink.

    A starting point for the implementation of IPatternMatchListener that creates hyperlinks can be found here:

    https://github.com/mjwach/ErrorLinkyThing/blob/master/source/errorlinkything/ErrorLinkyPatternMatchListenerDelegate.java

    The trickiest part seems to be to get the full file path from the file name (I don't use a logger).

    A work around used by the sunshade plugin can be found here:

    http://sunshade.cvs.sourceforge.net/viewvc/sunshade/net.sourceforge.sunshade/src/net/sourceforge/sunshade/util/FileUtil.java?view=markup

    See method "dereferenceWindowsLink"

    I am still looking for an alternative.

    0 讨论(0)
  • 2020-12-09 11:21

    To add to the other answers, you can link to the specific class with this quirky format:

    java.util..(List.java:100)
    java.awt..(List.java:100)

    0 讨论(0)
  • 2020-12-09 11:27

    Or, if you use log4j, you can configure your appender like this:

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p %m (%F:%L) in %t%n"/>
        </layout>
    </appender>
    

    If you want it to work in IntelliJ IDEA too, you can use:

            <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p %m - at %c.%M(%F:%L) in %t%n"/>
    
    0 讨论(0)
  • 2020-12-09 11:34

    Maybe this is clear to other people, but I found the other answers confusing, although correct. The eclipse console parses the pattern (FileName.java:lineNumber) to be a link to that line in that file:

    (MyFile.java:2)
    

    As the other answers point out, there are many ways to output this to the console. Location in the line does not matter, it is just a pattern match. As Colin Smith shows, log4j PatternLayout can use (%F:%L) to get the file name and line number. To get the file name and line number programmatically see this question.

    The question asks about linking to a method and I believe you could use the consolePatternMatchListeners method recommended by Tonny Madsen and described in more detail in this question.

    0 讨论(0)
  • 2020-12-09 11:36

    A much easier method is to trick the console into creating a link for you. The format is simple:

    System.out.println("(" + new TestBed().getClass().getSimpleName() + ".java:" + 18 + ")");
    

    Obviously you can provide the class type and line number in code as you wish.

    0 讨论(0)
提交回复
热议问题