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

  • Home
  • SEARCH
  • 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 6869385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:33:04+00:00 2026-05-27T03:33:04+00:00

I have code that has a Map of (Message)Handlers. I’m trying to make the

  • 0

I have code that has a Map of (Message)Handlers. I’m trying to make the handlers generified (as seen by the interface Handler). Without generics the handlers all need to cast from Object to the respective class, which would be nice to avoid (but everything works). For each message class (Foo below) I have a handler class.

How can I keep a Map of any kind of Class to any kind of Handlers and get/call with “just” an Object? (the parameter to handleMessage(Object) can’t be restricted)

See MWE below.

import java.util.*;
public class Logic
{   
    Map<Class<?>, Handler<?>> handlers = new HashMap<Class<?>, Handler<?>>();

    public void run()
    {   
        handlers.put(Foo.class, new FooHandler());
    }   

    public void handleMessage(Object msg)
    {   
        Handler<?> handler = handlers.get(msg.getClass());
        if (handler != null) {
            handler.execute(msg);
        }   
    }   

    private interface Handler<T>
    {   
        public void execute(T msg);
    }   

    private class FooHandler implements Handler<Foo>
    {   
        public void execute(Foo msg) {}
    }   

    private class Foo {}
}

This code produces:

Logic.java:16: execute(capture#x of ?) in Logic.Handler cannot be applied > to (java.lang.Object)
handler.execute(msg);

How can this be repaired to work while still keeping the Handler interface generic?

  • 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-27T03:33:05+00:00Added an answer on May 27, 2026 at 3:33 am

    You can’t define the relationship between the key and the value in a field, but you can use accessor methods to enforce it, provided only these methods are used to access the map.

    private final Map<Class, Handler> handlers = new HashMap<Class, Handler>();
    
    public <T> void addHandler(Class<T> clazz, Handler<T> handler) {
        handlers.put(clazz, handler);
    }
    
    @SuppressWarnings("unchecked")
    public <T> Handler<T> getHandler(Class<T> clazz) {
        return (Handler<T>) handlers.get(clazz);
    }
    
    @SuppressWarnings("unchecked")
    public <T> Handler<T> getHandlerFor(T t) {
        return getHandler((Class<T>) t.getClass());
    }
    
    public void run()    {
        addHandler(Foo.class, new FooHandler());
    }
    
    public <T> void handleMessage(T msg) {
        Handler<T> handler = getHandlerFor(msg);
        if (handler != null) {
            handler.execute(msg);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that has one button that let's me choose an entry
I have a code base that has been used as an ASP.Net web application.
I have some older (broken) code that has a join using a *= table1.ID
I have a standalone Java app that has some licensing code that I want
I have a simple jQuery code that runs on a web page that has
I have written a PHP function to take a video embed code that has
I have a HTML file that has code similar to the following. <table> <tr>
I have a very large codebase (read: thousands of modules) that has code shared
You're stepping through C/C++ code and have just called a Win32 API that has
We have a Japanese client that has source code in COBOL on an mainframe.

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.