Java tutorial
/* * Copyright 2006-2007 the original author or authors. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package egovframework.rte.bat.core.reflection; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.math.BigDecimal; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.util.ReflectionUtils; /** * Reflection method * @author * @since 2012.07.20 * @version 1.0 * @see * <pre> * ?(Modification Information) * * ? ? * ------- -------- --------------------------- * 2012.07.20 ? * </pre> */ public class EgovReflectionSupport<T> { // VO Object private Object object = null; // VO? Method ? private Method[] methods; // VO? Field Method ?? Index private HashMap<String, Method> methodMap; // VO? Field private Type[] fieldType; /** * VO? Method ? get */ public HashMap<String, Method> getMethodMap() { return methodMap; } /** * Method ?(methodName) Method . * @param methods * @param methodName * @return Method */ private Method retrieveMethod(Method[] methods, String methodName) { Method method = null; for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equals(methodName)) { method = methods[i]; break; } } return method; } public EgovReflectionSupport() { super(); } /** * VO ? instance ? * @param type VO */ private void createObject(Class<?> type) { try { object = type.newInstance(); } catch (InstantiationException e) { ReflectionUtils.handleReflectionException(e); } catch (IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); } } /** * VO? Field Setter Method? ? Map? Put . * Bean ? ?. */ public void generateSetterMethodMap(Class<?> type, String[] names) throws Exception { methods = type.newInstance().getClass().getMethods(); methodMap = new HashMap<String, Method>(); for (int i = 0; i < names.length; i++) { try { String strMethod = "set" + (names[i].substring(0, 1)).toUpperCase() + names[i].substring(1); methodMap.put(names[i], retrieveMethod(methods, strMethod)); } catch (Exception e) { throw new Exception("Cannot create a method list of given : " + type.toString()); } } } /** * VO? Setter method * @param tokens VO? set ? value * @param names VO? field */ private void invokeSetterMethod(List<String> tokens, String[] names) { Method method; for (int i = 0; i < names.length; i++) { method = methodMap.get(names[i]); try { method.invoke(object, parsingFromString(tokens.get(i).trim(), fieldType[i])); } catch (IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); } catch (InvocationTargetException e) { ReflectionUtils.handleReflectionException(e); } catch (SecurityException e) { ReflectionUtils.handleReflectionException(e); } } } @SuppressWarnings("unchecked") public T generateObject(Class<?> type, List<String> tokens, String[] names) { createObject(type); invokeSetterMethod(tokens, names); return (T) object; } /** * VO? Field Getter Method? ? Map? Put . * Bean ? ?. */ public void generateGetterMethodMap(String[] names, T item) throws Exception { if (methods == null) { methods = item.getClass().getMethods(); methodMap = new HashMap<String, Method>(); try { for (int i = 0; i < names.length; i++) { String strMethod = "get" + (names[i].substring(0, 1)).toUpperCase() + names[i].substring(1); methodMap.put(names[i], retrieveMethod(methods, strMethod)); } } catch (Exception e) { throw new Exception("Cannot create a method list of given : " + item.toString()); } } } /** * item? field getter invoke * @param item VO * @param names VO? field name * @return getValue field get return */ public Object invokeGettterMethod(T item, String names) { Object value = null; try { value = methodMap.get(names).invoke(item); } catch (IllegalArgumentException e) { ReflectionUtils.handleReflectionException(e); } catch (IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); } catch (InvocationTargetException e) { ReflectionUtils.handleReflectionException(e); } return value; } /** * item? field getter invoke * @param item item VO * @param param * @param mapMethod VO? getter Map * @return field get return */ public Object invokeGettterMethod(T item, String param, Map<String, Method> mapMethod) { Object value = null; try { value = mapMethod.get(param).invoke(item); } catch (IllegalArgumentException e) { ReflectionUtils.handleReflectionException(e); } catch (IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); } catch (InvocationTargetException e) { ReflectionUtils.handleReflectionException(e); } return value; } /** * VO? field? get * @param type VO? * @param names VO field names */ public void getFieldType(Class<?> type, String[] names) { fieldType = new Type[names.length]; for (int i = 0; i < names.length; i++) { try { fieldType[i] = type.newInstance().getClass().getDeclaredField(names[i]).getType(); } catch (SecurityException e) { ReflectionUtils.handleReflectionException(e); } catch (NoSuchFieldException e) { ReflectionUtils.handleReflectionException(e); } catch (InstantiationException e) { ReflectionUtils.handleReflectionException(e); } catch (IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); } } } /** * sqlType? ? */ public String[] getSqlTypeArray(String[] params, Object item) { String[] sqlTypes = new String[params.length]; for (int i = 0; i < params.length; i++) { try { sqlTypes[i] = item.getClass().getDeclaredField(params[i]).getType().getSimpleName().toString(); } catch (SecurityException e) { ReflectionUtils.handleReflectionException(e); } catch (NoSuchFieldException e) { ReflectionUtils.handleReflectionException(e); } } return sqlTypes; } /** * String to Type parsing * @param tokenValue String ? token value * @param type VO? token value? type * @return parsingValue type paring value return */ private Object parsingFromString(String tokenValue, Type type) { Object parsingValue = null; if (type == String.class) { parsingValue = tokenValue; } else if (type == int.class) { parsingValue = Integer.parseInt(tokenValue); } else if (type == double.class) { parsingValue = Double.parseDouble(tokenValue); } else if (type == float.class) { parsingValue = Float.parseFloat(tokenValue); } else if (type == long.class) { parsingValue = Long.parseLong(tokenValue); } else if (type == char.class) { parsingValue = tokenValue.charAt(0); } else if (type == byte[].class) { parsingValue = tokenValue.getBytes(); } else if (type == boolean.class) { parsingValue = Boolean.valueOf(tokenValue); } else if (type == BigDecimal.class) { parsingValue = new BigDecimal(tokenValue); } else { parsingValue = tokenValue; } return parsingValue; } }