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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:47:33+00:00 2026-06-12T19:47:33+00:00

I want to create a dynamic proxy, which can delegate its methods to different

  • 0

I want to create a dynamic proxy, which can delegate its methods to different implementations (each method invocation chooses a potentially different object). And I want to achieve the polymorphic effect, like when some proxied method calls another proxied method, the object selection mechanism does apply again.

Okay, enough confusion, here is an example:

interface IService {
  void a();
  void b();
}

class HappyService implements IService {
  public void a() {
    System.out.println("Happy a");
    b();
  }

  public void b() {
    System.out.println("Happy b");
  }
}

class SadService implements IService {
  public void a() {
    System.out.println("Sad a");
    b();
  }

  public void b() {
    System.out.println("Sad b");
  }
}

Now, I want to create a proxy for IService which always chooses HappyService for invocations of method a() and SadService for invocations of method b(). Here is what comes to my mind at first:

InvocationHandler h = new InvocationHandler() {
  @Override
  public Object invoke( final Object proxy, final Method method, final Object[] args ) throws Throwable {
    Object impl;
    if (method.getName().equals("a")) {
      impl = new HappyService();
    } else if (method.getName().equals("b")) {
      impl = new SadService();
    } else {
      throw new IllegalArgumentException("Unsupported method: " + method.getName());
    }
    return method.invoke(impl, args);
  }
};
IService service = (IService)Proxy.newProxyInstance( IService.class.getClassLoader(), new Class[]{ IService.class }, h );
service.a();

This prints:

Happy a
Happy b

Yeah, that’s because the invocation of b() inside of a() does not know anything about the dynamic proxy.

So, how do I best achieve my goal? My desired output is:

Happy a
Sad b

I could probably replace my new HappyService() inside the invocation handler with yet another proxy, which passes only method a() to HappyService, and redirects all other methods back to the original proxy. But maybe there is a better/easier solution?

  • 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-12T19:47:34+00:00Added an answer on June 12, 2026 at 7:47 pm

    A proxy can only be used to intercept inter-object calls, when the caller has a reference to the proxy instead of the “real” implementation. Here, both your implementations of a() call b() directly, so of course they call it on this. What you want to do cannot be achieved by proxies.

    However, you can do it using AOP, for example using AspectJ and either compile-time weaving (also available with the aspectj-maven-plugin) or load-time weaving. Roughly, you create an aspect with pointcuts on the calling sites of the a() and b() methods in the IService implementations, and you advise the execution, which might be to replace the original call by another. Untested code, but it looks like this:

    public aspect IServiceAspect {
        private IService happy = new HappyService(); // Assuming these are stateless services
        private IService sad = new SadService();     // that can be shared.
    
        // The pointcut is inside an IService implementation, and is a call to the a() method
        pointcut aCall(): within(IService+) && call(* IService.a());
    
        pointcut bCall(): within(IService+) && call(* IService.b());
    
        around(): aCall() && !within(HappyService) { // To avoid infinite recursion
            return happy.a();
        }
    
        around(): bCall() && !within(SadService) {
            return sad.b();
        }
    }
    

    You can then provide directly an HappyService or SadService implementation as an IService, both have been modified. You can also create a single pointcut matching all the methods of IService, and do the routing dynamically based on the method name (using thisJoinPoint in the advice), like in your example.

    Aspects can also be declared using annotations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create dynamic tabPages in TabControl. In each tabPage I create dataGridView
I want to create dynamic number of thread which is depends on the database
I want to create a dynamic Expression<Func<T,Y>> . Here is the code which works
I've got a need to create a dynamic proxy in C#. I want this
I want to create a dynamic (fetching data from the database) XML sitemap which
I have issue where i want to create Dynamic function which will do some
I want to create dynamic UITextField with different object name.I have shown the code
I want to create dynamic html which needs to render as code given below:
I want to create dynamic lambda expressions so that I can filter a list
Hello i want to create dynamic drop-downs using javascript, but its not working.. My

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.