Results 1 to 3 of 3

Thread: LinkedMap instead of LinkedHashMap when running Java1.6 for PropertyEditors

  1. #1
    Join Date
    Mar 2008
    Location
    Manchester, UK
    Posts
    44

    Default LinkedMap instead of LinkedHashMap when running Java1.6 for PropertyEditors

    I have fixed our problem but I have no idea why changing from Java 1.5 to Java 1.6 should case the following error.

    org.apache.commons.collections.map.LinkedMap cannot be cast to
    java.util.HashMap

    The code that was generating the problem was:
    Code:
    private Map<String, Object> mPropertyEditors = new HashMap<String, Object>();
    
    private void someFunction(Object aObject) {
       Map fieldNames = (HashMap)mPropertyEditors.get(aObject.getClass().getName());
    }
    under Java 1.5 the mPropertyEditors.get() returned a java.util.LinkedHashMap
    under java 1.6 it returns org.apache.commons.collections.map.LinkedMap

    Changing the cast to java.util.Map solves the issue but I do not understand why changing to Java 1.6 should cause this change.

    Any thoughts ?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    You should always cast to an interface instead of a concrete implementation.

    Is this custom code or is it code somewhere in the spring framework? Spring does some detection of which collection implementation to use, based on jdk version and classpath. Also which version of spring does it concern? Spring < 2.5.x didn't contain any specific jdk 1.6 detection stuff. Check the CollectionFactory class for details.
    Last edited by Marten Deinum; Sep 30th, 2008 at 01:57 PM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2008
    Location
    Manchester, UK
    Posts
    44

    Default

    Spring 1.2.7

    You should always cast to an interface instead of a concrete implemention - Good point

    The class is a Controller that extends from org.springframework.web.servlet.mvc.SimpleFormCont roller.

    Looking furthur at our code I think the problem is in some lovely reflection code attempting to determine the appropiate getter method for some data.
    All private methods and no tests - lovely.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •