import com.ablia.contab.DocumentoCR; import com.ablia.db.CRAdapter; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.Date; import java.sql.Time; public class TestReflection { public static void main(String[] args) { DocumentoCR CR = new DocumentoCR(); CR.setFlgClienteFornitore("99"); CR.setDataChiusuraA(new Date(0L)); Field[] ff1 = CR.getClass().getDeclaredFields(); for (int i = 0; i < ff1.length; i++) { String name = ff1[i].getName(); String functionName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); Object valueUpd = getFunctionName(functionName, ff1[i].getType(), CR); if (isNotEmpty(valueUpd, ff1[i].getType())) { System.out.print(String.valueOf(ff1[i].getName()) + ": "); System.out.println(valueUpd); } } } private static boolean isNotEmpty(Object valueUpd, Class tipo) { boolean ret = false; if (valueUpd != null) { String sclass = valueUpd.getClass().getName(); if (sclass.indexOf("Long") > 0 || sclass.indexOf("Double") > 0 || sclass.indexOf("String") > 0 || sclass.indexOf("Date") > 0 || sclass.indexOf("Time") > 0) if (tipo.getName() == "long") { if ((Long)valueUpd == 0L || (Long)valueUpd == -1L) { ret = false; } else { ret = true; } } else if (tipo.getName() == "double") { if ((Double)valueUpd == 0.0D) { ret = false; } else { ret = true; } } else if (tipo.getName().indexOf("String") > 0) { if (((String)valueUpd).equals("") || ((String)valueUpd).isEmpty()) { ret = false; } else { ret = true; } } else if (tipo.getName().indexOf("Date") > 0) { Date data = (Date)valueUpd; if (data == null) { ret = false; } else { ret = true; } } else if (tipo.getName().indexOf("Time") > 0) { if ((Time)valueUpd == null) { ret = false; } else { ret = true; } } } return ret; } private static Object getFunctionName(String functionName, Class tipo, CRAdapter thiss) { Object value = null; try { Method method = thiss.getClass().getMethod(functionName); value = method.invoke(thiss); } catch (SecurityException e) { } catch (NoSuchMethodException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) {} return value; } }