The answer to this comes down to how much control you have over the code that is implementing the Listener. You are right that it is not possible to create a stacktrace without being in a method.
The general technique is to create an Exception(), in the constructor, but don't throw it. This contains the stacktrace information, which you can use how you want. This will give you the line number of the constructor, but not of the class. Please note that this method is not particularly performant either, because creating a stacktrace is expensive.
You will need to either:
- force some code to be executed in the constructor (relatively easy if your Listener is an abstract class which you control)
- Instrument the code somehow (the cure seems worse than the disease here).
- Make some assumptions about the way classes are named.
- Read the jar (do the same thing as javac -p)
For 1), you'd simply put the Exception creation in the abstract class, and the constructor gets called by the subclass:
class Top {
Top() {
new Exception().printStackTrace(System.out);
}
}
class Bottom extends Top {
public static void main(String[] args) {
new Bottom();
}
}
this produces something like:
java.lang.Exception
at uk.co.farwell.stackoverflow.Top.<init>(Top.java:4)
at uk.co.farwell.stackoverflow.Bottom.<init>(Bottom.java: 11)
at uk.co.farwell.stackoverflow.Bottom.main(Bottom.java: 18)
In general, there are some naming rules which are followed: If you have an outer class called Actor and an inner called Consumer, then the compiled class will be called Actor$Consumer. Anonymous inner classes are named in the order in which they appear in the file, so Actor$1 will appear in the file before Actor$2. I don't think this is actually specified anywhere, so this is probably just a convention, and shouldn't be relied upon if you're doing anything sophisticated with multiple jvms etc.
It is possible, as jmg pointed out, that you can define multiple top level classes in the same file. If you have a public class Foo, this must be defined in Foo.java, but a non-public class can be included in another file. The above method will cope with this.
Explanation:
If you disassemble the java (javap -c -verbose), you'll see that there are line numbers in the debug information, but they only apply to methods. Using the following inner class:
static class Consumer implements Runnable {
public void run() {
// stuff
}
}
and the javap output contains:
uk.co.farwell.stackoverflow.Actors$Consumer();
Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #10; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 20: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Luk/co/farwell/stackoverflow/Actors$Consumer;
The LineNumberTable contains the list of line numbers which apply to a method. So my constructor for the Consumer starts at line 20. But this is the first line of the constructor, not the first line of the class. It is only the same line because I'm using the default constructor. If I add a constructor, then the line numbers will change. the compiler does not store the line that the class is declared on. So you can't find where the class is declared without parsing the java itself. You simply don't have the information available.
However, if you're using an anonymous inner class such as:
Runnable run = new Runnable() {
public void run() {
}
};
Then the line number of the constructor and class will match[*], so this gives you an line number.
[*] Except if the "new" and "Runnable()" are on different lines.
Listener
instance whereListener
is an interface or abstract class. You can distinguish them by the concrete class of theirListener
reference: if it is anXxxListener
you readXxxListener.java
and get a clue what is it going to do, but what can you do if theListener
is an instance ofSomeOuterClass$12
? – Pride