package javaHansya.main.test;
import java.util.ArrayList; import java.util.List;
public class TestMain {
public static List<String[]> csvList;
public static void main(String[] args) {
// String ty = "01"; // if (ty == CodeEnum.YOMIURI_SB.getValue()) // System.out.println("OK"); // else // System.out.println("NG"); // System.out.println(CodeEnum.getCodeEnum("SB"));
// csv data initialization initCsv();
// java reflex test CheckUtil ck = CheckUtil.getInstance(); CsvDataCheckFormat cf = new CsvDataCheckFormat (); try { ck.checkCsvData (csvList, cf); } catch (IllegalArgumentException e) { // TODO auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO auto-generated catch block e.printStackTrace(); } }
private static void initCsv() {
// TODO auto-generated method stub String [] s1 = new String [] {"", "0", "Kanji"}; String [] s2 = new String[] {"01","9",""}; String [] s3 = new String [] {"05", "", "1 2 3 4 5 6"}; csvList = new ArrayList<String[]> (); csvList.add(s1); csvList.add(s2); csvList.add(s3); }
}
package javaHansya.main.test;
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List;
public class CheckUtil { private static CheckUtil INSTANCE = null; public static HashMap<String, Object[]> codeEnumMap = null;
public static CheckUtil getInstance() {
if(INSTANCE == null) {
INSTANCE = new CheckUtil();
}
return INSTANCE;
}
private CheckUtil() {
try {
getCodeEnum();
} catch (NoSuchMethodException e) {
// TODO auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO auto-generated catch block e.printStackTrace(); } }
public void getCodeEnum() throws NoSuchMethodException, SecurityException, InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Get CodeEnum target
// CodeEnum ce = new CodeEnum ();
// Class<?> codeEnumClazz = ce.getClass();
Class codeEnumClazz = CodeEnum.class;
CodeEnum ce = (CodeEnum) codeEnumClazz.newInstance();
@SuppressWarnings("rawtypes")
Class[] innerClazz = codeEnumClazz.getDeclaredClasses();
if (codeEnumMap == null) {
codeEnumMap = new HashMap<String,Object[]> ();
}
for (@SuppressWarnings("rawtypes")Class tempClazz : innerClazz) {
// Method method = tempClazz.getMethod("values");
// Object oj = method.invoke(null);
String className = tempClazz.getName();
// System.out.println(className.substring(className.indexOf("
// int i = tempClazz.getModifiers(); // String s = Modifier.toString(i); // Class tempEnumClazz = null; // if (s.contains("static")) { // tempEnumClazz = (Class) tempClazz.getConstructor().newInstance(); // } else { // tempEnumClazz = (Class) tempClazz.getConstructor(codeEnumClazz.getClass()).newInstance(codeEnumClazz); // } // // Method method2 = tempEnumClazz.getMethod("getValue"); // Object oj2 = method2.invoke(null); }
String m = "";
System.out.println(m);
// Field[] codeEnumField = codeEnumClazz.getDeclaredFields(); // String s = "";
}
public boolean checkCsvData(List<String[]> csvList, Object obj)
throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException, SecurityException {
// Get CheckFormat target Class<?> checkClazz = obj.getClass(); Field[] checkField = checkClazz.getFields();
int dataCnt = 0;
for (String[] tempData : csvList) {
int colCnt = 0;
while (colCnt < tempData.length) {
String[] colFormat = (String[]) checkField[colCnt].get(obj);
// 1. need to check if (!"".equals(colFormat[1])) { if (tempData[colCnt] == null || "".equals(tempData[colCnt])) { System.out.println (colFormat [0] + "is null;" + (dataCnt + 1) + "line"); colCnt++; continue; } } // 2. Type / digit check String[] typeCheck = colFormat[2].split(","); for (String tempTypeCheck : typeCheck) { switch (tempTypeCheck) { case "isNum": isNum(tempData[colCnt], colFormat[3], colFormat[4]); break; case "isNumAlp": isNumAlp(tempData[colCnt], colFormat[3], colFormat[4]); break; case "isKanji": isKanji(tempData[colCnt], colFormat[3], colFormat[4]); break; } }
// 3. Code value content check if (!"".equals(colFormat[5])) { Object[] codeEnumArray = codeEnumMap.get(colFormat[5]); boolean codeErr = true; for (Object oj : codeEnumArray) { if (tempData[colCnt].equals(oj)) { codeErr = false; } } if (codeErr) System.out.println ("The code value does not match the value defined in CodeNum." + (DataCnt + 1) + "line"); }
//System.out.println(colFormat);
colCnt++;
}
dataCnt++;
}
return false;
}
private void isKanji(String columData, String min, String max) {
// TODO auto-generated method stub
}
private void isNumAlp(String columData, String min, String max) {
// TODO auto-generated method stub
}
private void isNum(String columData, String min, String max) {
// TODO auto-generated method stub
}
}
package javaHansya.main.test;
public class CsvDataCheckFormat {
public final String [] colum1 = {"Yomiuri Shimbun Headquarters Code", "Required", "isNum", "1", "3", "YOMIURI"}; public final String [] colum2 = {"test code", "required", "isNumAlp, isTel", "1", "1", "TESTCODE"}; public final String [] colum3 = {"Test Kanji", "", "isKanji", "1", "5", ""}; }
package javaHansya.main.test;
public final class CodeEnum {
// Yomiuri Shimbun Headquarters Code public static enum YOMIURI { YOMIURI_TY("TY","01"), YOMIURI_OS("OS","02"), YOMIURI_SB("SB","03");
private String key;
private String value;
YOMIURI (String key, String value) {
this.key = key;
this.value = value;
}
public static YOMIURI getYomiuri (String key) {
for (YOMIURI ce : YOMIURI.values()) {
if (ce.getKey() == key) {
return ce;
}
}
return null;
}
public String getKey() { return key;}
public String getValue() { return value;}
}
// Test code 1 public static enum TESTCODE { TESTCODE_TY("TY","0"), TESTCODE_OS("OS","N"), TESTCODE_SB("SB","8");
private String key;
private String value;
TESTCODE (String key, String value) {
this.key = key;
this.value = value;
}
public static TESTCODE getTestCode (String key) {
for (TESTCODE ce : TESTCODE.values()) {
if (ce.getKey() == key) {
return ce;
}
}
return null;
}
public String getKey() { return key;}
public String getValue() { return value;}
}
}
Recommended Posts