Announcement Announcement Module
No announcement yet.
ClassCastException from BindingModel Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • ClassCastException from BindingModel


    I just came up to following situation:
    	private List  [More ...] toErrors(Message[] messages) {
    		if (messages == null || messages.length == 0) {
    			return Collections.EMPTY_LIST;
    		ArrayList errors = new ArrayList(messages.length);
    		for (int i = 0; i < messages.length; i++) {
    			Message message = messages[i];
    			if (message.getSource() == null) {
    				errors.add(new ObjectError(objectName, message.getText()));
    			} else {
                                    // ClassCastException from casting to String
    				errors.add(new FieldError(objectName, (String) message.getSource(), message.getText()));
    		return Collections.unmodifiableList(errors);
    The line "errors.add(new FieldError(objectName, (String) message.getSource(), message.getText()));" is likely to throw a ClassCastException each time when source of message isn't a String...

    It is possible since Message defines getSource() as:
    	public Object getSource() {
    		return source;
    So it is not forbidden to set source to something different than String...

    Wouldn't it be better to make a toString() there?
    I found this actually when calling toString() on BindingModel (AbstractErrors.toString()->getErrorCount()->getAllErrors()->getGlobalErrors()->toErrors()->bang! ) - know that this is a corner case... but it sometimes makes debugging really nasty.