Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8122799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:57:03+00:00 2026-06-06T05:57:03+00:00

How can I log the parameters passed to a method at runtime ? Is

  • 0

How can I log the parameters passed to a method at runtime ? Is there any Java library for this or any any exception that can be raised to monitor it ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:57:04+00:00Added an answer on June 6, 2026 at 5:57 am

    You can use javassist’s ProxyFactory or Translator to change to print the arguments at runtime:


    Using Translator (with a new ClassLoader):

    public static class PrintArgumentsTranslator implements Translator {
    
        public void start(ClassPool pool) {}
    
        @Override
        public void onLoad(ClassPool pool, String cname)
                throws NotFoundException, CannotCompileException {
            CtClass c = pool.get(cname);
    
            for (CtMethod m : c.getDeclaredMethods()) 
                insertLogStatement(c, m);
            for (CtConstructor m : c.getConstructors())
                insertLogStatement(c, m);
        }
    
        private void insertLogStatement(CtClass c, CtBehavior m) {
            try {
                List<String> args = new LinkedList<String>();
                for (int i = 0; i < m.getParameterTypes().length; i++)
                    args.add("$" + (i + 1));
    
                String toPrint = 
                    "\"----- calling: "+c.getName() +"." + m.getName() 
                    + args.toString()
                    .replace("[", "(\" + ")
                    .replace(",", " + \", \" + ")
                    .replace("]", "+\")\""); 
                m.insertBefore("System.out.println("+toPrint+");");
            } catch (Exception e) { 
                // ignore any exception (we cannot insert log statement)
            }
        }
    }
    

    *Note that you need to change the default ClassLoader so that you can instrument the classes, so before calling your main you need some inserted the following code:

    public static void main(String[] args) throws Throwable {
        ClassPool cp = ClassPool.getDefault();
        Loader cl = new Loader(cp);
        cl.addTranslator(cp, new PrintArgumentsTranslator());
        cl.run("test.Test$MyApp", args);  // or whatever class you want to start with
    }
    
    public class MyApp {
    
        public MyApp() {
            System.out.println("Inside: MyApp constructor");
        }
    
        public static void main(String[] args) {
            System.out.println("Inside: main method");
            new MyApp().method("Hello World!", 4711);
        }
    
        public void method(String string, int i) {
            System.out.println("Inside: MyApp method");
        }
    }
    

    Outputs:

    ----- calling: test.Test$MyApp.main([Ljava.lang.String;@145e044)
    Inside: main method
    ----- calling: test.Test$MyApp.Test$MyApp()
    Inside: MyApp constructor
    ----- calling: test.Test$MyApp.method(Hello World!, 4711)
    Inside: MyApp method
    

    Using ProxyFactory

    public class Test {
    
        public String method(String string, int integer) {
            return String.format("%s %d", string, integer);
        }
    
        public static void main(String[] args) throws Exception {
    
            ProxyFactory f = new ProxyFactory();
            f.setSuperclass(Test.class);
    
            Class<?> c = f.createClass();
            MethodHandler mi = new MethodHandler() {
                public Object invoke(
                        Object self, Method m, Method proceed, Object[] args)
                    throws Throwable {
    
                    System.out.printf("Method %s called with %s%n", 
                                      m.getName(), Arrays.toString(args));
    
                    // call the original method
                    return proceed.invoke(self, args);
                }
            };
    
            Test foo = (Test) c.newInstance();
            ((Proxy) foo).setHandler(mi);
            foo.method("Hello", 4711);
        }
    }
    

    Output:

    Method method called with [Hello, 4711]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a project that can log errors in requests to Django on Google
Is there any way I can log a Wordpress user in given only their
The below log snippet shows the parameters that get passed in, but then when
By looking into the Open JPA website i've found that i can log the
HI, I have a device that exposes a telnet interface which you can log
Using KB180548 in a native c++ application, the user can log in in this
I got a vertical testimonials belt and i have this method that animates its
I know I can log exceptions with PetaPoco thus: public override void OnException(Exception x)
How can I find out more about that error? This is what's shown in
I am trying to log a method's parameters by using reflection. I read the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.