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 706469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:10:12+00:00 2026-05-14T04:10:12+00:00

Is there any technique available in Java for intercepting messages (method calls) like the

  • 0

Is there any technique available in Java for intercepting messages (method calls) like the method_missing technique in Ruby? This would allow coding decorators and proxies very
easily, like in Ruby:

:Client            p:Proxy                    im:Implementation
-------           ----------                  -----------------

p.foo() -------> method_missing()
                    do_something
                    im.foo() ------------------> do_foo


p.bar() --------> method_missing()
                   do_something_more
                    im.bar() -------------------> do_bar

(Note: Proxy only has one method: method_missing())

  • 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-05-14T04:10:12+00:00Added an answer on May 14, 2026 at 4:10 am

    As others have correctly said already, use a DynamicProxy. Here’s an example.

    This class uses a DynamicProxy to intercept invocations of methods declared in the “HammerListener” interface. It does some logging and then delegates to the “real” HammerListener implementation (yes, the same thing can be done with AOP).

    See the newInstance method for proxy instantiation (note that you need to pass in the interface(s) the proxy should implement – a proxy can implement multiple interface).

    All method invocations on interfaces that the proxy implements will end up as calls to the “invoke” method, which is declared in the “InvocationHandler” interface. All proxy handlers must implement this interface.

    import java.lang.reflect.*;
    
    /**
     * Decorates a HammerListener instance, adding BEFORE/AFTER 
     * log messages around all methods exposed in the HammerListener interface.
     */
    
    public class HammerListenerDecorator implements InvocationHandler {
    
        private final HammerListener delegate;
    
        static HammerListener newInstance(HammerListener delegate) {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            return (HammerListener)Proxy.newProxyInstance(cl, new Class[]{HammerListener.class},
                new HammerListenerDecorator(delegate));
        }
    
        private HammerListenerDecorator(HammerListener delegate) {
            this.delegate = delegate;
         }
    
         @Override
         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
             logger.info("BEFORE " + method.getName() + " {{{" + argsToString(args) + "}}}");
             Object rtn = method.invoke(delegate, args);
             logger.info("AFTER " + method.getName());
             return rtn;
         }
    
         private String argsToString(Object[] args) {
             StringBuilder sb = new StringBuilder();
             for (Object o : args) {
                 sb.append(String.valueOf(o)).append(" ");
             }
             return sb.toString();
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any technique or tool available to detect this kind of a deadlock
i'm using spring+tapestry for authenticate webuser. I wonder is there any technique i can
I am wondering if there is any technique out there for making HTML code
I just wanted to know, if there is any technique to insert values into
I am very new to Ajax technique Is there any site or online documents
Is there any software available in linux which compiles a source code containing large
Is there any readily available snippet that has all the countries, states and cities
I've been researching on drop-shadow-effect technique for web design. So, I would like to
First is there any technique for using Action Script for recording sound from microphone?
I was wondering - are there any known techniques to control access to a

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.