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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:13:19+00:00 2026-05-23T07:13:19+00:00

I’m using some Java code which produces the following interfaces. This code is non-modifiable.

  • 0

I’m using some Java code which produces the following interfaces. This code is non-modifiable.

interface A1 {
    public void run();
}

interface A2 {
    public void run();
}

...

interface A24 {
    public void run();
}

I’m having a class cast error with the following code. How would I dynamically build an adapter to my interface?

interface ARunnable {
    public void run();
}

public void someMethod() {
    // getARunnables() returns a list of A1, A2, ... A24
    List<ARunnable> runnables = (List<ARunnable>)getARunnables(); 

    for (ARunnable a : runnables) {
        a.run();
    }
}
  • 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-23T07:13:19+00:00Added an answer on May 23, 2026 at 7:13 am

    Since the interfaces can’t be modified to extend java.lang.Runnable, an option would be to use java.lang.reflect.Proxy to build up instances of Runnable that delegate to your A1, A2… interfaces.

    It’s not trivial, but take a look at this example using java.lang.reflect.Proxy. The sample simply delegates to a delegate object based on method name.

    public class ProxyTest
    {
        public static void main(String... args)
        {
            List<?> instances = Arrays.asList(new A1());
            List<ARunnable> runnableInstances = new ArrayList<ARunnable>(instances.size());
            for (Object instance : instances)
            {
                ARunnable runnableInstance = (ARunnable)Proxy.newProxyInstance(
                                                ARunnable.class.getClassLoader(), 
                                                new Class<?>[] {ARunnable.class}, 
                                                new RunnableWrapper(instance));
                runnableInstances.add(runnableInstance);
            }
    
            //Now we have a list of ARunnables!
            //Use them for something
            for (ARunnable runnableInstance : runnableInstances)
            {
                runnableInstance.run();
            }
        }
    
        private static class RunnableWrapper implements InvocationHandler
        {
            private final Object instance;
    
            public RunnableWrapper(Object instance)
            {
                this.instance = instance;
            }
    
            @Override
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable
            {
                //Ensure that your methods match exactly or you'll get NoSuchMethodExceptions here
                Method delegateMethod = instance.getClass().getMethod(method.getName(), method.getParameterTypes());
                return(delegateMethod.invoke(instance, args));
            }
        }
    
        public static class A1
        {
            public void run()
            {
                System.out.println("Something");
            }
        }
    
        public static interface ARunnable
        {
            public void run();
        }
    }
    

    Also I would recommend you fix the line

    List<ARunnable> runnables = (List<ARunnable>)getARunnables(); 
    

    That type safety warning you should not ignore. That list of objects does not actually contain ARunnables, hence the ClassCastException.

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

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm making a simple page using Google Maps API 3. My first. One marker

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.