Grazie per la risposta Lele e scusa il ritardo.
Aggiungo qualcosa:
mi ha tratto in inganno il fatto che il metodo Field.set(Object o1, Object o2) aspettasse 2 parametri di tipo Object, come a dire "dammi qualsiasi cosa, ci penso io". In realtá conta solo il fatto che il secondo parametro possa essere effettivamente castato al tipo del primo e ció, se non erro, é possibile solo quando il secondo parametro é della stessa classe del primo, oppure quando é una sua sottoclasse o quando ne implementa l'interfaccia.
In particolare, String puó essere castato a Object ma non il contrario. In realtá davo per scontato che per me la stringa "24253" é il numero di tipo Integer di tale valore. In realtá ci sono troppe variabili da considerare e specificare, nell'assegnare il valore numerico ad una stringa, come ad es.:
- chi mi dice che quel valore é in base 10?
- chi mi dice che quella stringa non é solo una concatenazione di simboli e non ha alcun valore numerico?
Agli occhi di un umano l'associazione numero -> valore (in base 10) é automatica, a quelli di un computer no. Peró..... perché a questo punto non inserire un cast intelligente (o stupido, dipende dai punti di vista) che ci permette automaticamente di castare stringhe ad oggetti di altri tipi, cosí come un umano di primo acchitto farebbe?
Ho scritto qualcosa a tal proposito, che utilizzo nel mio programma. Magari puó interessare a qualcuno...codice:public static Object castValueToType(Class<?> type, Object valueObject) throws Exception { if (type == null) throw new IllegalArgumentException("Type cannot be null"); if (!(valueObject instanceof String)) throw new UnsupportedOperationException("Casting of not String values is not supported"); String valueString = valueObject.toString(); String fieldTypeName = type.getName(); try { if (fieldTypeName.equals("boolean")) return Boolean.parseBoolean(valueString); else if (fieldTypeName.equals("byte")) return (valueString.getBytes()[0]); else if (fieldTypeName.equals("char")) return (valueString.charAt(0)); else if (fieldTypeName.equals("double")) return Double.parseDouble(valueString); else if (fieldTypeName.equals("float")) return Float.parseFloat(valueString); else if (fieldTypeName.equals("int")) return Integer.parseInt(valueString); else if (fieldTypeName.equals("long")) return Long.parseLong(valueString); else if (fieldTypeName.equals("short")) return Short.parseShort(valueString); else if (Boolean.class.equals(type)) return Boolean.valueOf(valueString); else if (Byte.class.equals(type)) return Byte.valueOf(valueString); else if (Character.class.equals(type)) return valueString.charAt(0); else if (Double.class.equals(type)) return Double.valueOf(valueString); else if (Float.class.equals(type)) return Float.valueOf(valueString); else if (Integer.class.equals(type)) return Integer.valueOf(valueString); else if (Long.class.equals(type)) return Long.valueOf(valueString); else if (Short.class.equals(type)) return Short.valueOf(valueString); else if (String.class.equals(type)) return valueString; else if (java.math.BigDecimal.class.equals(type)) return java.math.BigDecimal.valueOf(Double.valueOf(valueString)); else if (java.math.BigInteger.class.equals(type)) return java.math.BigDecimal.valueOf(Integer.valueOf(valueString)); else if (java.sql.Date.class.equals(type)) return java.sql.Date.valueOf(valueString); else if (java.sql.Time.class.equals(type)) return java.sql.Time.valueOf(valueString); else if (java.sql.Timestamp.class.equals(type)) return java.sql.Timestamp.valueOf(valueString); else if (java.util.Date.class.equals(type)) return java.text.DateFormat.getDateInstance().parse(valueString); else throw new IllegalArgumentException("Parameter not recognized"); } catch (Throwable ex) { String message = "The value cannot be converted to the field type." + System.lineSeparator(); message += "Value: " + valueString + System.lineSeparator(); message += "Field type: " + fieldTypeName + System.lineSeparator(); message += "Exception message: " + ex.getMessage(); throw new Exception(message); } }

Rispondi quotando
