1 Reply Latest reply on May 31, 2006 2:38 PM by Brian Stansberry

    Classes incorrectly casted using a HashMap

    Peter Gymoese Newbie

      I'm using JBossCache for a product in Hewlett Packard and here I have seen a strange issue that I have not been able to resolve.

      The problem is that I have a HashMap wrapped in a annotated class that also holds some additional information. When I draw the keyset from the HashMap and iterate over the keys I see is that the type of the keys entered have changed to String.

      I positively know that I put a Long as the key in the HashMap, however when I iterate usign the set returned from the keyset all the keys have "changed" type to String.

      I have created a small test application that I have entered into the examples directory in the JBossCache 1.3.0.SP1 kit that I have downloaded. I have made a copy of the annotated50 example and modified this to present my problem to ensure that I have no impact from "other" sources.

      I run with Suns 1.5_06 Java

      The code for the "main" part of the application is very simple and looks like this. The result of the code is that the text "This is a String" is printed on stdout.

       try {
       distributedInfo = new TreeCacheAop();
       PropertyConfigurator config = new PropertyConfigurator();
       config.configure(distributedInfo, "META-INF/replSync-service.xml");
       distributedInfo.setClusterName("ClassCastIssue");
       distributedInfo.start();
       } catch (Exception e) {
       e.printStackTrace();
       }
      
       // Add the map container element for the distribution
       try {
       map = new CachedMap();
       distributedInfo.putObject(NODE_JOB_RELATION_CACHE_PATH, map);
       } catch (CacheException e) {
       e.printStackTrace();
       }
      
       map.put(new Long(123), new Integer(234));
      
       Iterator i = map.keySet().iterator();
      
       Object obj = i.next();
      
       System.out.println("Class name = " + obj.getClass().getName());
      
       if (obj instanceof String) {
       System.out.println("This is a String");
       }
      
       if (obj instanceof Long) {
       System.out.println("This is a Long");
       }
      


      The code for the annotated wrapper class including the map looks like this:

      @org.jboss.cache.aop.annotation.PojoCacheable
      public class CachedMap
      {
       private HashMap map = new HashMap();
      
       public void put(Long id, Object bean)
       {
       map.put(id, bean);
       }
      
       public Object get(Long id)
       {
       return map.get(id);
       }
      
       public void remove(Long id)
       {
       map.remove(id);
       }
      
       public Set keySet()
       {
       return map.keySet();
       }
      }
      


      Can anyone tell me what is happening here - am I doing anything wrong - to me this mostly seems like a bug!

      Regards

      Peter