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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:23:05+00:00 2026-06-14T07:23:05+00:00

I have a class from another library that is closed-source, but I want to

  • 0

I have a class from another library that is closed-source, but I want to be able to use an interface for it. The reason being that I don’t want to do instanceof checks or null-checks everywhere, but I also don’t want to extend the existing class.

For example, let’s say I have this code:

public class Example {

    // QuietFoo is from another library that I can't change
    private static QuietFoo quietFoo;
    // LoudFoo is my own code and is meant to replace QuietFoo
    private static LoudFoo loudFoo;

    public static void main(String[] args) {
        handle(foo);
    }

    private static void handle(Object foo) {
        if (foo instanceof QuietFoo)
            ((QuietFoo) foo).bar();
        else if (foo instanceof LoudFoo)
            ((LoudFoo) foo).bar();
    }
}

I can’t change QuietFoo:

public class QuietFoo {

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

But I can change LoudFoo:

public class LoudFoo {

    public void bar() {
        System.out.println("BAR!!");
    }
}

The problem is, there may be many other implementations of bar in many classes, and there may be more methods than just bar, so not only would my handle method get slow and ugly with lots of instanceof statements, but I would have to write one of these handle methods for each method on QuietFoo and LoudFoo. Extending isn’t a viable solution because it violates the whole is-a contract since LoudFoo is not a QuietFoo.

Basically, given Foo:

public interface Foo {
    void bar();
}

How can I make QuietFoo implement Foo without changing its source so I don’t have to do casting and instanceof calls everywhere in my code?

  • 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-14T07:23:06+00:00Added an answer on June 14, 2026 at 7:23 am

    There are two approaches:

    1. Using an adapter pattern
    2. Using Proxy

    The adapter approach will be simpler but less flexible, and the Proxy approach will be more complex but more flexible. Even though the Proxy approach is more complex, that complexity is all restricted to a couple of classes.


    Adapter

    The adapter pattern is simple. For your example, it would just be one class, like so:

    public class QuietFooAdapter implements Foo {
    
        private QuietFoo quietFoo;
    
        public QuietFooAdapter(QuietFoo quietFoo) {
            this.quietFoo = quietFoo;
        }
    
        public void bar() {
            quietFoo.bar();
        }
    }
    

    Then to use it:

    Foo foo = new QuietFooAdapter(new QuietFoo());
    foo.bar();
    

    This is good, but if you have more than one class to make an adapter for, this can be tedious since you’ll need a new adapter for each class you have to wrap.


    Java’s Proxy Class

    Proxy is a native Java class that’s part of the reflection libraries that will allow you to create a more generic, reflective solution. It involves 3 parts:

    1. The interface (in this case, Foo)
    2. The InvocationHandler
    3. Creation of the proxy (Proxy.newProxyInstance)

    We already have the interface, so we’re fine there.

    The InvocationHandler is where we do our “auto-adapting” via reflection:

    public class AdapterInvocationHandler implements InvocationHandler {
    
        private Object target;
        private Class<?> targetClass;
    
        public AdapterInvocationHandler(Object target) {
            this.target = target;
            targetClass = target.getClass();
        }
    
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            try {
                Method targetMethod = targetClass.getMethod(method.getName(), method.getParameterTypes());
                if (!method.getReturnType().isAssignableFrom(targetMethod.getReturnType()))
                    throw new UnsupportedOperationException("Target (" + target.getClass().getName() + ") does not support: " + method.toGenericString());
                return targetMethod.invoke(target, args);
            } catch (NoSuchMethodException ex) {
                throw new UnsupportedOperationException("Target (" + target.getClass().getName() + ") does not support: " + method.toGenericString());
            } catch (IllegalAccessException ex) {
                throw new UnsupportedOperationException("Target (" + target.getClass().getName() + ") does not declare method to be public: " + method.toGenericString());
            } catch (InvocationTargetException ex) {
                // May throw a NullPointerException if there is no target exception
                throw ex.getTargetException();
            }
        }
    }
    

    The important code here is in the try block. This will handle the process of adapting any method calls that are called on the proxy to the inner target object. If a method is called on the interface that isn’t supported (non-public, wrong return type, or just flat out doesn’t exist), then we throw an UnsupportedOperationException. If we catch an InvocationTargetException, we rethrow the exception that caused it via InvocationTargetException.getTargetException. This occurs when the method we called reflectively throws an exception. Java wraps it in a new exception and throws that new exception.

    Next, we need something to create the adapters:

    public class AdapterFactory {
    
        public static <T> T createAdapter(Object target, Class<T> interfaceClass) {
            if (!interfaceClass.isInterface())
                throw new IllegalArgumentException("Must be an interface: " + interfaceClass.getName());
            return (T) Proxy.newProxyInstance(null, new Class<?>[] { interfaceClass }, new AdapterInvocationHandler(target));
        }
    }
    

    You could also nest the AdapterInvocationHandler class in the AdapterFactory class, if you like, so that everything is self-contained in AdapterFactory.

    Then to use it:

    Foo foo = AdapterFactory.createAdapter(new QuietFoo(), Foo.class);
    foo.bar();
    

    This approach requires more code than implementing a single adapter, but will be generic enough that it can be used to create auto-adapters for any class and interface pair, not just the QuietFoo and Foo example. Granted, this method uses reflection (the Proxy class uses reflection, as does our InvocationHandler), which can be slower, but recent improvements in the JVM have made reflection much faster than it used to be.

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

Sidebar

Related Questions

I have an RMI server that is using a class from another project. I
I have a static library project that I inherited from another developer. I added
I have a CSV file that is being exported from another system whereby the
I want create one form from another. But the Form class has no the
I have a winforms form which is inherited from another form. e.g. class StartForm
I have two namespaces: 1) Foo.Bar 2) Another.Foo.Bar From a class in namespace 2,
I have copied class from net which inherits UIImageView. How to put that class
I copied this class from a similar class I have in this app that
i have an application class inherited from QtGui.QDialog. I have to reload show-function but
I have a class library that has it's own model and connectionstring to 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.