You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by jeff <jl...@houseofdistraction.com> on 2001/02/06 21:50:53 UTC

Foreach.java contains questionable assumptions

And I am the one to question those assumptions :)

There is code which tests for the type of the container

            else if (Introspector.implementsMethod(listObject, "iterator"))
                type = INFO_ITERATOR;
            else if (Introspector.implementsMethod(listObject, "values"))
                type = INFO_MAP;

and then code which uses this information to make type casts

        case INFO_ITERATOR :
            if (((Collection) listObject).size() == 0)
                return null;    
            return ((Collection) listObject).iterator();        
...
        case INFO_MAP:          
            if (((Map) listObject).size() == 0)
                return null;
            return ((Map) listObject).values().iterator();

The problem is that the tests don't guarantee the type of listObject and
so these casts may throw.

Is there some reason why the test section doesn't look like the following?

            else if ( listObject instanceof Collection )
                type = INFO_ITERATOR;
            else if ( listObject instanceof Map )
                type = INFO_MAP;