17

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

In Eclipse Console: myPackage.MyClass.myMethod

I want to click on the output (myPackage.MyClass.myMethod) in Console and it directly shows the corresponding method, similar to what happens for exception stack traces. Any Idea?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
salman.mirghasemi
  • 1,009
  • 2
  • 7
  • 15

7 Answers7

18

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.

Community
  • 1
  • 1
viking
  • 296
  • 2
  • 7
10

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...

Tonny Madsen
  • 12,628
  • 4
  • 31
  • 70
7

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.

Rachel K. Westmacott
  • 2,038
  • 20
  • 15
  • to get the line number of the current line, you can throw and exception and grab the line number from that. Not amazingly efficient, probably, but if you're doing this kind of thing then efficiency is probably not what you're after. – Lynden Shields Mar 21 '12 at 05:39
  • `Class.getName` will output the canonical classname - with packages so this will only work if you dont use packages (which is an indicator for bad code), `getSimpleName` should work in all cases – specializt May 06 '15 at 11:18
3

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"/>
2

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.

Stefan
  • 10,010
  • 7
  • 61
  • 117
0

Here's a simple wrapper method based on everyone else's answers that can be used anywhere to get a string formatted so that the Eclipse console will link to the line in any file where getSourceCodeLine() is called. I also discovered that printing to System.err in Eclipse will be shown in red.

public static String getSourceCodeLine() {
    // An index of 1 references the calling method
    StackTraceElement ste = new Throwable().getStackTrace()[1]; 
    return "(" + ste.getFileName() + ":" + ste.getLineNumber() + ")";
}

public static void main( String[] args )
    System.out.println("Here's a link to " + getSourceCodeLine());
    System.err.println("And a red link to " + getSourceCodeLine());
}
David
  • 251
  • 2
  • 11
0

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)

boot-and-bonnet
  • 731
  • 2
  • 5
  • 16