I would like some help on this matter,
Example:
public class A { private void foo() { //Who invoked me? } } public class B extends A {} public class C extends A {} public class D { C.foo(); }
This is basically the scenario. My question is how can method foo()
know who is calling it?
EDIT: Basically I am trying to do a database Layer, and in class A
I will create a method that will generate SQL statements. Such statements are dynamically generated by getting the values of all the public
properties of the calling class
.
The simplest way is to call the getClass() method that returns the class's name or interface represented by an object that is not an array. We can also use getSimpleName() or getCanonicalName() , which returns the simple name (as in source code) and canonical name of the underlying class, respectively.
If you have a JavaSW object, you can obtain it's class object by calling getClass() on the object. To determine a String representation of the name of the class, you can call getName() on the class.
To call a method in Java, simply write the method's name followed by two parentheses () and a semicolon(;). If the method has parameters in the declaration, those parameters are passed within the parentheses () but this time without their datatypes specified.
A method must be created in the class with the name of the method, followed by parentheses (). The method definition consists of a method header and method body. We can call a method by using the following: method_name(); //non static method calling.
Easiest way is the following:
String className = new Exception().getStackTrace()[1].getClassName();
But in real there should be no need for this, unless for some logging purposes, because this is a fairly expensive task. What is it, the problem for which you think that this is the solution? We may come up with -much- better suggestions.
Edit: you commented as follows:
basically i'am trying to do a database Layer, and in Class A i will create a method that will generate sql statements, such statements are dynamically generated by getting the values of all the public properties of the calling class.
I then highly recommend to look for an existing ORM library, such as Hibernate, iBatis or any JPA implementation to your taste.
JEP 259 provides an efficient standard API for stack walking that allows easy filtering of, and lazy access to, the information in stack traces. First off, you should obtain an instance of StackWalker
:
import static java.lang.StackWalker.Option.RETAIN_CLASS_REFERENCE; // other imports StackWalker walker = StackWalker.getInstance(RETAIN_CLASS_REFERENCE);
After that you can call the getCallerClass()
method:
Class<?> callerClass = walker.getCallerClass();
Regardless of how you configured the StackWalker
instance, the getCallerClass
method will ignore the reflection frames, hidden frames and those are related to MethodHandle
s. Also, this method shouldn't be called on the first stack frame.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With