범용 도구 류 Generics Utils

package com.geostar.platform.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 *      
 */
public class GenericsUtils{
	/**
	 *     ,                  .  DaoSupport<Buyer>
	 * 
	 * @param clazz
	 *            clazz       ,          
	 * @param index
	 *                    , 0  .
	 * @return          ,       ParameterizedType  ,      ,      <code>Object.class</code>
	 */
	@SuppressWarnings("unchecked")
	public static Class getSuperClassGenricType(Class clazz, int index){
		
		Type genType = clazz.getGenericSuperclass();//       
		
		//       ParameterizedType  ,      ,    Object.class
		if (!(genType instanceof ParameterizedType)){
			
			return Object.class;
		}
		
		//               Type     ,            Class,  BuyerServiceBean extends DaoSupport<Buyer,Contact>   Buyer Contact  
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
		if (index >= params.length || index < 0){
			
			throw new RuntimeException("      " + (index < 0 ? "    0" : "        "));
		}
		if (!(params[index] instanceof Class)){
			
			return Object.class;
		}
		return (Class) params[index];
	}

	/**
	 *     ,                     .  DaoSupport<Buyer>
	 * 
	 * @param clazz
	 *            clazz       ,          
	 * @return          ,       ParameterizedType  ,      ,      <code>Object.class</code>
	 */
	@SuppressWarnings("unchecked")
	public static Class getSuperClassGenricType(Class clazz){
		
		return getSuperClassGenricType(clazz, 0);
	}

	/**
	 *     ,                .  : public Map<String, Buyer> getNames(){}
	 * 
	 * @param Method
	 *            method   
	 * @param int index         , 0  .
	 * @return          ,       ParameterizedType  ,      ,      <code>Object.class</code>
	 */
	@SuppressWarnings("unchecked")
	public static Class getMethodGenericReturnType(Method method, int index){
		
		Type returnType = method.getGenericReturnType();
		
		if (returnType instanceof ParameterizedType){
			
			ParameterizedType type = (ParameterizedType) returnType;
			Type[] typeArguments = type.getActualTypeArguments();
			
			if (index >= typeArguments.length || index < 0){
				
				throw new RuntimeException("      " + (index < 0 ? "    0" : "        "));
			}
			return (Class) typeArguments[index];
		}
		return Object.class;
	}

	/**
	 *     ,                   .  : public Map<String, Buyer> getNames(){}
	 * 
	 * @param Method
	 *            method   
	 * @return          ,       ParameterizedType  ,      ,      <code>Object.class</code>
	 */
	@SuppressWarnings("unchecked")
	public static Class getMethodGenericReturnType(Method method){
		
		return getMethodGenericReturnType(method, 0);
	}

	/**
	 *     ,         index                 .  : public void add(Map<String, Buyer> maps, List<String> names){}
	 * 
	 * @param Method
	 *            method   
	 * @param int index        
	 * @return                 ,       ParameterizedType  ,      ,         
	 */
	@SuppressWarnings("unchecked")
	public static List<Class> getMethodGenericParameterTypes(Method method, int index){
		
		List<Class> results = new ArrayList<Class>();
		Type[] genericParameterTypes = method.getGenericParameterTypes();
		
		if (index >= genericParameterTypes.length || index < 0){
			
			throw new RuntimeException("      " + (index < 0 ? "    0" : "        "));
		}
		Type genericParameterType = genericParameterTypes[index];
		
		if (genericParameterType instanceof ParameterizedType){
			
			ParameterizedType aType = (ParameterizedType) genericParameterType;
			Type[] parameterArgTypes = aType.getActualTypeArguments();
			for (Type parameterArgType : parameterArgTypes){
				Class parameterArgClass = (Class) parameterArgType;
				results.add(parameterArgClass);
			}
			return results;
		}
		return results;
	}

	/**
	 *     ,                           .  : public void add(Map<String, Buyer> maps, List<String> names){}
	 * 
	 * @param Method
	 *            method   
	 * @return                 ,       ParameterizedType  ,      ,         
	 */
	@SuppressWarnings("unchecked")
	public static List<Class> getMethodGenericParameterTypes(Method method){
		
		return getMethodGenericParameterTypes(method, 0);
	}

	/**
	 *     ,  Field         .  : public Map<String, Buyer> names;
	 * 
	 * @param Field
	 *            field   
	 * @param int index         , 0  .
	 * @return          ,       ParameterizedType  ,      ,      <code>Object.class</code>
	 */
	@SuppressWarnings("unchecked")
	public static Class getFieldGenericType(Field field, int index){
		
		Type genericFieldType = field.getGenericType();

		if (genericFieldType instanceof ParameterizedType){
			
			ParameterizedType aType = (ParameterizedType) genericFieldType;
			Type[] fieldArgTypes = aType.getActualTypeArguments();
			if (index >= fieldArgTypes.length || index < 0){
				
				throw new RuntimeException("      " + (index < 0 ? "    0" : "        "));
			}
			return (Class) fieldArgTypes[index];
		}
		return Object.class;
	}

	/**
	 *     ,  Field         .  : public Map<String, Buyer> names;
	 * 
	 * @param Field
	 *            field   
	 * @param int index         , 0  .
	 * @return          ,       ParameterizedType  ,      ,      <code>Object.class</code>
	 */
	@SuppressWarnings("unchecked")
	public static Class getFieldGenericType(Field field){
		
		return getFieldGenericType(field, 0);
	}
}

좋은 웹페이지 즐겨찾기