ClassCastException using Class.cast using Generics

morsif

New Member
I am writing a generic method which will validate a property by trying a class.cast on it but I keep getting a ClassCastException... Class to Test\[code\]public <T> T get(Properties p, String propKey, Class<T> clazz) throws Exception { T val = null; Object propValue = http://stackoverflow.com/questions/15646468/p.get(propKey); if(propValue== null) { throw new Exception("Property (" + propKey + ") is null"); } try { val = clazz.cast(propValue); // MARKER } catch(Exception e) { throw new Exception("Property (" + propKey + ") value is invalid value of type (" + clazz + ")", e); } return val;}\[/code\]... Test Class\[code\]@Beforepublic void setUp() { propUtil = new PropUtil(); properties = new Properties(); properties.setProperty("test.int.prop", "3");}@Testpublic void testGet() { try { assertEquals(new Integer(3), propUtil.get(properties, "test.int.prop", Integer.class)); } catch (Exception e) { System.out.println(e); }}\[/code\]The code at commented at MARKER is causing the ClassCastException.Any ideas much appreciated.
 
Back
Top