It's a system repair job, but it's been a while since Java. The environment is Java 7.
FugaName1 to FugaName4 in a certain table column will be expanded to FugaName1 to FugaName50. I suggested that if you want to increase it to that extent, it would be better to use a separate table, but considering the usage situation, it is not necessary to separate the tables.
The following Enum class was created to output to a file. The constructor was added on 2017/06/11.
Enum class
public class Hoge {
public String no;
public String fuganame1;
public String fuganame2;
public String fuganame3;
public String fuganame4;
//constructor
public Hoge(String _no, HashMap<String, String> resultList) {
no = _no;
fuganame1 = resultList.get(dataParams.FugaName1.getColNamePhysical());
fuganame2 = resultList.get(dataParams.FugaName2.getColNamePhysical());
fuganame3 = resultList.get(dataParams.FugaName3.getColNamePhysical());
fuganame4 = resultList.get(dataParams.FugaName4.getColNamePhysical());
}
public enum dataParams{
No ("no" , "No" , 0)
, FugaName1 ("fuganame1" , "Fuga Name1" , 1)
, FugaName2 ("fuganame2" , "Fuga Name2" , 2)
, FugaName3 ("fuganame3" , "Fuga Name3" , 3)
, FugaName4 ("fuganame4" , "Fuga Name4" , 4);
private final String colNamePhysical; //Physical name
private final String colNameLogical; //Logical name
private final int colIdx;
private dataParams( String physical , String logical, int index) {
colNamePhysical = physical;
colNameLogical = logical;
colIdx = index;
}
public String getColNamePhysical() {
return colNamePhysical;
}
public String getColNameLogical() {
return colNameLogical;
}
public int getColIdx() {
return colIdx;
}
}
}
Since it is about four, it is written solidly.
Where to use
String fugaName1 = (String)Hoge.dataParams.FugaName1.getColNamePhysical();
addMap.put(fmColumnName, fugaName1);
resultList.add(addMap);
String fugaName2 = (String)Hoge.dataParams.FugaName2.getColNamePhysical();
addMap.put(fmColumnName, fugaName2);
resultList.add(addMap);
String fugaName3 = (String)Hoge.dataParams.FugaName3.getColNamePhysical();
addMap.put(fmColumnName, fugaName3);
resultList.add(addMap);
String fugaName4 = (String)Hoge.dataParams.FugaName4.getColNamePhysical();
addMap.put(fmColumnName, fugaName4);
resultList.add(addMap);
If it's still four, it's acceptable even if it's solid, but I don't think it's cool as a programmer to write 50, and it seems that there are several other people doing the same thing, so in the future Thinking about that, I tried reflection. This is my first time to use reflection, so I will make a confirmation program.
Experiment
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static void main(String []args) {
Enum<?> clz = Enum.valueOf(Hoge.dataParams.class, "FugaName2");
Object[] consts = clz.getClass().getEnumConstants();
Class<?> sub = consts[0].getClass();
Method mth = sub.getDeclaredMethod("getColNameLogical"); //getColNamePhysical");
String val = (String) mth.invoke(consts[clz.ordinal()]);
System.out.println(val); //result: Fuga Name2
}
}
For the Enum class, I increased it to 50 for the time being. Regarding the place of use, it corresponded by reflection.
Where to use
Class<?> clz = Hoge.dataParams.class;
Object[] consts = clz.getEnumConstants();
Class<?> sub = consts[0].getClass();
try {
Method mth = sub.getDeclaredMethod("getColNamePhysical");
for(int i=1; i<=50; i++){
Enum<?> v = Enum.valueOf(Hoge.dataParams.class, String.format("FugaName%d", i));
String fugaName = (String) mth.invoke(consts[v.ordinal()]);
addMap.put(fmColumnName, fugaName);
resultList.add(addMap);
}
} catch (NoSuchMethodException|SecurityException|IllegalAccessException|InvocationTargetException ex) {
throw ex;
}
I received a comment from Mr. saka1029 and learned how to prevent reflection.
Where to use
for (Hoge.dataParams v : Hoge.dataParams.values()) {
if (!v.toString().matches("FugaName\\d+"))
continue;
String fugaName = v.getColNamePhysical();
addMap.put(fmColumnName, fugaName);
resultList.add(addMap);
}
At the constructor, getField (field name) is used to get the field variable. In that case, throws IllegalArgumentException, IllegalAccessException will be added.
See also: Differences between getFields and getDeclaredFields
Enum class
//constructor
public Hoge(String _no, HashMap<String, String> resultList) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
no = _no;
//Same thing as below FugaName1-Do up to 50
// fuganame1 = resultList.get(dataParams.FugaName1.getColNamePhysical());
for (Hoge.dataParams v : Hoge.dataParams.values()) {
String name = v.toString();
if (!name.matches("FugaName\\d+"))
continue;
Field field = this.getClass().getField(name.toLowerCase());
field.set(this, resultList.get(v.getColNamePhysical()));
}
}
Looking at the Java API specification, there was a statement that the order was certainly not fixed. Class#getFields()
Returns an array holding a Field object that reflects all accessible public fields of the class or interface represented by this Class object. The elements in the returned array are never sorted or in any particular order.
Since it is not possible to acquire field information in the order in which fields are defined with standard Java functions, the order is specified by using annotations. Reference: Print fields in order of annotation value
It's my first time to do a full-scale renovation work in Java, so there may actually be a better way.
Recommended Posts