[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jaxme-dev
Subject:    Dynamic proxy returns List<Object[]> instead of List<List<Integer>>
From:       Oliver Archner <oliver.archner () bayceer ! uni-bayreuth ! de>
Date:       2012-11-16 10:22:57
Message-ID: 50A61401.1060004 () bayceer ! uni-bayreuth ! de
[Download RAW message or body]

Hello,

the concept of dynamic proxies looks great and i decided to give it a 
try with the following interface and implementation:

public interface Handler {
         List<Map<String, String>> getMap(Integer id);
         List<List<Integer>> getList(Integer a);
}

public class HandlerImpl implements Handler {
     @Override
     public List<Map<String,String>> getMap(Integer id) {
         List<Map<String,String>> l = new ArrayList<Map<String,String>>(3);

         Map<String,String> r = new Hashtable<String, String>(2);
         r.put("Sample 1", String.valueOf(0));
         r.put("Sample 2", String.valueOf(299));
         l.add(r);

         r = new Hashtable<String, String>(2);
         r.put("Sample 1", String.valueOf(0));
         r.put("Sample 2", String.valueOf(299));
         l.add(r);

         r = new Hashtable<String, String>(2);
         r.put("Sample 1", String.valueOf(0));
         r.put("Sample 2", String.valueOf(299));
         l.add(r);
         return l;
     }

     @Override
     public List<List<Integer>> getList(Integer i) {
         List<List<Integer>> l = new ArrayList<List<Integer>>(1);
         List<Integer> r = new ArrayList<Integer>(2);
         r.add(Integer.valueOf(20));
         r.add(Integer.valueOf(10));
         l.add(r);
         return l;
     }

My test for method getMap runs without errors and without any casting:

@Test A
     public void getMapTypeFactory() {
         try {
             ClientFactory factory = new ClientFactory(client);
             Handler zm = (Handler) 
factory.newInstance(ClassLoader.getSystemClassLoader(), 
Handler.class,"Handler");
             List<Map<String,String>> o = zm.getMap(1);
             for (Map<String, String> m : o) {
                 System.out.print("|");
                 System.out.print(m);
             }
         } catch (Exception e) {
             fail(e.getMessage());
         }
     }

I decided to write a test for the getList method as well:

@Test B
     public void getListTypeFactory() {
         ClientFactory factory = new ClientFactory(client);
         Handler zm = (Handler) 
factory.newInstance(ClassLoader.getSystemClassLoader(), 
Handler.class,"Handler");
                 List<List<Integer>> o = zm.getList(1);
                 for (List<Integer> m : o) {
                    System.out.print("|");
                    System.out.print(m);
                 }
    }

It throws a casting exception because zm.getList(1) returns: 
List<Object[]> and not as expected: List<List<Integer>>.
I already know that there is a workaround for this problem, but i would 
prefer the usage of factories and the provided type converters.
Can someone explain me why test case B fails and A not ? Is it a problem 
because of the nested types ?

Oliver







---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ws.apache.org
For additional commands, e-mail: dev-help@ws.apache.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic