@Test public void testToObjectArrayObjects() throws Exception { Object [] objarr = ObjectUtil.toObjectArray(new String[] { "one", "two" }); assertEquals("one", objarr[0]); assertEquals("two", objarr[1]); objarr = ObjectUtil.toObjectArray(new int[] { 1, 2 }); assertEquals("1", objarr[0]); assertEquals("2", objarr[1]); }
public static Object[] toObjectArray(final Object obj) { if (obj == null) { return new Object[]{ null }; } if (obj.getClass().isPrimitive() || obj.getClass().getName().startsWith("java.lang") || obj.getClass().isArray()) { return escapeXml(obj); } final Field[] fields = obj.getClass().getDeclaredFields(); final Object[] rez = new Object[fields.length]; for (int i = 0; i < fields.length; i++) { if (Modifier.isStatic(fields[i].getModifiers())) { rez[i] = "Static: " + fields[i].getName(); continue; } if (!fields[i].isAccessible()) { fields[i].setAccessible(true); } try { final Object value = fields[i].get(obj); if (value instanceof HibernateProxy) { rez[i] = ((HibernateProxy) value).getHibernateLazyInitializer().getIdentifier() + ": " + fields[i].getName() + " (H)"; } else if (value instanceof Auditable) { rez[i] = ((Auditable) value).getId() + ": " + fields[i].getName(); } else if (value instanceof PersistentCollection) { rez[i] = "Collection: " + fields[i].getName() + " (H)"; } else if (value instanceof Collection) { rez[i] = "Collection: " + fields[i].getName(); } else if (value instanceof Map) { rez[i] = "Map: " + fields[i].getName(); } else if (value != null) { rez[i] = StringEscapeUtils.escapeXml(String.valueOf(value)); } } catch (Exception ex) { rez[i] = "Error: " + fields[i].getName() + ", cause: " + ex.getMessage(); } } return rez; }
ObjectUtil { public static Object[] toObjectArray(final Object obj) { if (obj == null) { return new Object[]{ null }; } if (obj.getClass().isPrimitive() || obj.getClass().getName().startsWith("java.lang") || obj.getClass().isArray()) { return escapeXml(obj); } final Field[] fields = obj.getClass().getDeclaredFields(); final Object[] rez = new Object[fields.length]; for (int i = 0; i < fields.length; i++) { if (Modifier.isStatic(fields[i].getModifiers())) { rez[i] = "Static: " + fields[i].getName(); continue; } if (!fields[i].isAccessible()) { fields[i].setAccessible(true); } try { final Object value = fields[i].get(obj); if (value instanceof HibernateProxy) { rez[i] = ((HibernateProxy) value).getHibernateLazyInitializer().getIdentifier() + ": " + fields[i].getName() + " (H)"; } else if (value instanceof Auditable) { rez[i] = ((Auditable) value).getId() + ": " + fields[i].getName(); } else if (value instanceof PersistentCollection) { rez[i] = "Collection: " + fields[i].getName() + " (H)"; } else if (value instanceof Collection) { rez[i] = "Collection: " + fields[i].getName(); } else if (value instanceof Map) { rez[i] = "Map: " + fields[i].getName(); } else if (value != null) { rez[i] = StringEscapeUtils.escapeXml(String.valueOf(value)); } } catch (Exception ex) { rez[i] = "Error: " + fields[i].getName() + ", cause: " + ex.getMessage(); } } return rez; } }
ObjectUtil { public static Object[] toObjectArray(final Object obj) { if (obj == null) { return new Object[]{ null }; } if (obj.getClass().isPrimitive() || obj.getClass().getName().startsWith("java.lang") || obj.getClass().isArray()) { return escapeXml(obj); } final Field[] fields = obj.getClass().getDeclaredFields(); final Object[] rez = new Object[fields.length]; for (int i = 0; i < fields.length; i++) { if (Modifier.isStatic(fields[i].getModifiers())) { rez[i] = "Static: " + fields[i].getName(); continue; } if (!fields[i].isAccessible()) { fields[i].setAccessible(true); } try { final Object value = fields[i].get(obj); if (value instanceof HibernateProxy) { rez[i] = ((HibernateProxy) value).getHibernateLazyInitializer().getIdentifier() + ": " + fields[i].getName() + " (H)"; } else if (value instanceof Auditable) { rez[i] = ((Auditable) value).getId() + ": " + fields[i].getName(); } else if (value instanceof PersistentCollection) { rez[i] = "Collection: " + fields[i].getName() + " (H)"; } else if (value instanceof Collection) { rez[i] = "Collection: " + fields[i].getName(); } else if (value instanceof Map) { rez[i] = "Map: " + fields[i].getName(); } else if (value != null) { rez[i] = StringEscapeUtils.escapeXml(String.valueOf(value)); } } catch (Exception ex) { rez[i] = "Error: " + fields[i].getName() + ", cause: " + ex.getMessage(); } } return rez; }  }
ObjectUtil { public static Object[] toObjectArray(final Object obj) { if (obj == null) { return new Object[]{ null }; } if (obj.getClass().isPrimitive() || obj.getClass().getName().startsWith("java.lang") || obj.getClass().isArray()) { return escapeXml(obj); } final Field[] fields = obj.getClass().getDeclaredFields(); final Object[] rez = new Object[fields.length]; for (int i = 0; i < fields.length; i++) { if (Modifier.isStatic(fields[i].getModifiers())) { rez[i] = "Static: " + fields[i].getName(); continue; } if (!fields[i].isAccessible()) { fields[i].setAccessible(true); } try { final Object value = fields[i].get(obj); if (value instanceof HibernateProxy) { rez[i] = ((HibernateProxy) value).getHibernateLazyInitializer().getIdentifier() + ": " + fields[i].getName() + " (H)"; } else if (value instanceof Auditable) { rez[i] = ((Auditable) value).getId() + ": " + fields[i].getName(); } else if (value instanceof PersistentCollection) { rez[i] = "Collection: " + fields[i].getName() + " (H)"; } else if (value instanceof Collection) { rez[i] = "Collection: " + fields[i].getName(); } else if (value instanceof Map) { rez[i] = "Map: " + fields[i].getName(); } else if (value != null) { rez[i] = StringEscapeUtils.escapeXml(String.valueOf(value)); } } catch (Exception ex) { rez[i] = "Error: " + fields[i].getName() + ", cause: " + ex.getMessage(); } } return rez; }  static String dump(Map<?, ?> map); static Object[] toObjectArray(final Object obj); static List<Object[]> transformTypedResultListToArrayList(List listOfObjects); static Object[] escapeXml(Object raw); }
ObjectUtil { public static Object[] toObjectArray(final Object obj) { if (obj == null) { return new Object[]{ null }; } if (obj.getClass().isPrimitive() || obj.getClass().getName().startsWith("java.lang") || obj.getClass().isArray()) { return escapeXml(obj); } final Field[] fields = obj.getClass().getDeclaredFields(); final Object[] rez = new Object[fields.length]; for (int i = 0; i < fields.length; i++) { if (Modifier.isStatic(fields[i].getModifiers())) { rez[i] = "Static: " + fields[i].getName(); continue; } if (!fields[i].isAccessible()) { fields[i].setAccessible(true); } try { final Object value = fields[i].get(obj); if (value instanceof HibernateProxy) { rez[i] = ((HibernateProxy) value).getHibernateLazyInitializer().getIdentifier() + ": " + fields[i].getName() + " (H)"; } else if (value instanceof Auditable) { rez[i] = ((Auditable) value).getId() + ": " + fields[i].getName(); } else if (value instanceof PersistentCollection) { rez[i] = "Collection: " + fields[i].getName() + " (H)"; } else if (value instanceof Collection) { rez[i] = "Collection: " + fields[i].getName(); } else if (value instanceof Map) { rez[i] = "Map: " + fields[i].getName(); } else if (value != null) { rez[i] = StringEscapeUtils.escapeXml(String.valueOf(value)); } } catch (Exception ex) { rez[i] = "Error: " + fields[i].getName() + ", cause: " + ex.getMessage(); } } return rez; }  static String dump(Map<?, ?> map); static Object[] toObjectArray(final Object obj); static List<Object[]> transformTypedResultListToArrayList(List listOfObjects); static Object[] escapeXml(Object raw);  }