5

Is it possible to get arguments pass to a method in Java using reflection api?

Is it possible to achieve this using AOP libraries like AspectJ?

I am running on Android.

public abstract class Base {

   public void printArguments() {

      //Here I need to get access to arg1, arg2, arg3
   }

}

.

public class MyClass extends Base {

   public void (String arg1, Integer arg2, String arg3) {

      super.printArguments();      
   }
}
3
  • are u trying to print any number of arguments comming as parameter in a single method? Commented Dec 27, 2012 at 8:17
  • Yes, I am designing the caching layer for my application, and I would like to use the class name, method name, arguments, and an annotation (CacheForMinute) as a key Commented Dec 27, 2012 at 8:19
  • 1
    Possible duplicate of Java: how to get arguments passed to method that called this method? Commented Jun 13, 2019 at 17:44

2 Answers 2

3

Is it possible to achieve this using AOP libraries like AspectJ?

Yeah, sure. This is kind of the typical beginner's exercise in AspectJ, like so:

public class SampleClass {
    public SampleClass() { super(); }
    public SampleClass(String s) { this(); }
    public SampleClass(int i) { this(); }
    public void method01(String s, Number n, Throwable t) {}
    public void method02(int i, String s, double d) {}
    public void method03(String s) {}
    public void method04() {}
    public void method05(String s, Number n, double d) {}

    public static void main(String[] args) {
        new SampleClass().method01("foo", new Integer(11), new RuntimeException("error"));
        new SampleClass("test").method02(11, "bar", Math.PI);
        new SampleClass(123).method03("zot");
        new SampleClass("another test").method04();
        new SampleClass(456).method05("baz", new Integer(11), Math.E);
    }
}

Now you just need to write an aspect which intercepts all your method executions (and optionally also constructor executions, as shown below):

public aspect MethodArgsAspect {
    pointcut allMethods()      : execution(* *(..));
    pointcut allConstructors() : execution(*.new(..));

    before() : !within(MethodArgsAspect) && (allMethods() || allConstructors()) {
        System.out.println(thisJoinPointStaticPart.getSignature());
        for (Object arg : thisJoinPoint.getArgs())
            System.out.println("    " + arg);
    }
}

When running SampleClass.main, this aspect will print:

void SampleClass.main(String[])
    [Ljava.lang.String;@7fdcde
SampleClass()
void SampleClass.method01(String, Number, Throwable)
    foo
    11
    java.lang.RuntimeException: error
SampleClass()
SampleClass(String)
    test
void SampleClass.method02(int, String, double)
    11
    bar
    3.141592653589793
SampleClass()
SampleClass(int)
    123
void SampleClass.method03(String)
    zot
SampleClass()
SampleClass(String)
    another test
void SampleClass.method04()
SampleClass()
SampleClass(int)
    456
void SampleClass.method05(String, Number, double)
    baz
    11
    2.718281828459045
Sign up to request clarification or add additional context in comments.

2 Comments

It will intercept all the methods of program. Is there any way to do it dynamically, while application is running and there is a need to identify method parameter value being passed?
I will be happy to answer your question if (a) you create a new one instead of hijacking this one because you want something different from what was asked and answered here and (b) you ask the question in a way I can understand it, also providing illustrative sample code (executeable, if possible), so I have something to actually fix or implement for you. You may add a link to the new question in another comment here, so I can find it. Thank you very much.
1

You could you the java variable argument for attaining this logic

since i am not sure if it can be done in reflection

i would have done this alternative

www.java.net/pub/a/today/2004/04/19/varargs.html

where your method

 public void printArguments(Obeject... argArr) {

  //Here I need to get access to arg1, arg2, arg3
  //TODO iterate over object array (ie: argArr) and print it.
  }

and in your sub class

public void someMethodName(String arg1, Integer arg2, String arg3) {

  super.printArguments(arg1,arg2);   
  super.printArguments(arg1,arg2,arg3);      //the print argument can be called with any number of argument
}

1 Comment

I don't like doing this, I rather make everything automated, but If I can't get some kind of method interception to work, I might go with this. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.