@Test(expectedExceptions = RuntimeException.class) public void testGetInt() { Integer result = ValueUtil.getInt(null); Assert.assertNull(result); result = ValueUtil.getInt("100"); Assert.assertEquals(result, new Integer(100)); result = ValueUtil.getInt(new Long(100)); Assert.assertEquals(result, new Integer(100)); ValueUtil.getInt(new Object()); }
public static Integer getInt(Object obj) { if(obj == null) { return null; } else if(obj instanceof String) { return Integer.valueOf((String) obj); } else { try { Method method = obj.getClass().getMethod("intValue"); return (int) method.invoke(obj); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new RuntimeException("Unable to convert " + obj + " into Interger",e); } } }
ValueUtil { public static Integer getInt(Object obj) { if(obj == null) { return null; } else if(obj instanceof String) { return Integer.valueOf((String) obj); } else { try { Method method = obj.getClass().getMethod("intValue"); return (int) method.invoke(obj); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new RuntimeException("Unable to convert " + obj + " into Interger",e); } } } }
ValueUtil { public static Integer getInt(Object obj) { if(obj == null) { return null; } else if(obj instanceof String) { return Integer.valueOf((String) obj); } else { try { Method method = obj.getClass().getMethod("intValue"); return (int) method.invoke(obj); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new RuntimeException("Unable to convert " + obj + " into Interger",e); } } }  }
ValueUtil { public static Integer getInt(Object obj) { if(obj == null) { return null; } else if(obj instanceof String) { return Integer.valueOf((String) obj); } else { try { Method method = obj.getClass().getMethod("intValue"); return (int) method.invoke(obj); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new RuntimeException("Unable to convert " + obj + " into Interger",e); } } }  static Integer getInt(Object obj); }
ValueUtil { public static Integer getInt(Object obj) { if(obj == null) { return null; } else if(obj instanceof String) { return Integer.valueOf((String) obj); } else { try { Method method = obj.getClass().getMethod("intValue"); return (int) method.invoke(obj); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new RuntimeException("Unable to convert " + obj + " into Interger",e); } } }  static Integer getInt(Object obj);  }