Find the caller of a method (reflection) in Java [duplicate] - java

This question already has answers here:
How do I find the caller of a method using stacktrace or reflection?
(13 answers)
How to get the caller class in Java [duplicate]
(10 answers)
Closed 9 years ago.
I want to recover in Java the instance of the caller.
Here is my code
class A {
void methodA() {
B b = new B();
A a = b.methodB(); //needs to be the same instance ie "this"
}
}
class B {
A methodB() {
//Here the code (with reflection) I need
A a = getPreviousInstanceCaller();
return A
}
}
Does it exist in Java a way to do that ? Maybe with reflection ?

You don't need reflection for this. You need one of these methods.
http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getStackTrace%28%29
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getStackTrace%28%29
Note that the ordering of the stack trace elements (0, 1, 2, etc.)
may vary in different versions of the JDK. Meaning in some versions
element 0 may be the top-most element, while in others it may be the
bottom-most one. This is an important thing to have in mind.
See here for more details.
Getting the name of the current executing method

Related

What do the instances of "this" mean in this code? [duplicate]

This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 4 years ago.
public class MyResults extends Results {
...public MyResults() {
this(5);
}
public double average() {
return this.getSum()/numberOfCourses;
}
}
What do both instances of ―this mean in the code?
First instance is a call to another constructor in the same class. This is also known as Constructor Chaining pattern. Since you didn't post the entire code we don't know if that other constructor is defined (it should be, otherwise you'll have a compile time error).
Second instance is a call to the getSum() method. This method might be defined either in MyResults class or Results class (or some parent class of Results, if any).

how this kind of syntax is even possible [duplicate]

This question already has answers here:
What is the point of allowing type witnesses on all method calls?
(4 answers)
Issues relating to type parameters in Java method calls
(1 answer)
Closed 4 years ago.
Ive just noticed this kind of syntax:
System.out.<String>println("...");
If we look at method println:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
Its not parametrized, so how complier dont give a warning? Thx for your time
The code makes no sense and of course has to not be used as whatever the generic type you specify, it has the same effect : nothing but a warning at compile time explaining that the type argument is not used as the method is not generic.

Java | What does synthetic mean? [duplicate]

This question already has answers here:
Synthetic Class in Java
(13 answers)
Closed 7 years ago.
I was going through the java Method.class (Decompiled) and I found something that caught my eye.
#Override
public boolean isSynthetic() {
return super.isSynthetic();
}
"Synthetic". What does that mean?
Is it usable in code?
.
And since I found this in the Method class, I was wondering, is it that an entire method could be "synthetic", or is it that it contains something synthetic?
Thanks in advance.
.
not a copy of What is the meaning of “static synthetic”?
Any constructs introduced by the compiler that do not have a
corresponding construct in the source code must be marked as
synthetic, except for default constructors and the class
initialization method.
http://www.javaworld.com/article/2073578/java-s-synthetic-methods.html

Invoking the caller of a method in Java [duplicate]

This question already has answers here:
How do I find the caller of a method using stacktrace or reflection?
(13 answers)
Closed 9 years ago.
Just wondering, how do you find out the name of the method that invokes another method? I want to use the Method class.
You can make use of StackTraceElement :
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
Order of elements in StackTraceElement array: The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.
Once you have the desired method name then you can call it using reflection :
Method lastMethod = YourClass.class.getMethod("yourMethodName");
lastMethod.invoke();
Note: The code should be changed as per your class and method.
Getting the name is tricky enough, getting the method is very hard because you don't know the argument list but you do know the class and you can use the line number when reading the byte code to find which method which contains that line. If the method is static (and I suspect its not) you are ok. If you need an instance, finding that is next to impossible in any portable way.
The sort of thing you are trying to do is the Java equivalent of saying; how do I learn to flight in a couple of easy steps.
What ever your problem is this is highly unlikely to be a good solution. If you need to call back to the object which called you, you should pass that object as an argument, ideally via an interface.
When ever you find the code running back on itself, this is sure to end in a mess of some kind, You want to simplify your dependencies.
What you should do is something like this.
interface Callback<T> {
onResult(T t);
}
class A implements Callback<MyResult> {
B b = new B();
public void method() {
b.doSomething(this);
}
public void onResult(MyResult mr) {
// do something with mr
}
}
class B {
public void doSomething(Callback<MyResult> cb) {
// do something
cb.onResult(new MyResult(something));
// do something else
}
}
Of course this would be all much simpler if instead of call back on the first method, you just returned a result and half this code wouldn't be needed. This is why normally when you want to pass something back to the caller, you use a return value instead of recursion.

How does a method signature of only "static" work? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Static initializer in Java
I have a few years' experience with Java, but I've recently run across something I've never seen before:
public class Project{
...
static{
initDataTypeMapping();
}
...
}
How does this method signature work? Is this in fact even technically a method? I'm wondering why one wouldn't simply put the the method call to initDataTypeMapping() in the constructor. Just trying to increase my understanding so I don't mess something up. Thanks!
This is known as a static initializer.
The code in the static { } block is run when the class is first loaded by the classloader (which is usually, but not always, when code that refers to the class is first loaded/executed), and is guaranteed to be run in a thread-safe manner.
See this question also.

Categories