Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hello,
I am using tjavaRow,
I have many output_row starting with VALEURUNITE, inspite of writing many times output_row.VALEURUNITE**** = input_row.VALEURUNITE
I looped, but I didin't knew how to edit the output_row value :
for ( java . lang . reflect . Field field: output_row.getClass().getDeclaredFields()) {
if (field.getName().matches("VALEURUNITE.*")) {
output_row.[field.getName()] = input_row.valeur_unite_tmp;
}
}
Can you help me by telling me how to implement the output_row values dynamically?
An even better solution. All of my fields are called "newColumn", "newColumn1", etc, etc. In this example I am changing just one column called "newColumn".
output_row.newColumn = input_row.newColumn; output_row.newColumn1 = input_row.newColumn1; output_row.newColumn2 = input_row.newColumn2; output_row.newColumn3 = input_row.newColumn3; int count = 0; for (java.lang.reflect.Field field: output_row.getClass().getDeclaredFields()) { if(count>1){ System.out.println(field.getName()); if(field.getName().compareToIgnoreCase("newColumn")==0){ field.set(output_row,"TEST123"); }else{ field.set(output_row,field.get(output_row)); } } count++; }
It is not entirely clear what you are looking to do here. Can you elaborate please?
Ah I see. There are a couple of hacks to do this. See below an example of how to set every column to the same value without errors. I am assuming you can extrapolate to include your logic....
int count = 0; for (java.lang.reflect.Field field: output_row.getClass().getDeclaredFields()) { if(count>1){ field.set(output_row,"TEST123"); } count++; }
What the above does is very similar to your base code. I am skipping the first two fields because they are hidden fields that are not used,. Trying to set those will cause an error. The subsequent fields I know (in this example) are all Strings.
An even better solution. All of my fields are called "newColumn", "newColumn1", etc, etc. In this example I am changing just one column called "newColumn".
output_row.newColumn = input_row.newColumn; output_row.newColumn1 = input_row.newColumn1; output_row.newColumn2 = input_row.newColumn2; output_row.newColumn3 = input_row.newColumn3; int count = 0; for (java.lang.reflect.Field field: output_row.getClass().getDeclaredFields()) { if(count>1){ System.out.println(field.getName()); if(field.getName().compareToIgnoreCase("newColumn")==0){ field.set(output_row,"TEST123"); }else{ field.set(output_row,field.get(output_row)); } } count++; }
looks intresting approach @chou
can you please let us know why you are trying to do this ? what is your complete use case?
I am trying to do this, inspite of writing output_row many times for each case, here is the solution and it is woring great thanks to @rhall :
int i = 0;
for (java.lang.reflect.Field field: output_row.getClass().getDeclaredFields()) {
if (i>1){
if (field.getName().equalsIgnoreCase("VALEUR_" + input_row.code)){
field.set(output_row,input_row.valeur_tmp);
}
else if (field.getName().equalsIgnoreCase("LIBELLE_" + input_row.code)){
field.set(output_row,input_row.Libelle_tmp);
}
else {
field.set(output_row,"");
}
}
i++;
}