自らIOCとMVCを実現する(五)


プロジェクトで使用されるいくつかのツールクラスについて説明します.
① BeanUtils.JAvaのjava反射で動作するいくつかのパッケージ
package com.ajunframework.beans.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @author ajun
 * @http://blog.csdn.net/ajun_studio
 */
public class BeanUtils {
	
	/**
	 *  class 
	 * @param <T>
	 * @param clazz Person.class
	 * @return
	 */
	public static <T> T instanceClass(Class<T> clazz){
		if(!clazz.isInterface()){
			try {
				return clazz.newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 *  
	 * @param <T>
	 * @param ctor
	 * @param args
	 * @return
	 * @throws IllegalArgumentException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static <T> T instanceClass(Constructor<T> ctor, Object... args) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException{
		makeAccessible(ctor);
		return ctor.newInstance(args);// 
	}
	
	/**
	 *  class 
	 * @param clazz
	 * @param methodName
	 * @param paramTypes
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 */
	public static  Method findMethod(Class<?> clazz, String methodName, Class<?>... paramTypes){
		try {
			return clazz.getMethod(methodName, paramTypes);
		} catch (NoSuchMethodException e) {
			return findDeclaredMethod(clazz, methodName, paramTypes);// 
		}
	}
	
	public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class<?>[] paramTypes){
		try {
			return clazz.getDeclaredMethod(methodName, paramTypes);
		}
		catch (NoSuchMethodException ex) {
			if (clazz.getSuperclass() != null) {
				return findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes);
			}
			return null;
		}
	}
	
	
	public static Method [] findDeclaredMethods(Class<?> clazz){
			return clazz.getDeclaredMethods();
	}
	
	public static void makeAccessible(Constructor<?> ctor) {
		if ((!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers()))
				&& !ctor.isAccessible()) {
			ctor.setAccessible(true);//   true  
		}
	}
	
	
	public static Field[] findDeclaredFields(Class<?> clazz){
		return clazz.getDeclaredFields();
	}
	
}

②BeanWrapper.JAvaはbeanの属性値を設定するために使用されます
package com.ajunframework.beans.utils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author ajun
 * @http://blog.csdn.net/ajun_studio
 */
public class BeanWrapper {
	
	private Object object;
	

	public BeanWrapper(Object object) {
		super();
		this.object = object;
	}
	
	
	public void setPropertyValue(String name,Object value){
			try {
				PropertyDescriptor pd = new PropertyDescriptor(name,this.object.getClass());
				Method setMethod = pd.getWriteMethod();
			    setMethod.invoke(object, value);
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}catch (IntrospectionException e) {
				e.printStackTrace();
			}
	}
	
	public Object getPropertyValue(String name){
		Object value=null;
		try {
			PropertyDescriptor pd = new PropertyDescriptor(name,this.object.getClass());
			Method getMethod = pd.getReadMethod();
			value = getMethod.invoke(object);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return value;
	}
}

③PubConstant.JAvaはconstantを読み出す.propertiesファイル
package com.ajunframework.beans.utils;

import java.io.IOException;
import java.util.Properties;

/**
 * @author ajun
 * @http://blog.csdn.net/ajun_studio  
 **/
public class PubConstant {

	private static Properties properties= new Properties();
	
	static{
		try {
			properties.load(PubConstant.class.getClassLoader().getResourceAsStream("constant.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String getValue(String key){
		String value = (String)properties.get(key);
		return value.trim();
	}
}

次のセクションでは、現在のiocをテストします.