범용 도구 류 Generics Utils
5843 단어 자바범 형도구 클래스GenericsUtils
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);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.