java reflexes

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("")+1)); Method[] methods = tempClazz.getDeclaredMethods(); List list = Arrays.asList(tempClazz.getEnumConstants()); Object [] tempCodeEnumArray = new String[] {}; List tempCodeEnumList = new ArrayList (); for(Object enu : list){ for(Method method : methods){ if(method.getName().startsWith("getV")){ tempCodeEnumList.add(method.invoke(enu).toString()); // System.out.println(method.invoke(enu)); } } } tempCodeEnumArray = tempCodeEnumList.toArray(); codeEnumMap.put(className.substring(className.indexOf("")+1), tempCodeEnumArray);

// 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

java reflexes
Java
Java
Java learning (0)
Studying Java ―― 3
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)
JAVA (Map)
[java] interface
Java9 collection
Java basics
Java methods
Java diary