Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the name of the calling class in Java?

Tags:

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.

like image 856
Mark Buhagiar Avatar asked Nov 08 '09 13:11

Mark Buhagiar


People also ask

How do you call a class name in Java?

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.

How do I find the class name of an object?

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.

How do you call a name in Java?

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.

How do you call a method by class name?

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.


2 Answers

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.

like image 181
BalusC Avatar answered Oct 26 '22 17:10

BalusC


Java 9: Stack Walking API

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 MethodHandles. Also, this method shouldn't be called on the first stack frame.

like image 31
Ali Dehghani Avatar answered Oct 26 '22 18:10

Ali Dehghani