0

I have a classA which contains following code snippet:

 public classA {

    public static Integer getValue(int x) {
      system.out.println(x);
      System.out.println(Qualifiedclassname that where you got the xvalue);
      return x;
    }
 }

The above class contains a method called getValue() which returns int as its declared as argument. This method would be called in another class files like below:

 public class B {

   @Parameters{value}
   public static void setValue(int value) {
     ClassA.getValue(value);
    }
 }

I just want to get a "class name" of the value where I'm getting it. As per above code Class B is giving a value for the getValue() method. I would like to print a class name of b in output console as "com.online.getvaluecode.ClassB".

As per my requirement, I can't use any of the below code in ClassB(client level code, but it can be used in Class A inside getValues()method):

    this.getClass().getName(); or  
    Class<?> enclosingClass = getClass().getEnclosingClass();
    enclosingClass.getName();

Class B always used to call ClassA.getValue(x) method, and It has to print the value as well "class name" where I'm getting int value for that method. I don't have permission to use any object creation in ClassB. All I have to do, call the ClassA.getValue(x);

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84
  • Your question is unclear. Do you want `ClassA.getValue` to print the name of `ClassB` ? Or do you want it to print out `java.lang.Integer` because that's the type of its argument? – Ian McLaird Apr 04 '16 at 17:35
  • Yes. I want to print a ClassB in classA.getValue() method. – ArrchanaMohan Apr 04 '16 at 17:36
  • Is `ClassA.class` what you're looking for here? Or are you looking to pass the ClassB class to the method? In which case, just change the method signature of getValue to accept a Class> as well as an int. – ManoDestra Apr 04 '16 at 17:37
  • ClassA.java file will just get some int value from class files. Along with I want to print the class name where it gets the value. like i got 10 from ClassC, and 20 from ClassD. So, I just want to print ClassC, and ClassD in output console along with int values. the problem is , I can't create any object in ClassC and ClassD to get class name. I have permission to invoke ClassA.getValue() method and single int as its argument. – ArrchanaMohan Apr 04 '16 at 17:47

3 Answers3

4

The straight-forward way is to generate a stacktrace, parse it, and find the caller:

Exception ex = new Exception();
ex.fillInStackTrace();
StackTraceElement[] trace = ex.getStackTrace();
String caller = trace[1].getClassName(); // trace[0] is us, trace[1] is our caller

Though this is generally not very performant, and should not be used in regular operation. "Who called this method" is generally not relevant in the majority of situations, and due to runtime optimizations (inlining, among other things) this information may not even be available at all. Some JVMs do not provide stack trace information because it is very expensive to provide.

I would reconsider your approach and make sure you really need to know who called your method.

Darth Android
  • 3,437
  • 18
  • 19
  • 1
    If using later versions of Java, use `Thread.currentThread().getStackTrace()` instead of creating an Exception. It will provide better performance. Note the offset in the array is probably [2] in using the `.getStackTrace()`. – KevinO Apr 04 '16 at 17:42
  • +1 for the admonishment against doing this. In general, if your method needs to figure out who called it, *you're doing it wrong* – Ian McLaird Apr 06 '16 at 17:53
0

in the method getValue(...) you get the int as parameter, the class that is calling that method is not relevant for that method per se, so java is not giving any way to get that info inside the method...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

One option is to create Exception object and call fillInStackTrace method and finally call getStackTrace() method. It returns StackTrace array from which you can get the caller of this method.