0

I have 3 classes, 2 service layer classes are calling a utility class. Is it possible to find the name of the class that is calling my static method from within the method or is it too late at that point?

I don't want to pass the class when I call it because I feel like I should be able to infer that and want to ensure that there is no room for error. Like if the wrong class is passed or if null is passed instead.

Is this possible?

// ClassA.java
public void doSomething(){
    // Logic ...
    ClassC.invoke();
}

// ClassB.java
public void doSomethingElse(){
    ClassC.invoke();
    // Logic ...
}

// ClassC.java
private static final Logger LOGGER = LoggerFactory.getLogger(ClassC.class);

public static void invoke(){
    String callingClass = // findClassingClassName 
    LOGGER.info("ClassC::invoke was called by: {} @ {}", callingClass, LocalDateTime.now());
}
wheeleruniverse
  • 1,511
  • 2
  • 20
  • 37

0 Answers0