Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying the caller method and arguments without reflection

Tags:

java

Let's say I have a class with a method like below

public class Parent {

    public boolean isValidURL() {
        System.out.println("print the name of the caller method and the method's arguements here");
        //pls ignore the return true below. just an eg.
        return true;
    }
}

I then have another method that calls the isValidURL in the parent class

public class Child {
    Parent parent = new Parent();

    public void verifyURL(String url) {
        parent.isValidURL();
    }
}

Now in the Parent class the method isValidURL() should print the caller method which is verifyURL() and its arguments.

Is that possible without reflection? Are there any design pattern that needs to be followed?

EDIT: I want to do this because I want to implement the idea on a logger. Basically, there are many other methods like verifyURL() method accepting different parameters. I'd like to have a common logger to print the it on the console when any methods in the `Child' class is called

like image 881
Damien-Amen Avatar asked Jan 09 '23 05:01

Damien-Amen


1 Answers

Is that possible without reflection?

No. (I don't even think it's possible with reflection.)

Are there any design pattern that needs to be followed?

The pattern here would be to pass the relevant information as argument to the method. :-)

You could also pass the instance of the Child to the constructor of the Parent, and store the URL as a field in Child.

Parent parent = new Parent(this);  // ...then look up URL through field in Child

Or, you could do use a setter prior to the call to isValidURL:

public void verifyURL(String url) {
    parent.setUrl(url);
    parent.isValidURL();
}

Regarding your edit:

EDIT: I want to do this because I want to implement the idea on a logger. Basically, there are many other methods like verifyURL() method accepting different parameters. I'd like to have a common logger to print the it on the console when any methods in the `Child' class is called

That clears things up quite a bit.

For this I recommend looking at the current stack trace. I posted a similar solution over here:

  • Include filename/line number corresponding to System.out.print statements in Eclipse Console

What's important to keep in mind for robustness is that you loop through the stack trace until you find the element you're looking for. Even though Parent may internally delegate the call or use isValidUrl as a helper method, it is most likely the calling class (in this case Child) that is of interest. Here's an example that discards the stack elements that are "internal" and prints the name of the calling class/method:

public boolean isValidURL() {
    for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
        if (ste.getClassName().equals(Thread.class.getName())
                || ste.getClassName().equals(getClass().getName()))
            continue;
        System.out.println(ste.getClassName() + "." + ste.getMethodName());
        break;
    }
    return true;
}
like image 109
aioobe Avatar answered Jan 10 '23 17:01

aioobe