0

I have a listener class which listens to message objects. The messages objects are checked for instance types and appropriate action is taken once instance type is determined. Below is the sample code

public void onMessage(IMessage message){

   if(message instanceof MoPn) {
      doThis();
   } else if(message instanceof MoAn) {
      doThat();
   } else if(message instanceof MoBn) {
      doSomethingElse();
   }
}

I know this is not the best possible way of doing this, but for legacy reasons, I cant change it. The point now is I wanted to make the message handling asynchronous. All I would like to do is check if the message falls in the category I am interested in. If yes, put it in the Queue for async processing. Below is the sample class.

public class SampleClass
{
    List<Class<? extends IMessage>> messageClasses = new ArrayList<Class<? extends IMessage>>();

    SampleClass() {
        messageClasses.add(moAn.class);
        messageClasses.add(moBn.class);
        messageClasses.add(moPn.class);
    }

    public void onMessage(IMessage message) {
        for(IMessage messageClass : messageClasses) {
           if(message instanceof messageClass) {
              putItInTheQueue(message);
           }
        }   
    }
}

When I code like the above, I cant cast my message class properly. Can somebody help me writing syntactically correct code for the below statement?

 if(message instanceof messageClass  )
2
  • 1
    The elements of your messageClasses list would be Class objects, not IMessage objects. Commented Oct 19, 2014 at 18:35
  • If you do the same thing for every kind of message, why the check in the first place? Or is this just a simplified example? Commented Oct 19, 2014 at 18:39

3 Answers 3

3

instanceof only works with classes known at compile time. Check the documentation of Class instead. You can do something like:

public void onMessage(IMessage message) {
    for(Class<? extends IMessage> messageClass : messageClasses) {
        if(messageClass.isAssignableFrom(message.getClass())) {
            putItInTheQueue(message);
        }
    }
}   
Sign up to request clarification or add additional context in comments.

2 Comments

should be message.getClass(), isAssignableFrom() takes a Class as arguement
Yes it should, I've fixed it.
1
 parentClazz.isAssignableFrom(clazz)

Comments

1

As the other answers have mentioned you can use messageClass.isAssignableFrom(message.getClass()).

You can also use the isInstance method, which is pretty much the exact equivalent of the instanceof operator(with the arguments swapped). I.e.

for(Class<? extends IMessage> messageClass : messageClasses) {
    if(messageClass.isInstance(message)) {
        putItInTheQueue(message);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.