Java获取泛型参数实际类型

最近在优化JDBFly代码时,需要通过泛型获得实际的实体对象类型,封装了一个工具类,分享给大家,目前该工具类实现了通过子类获取父类或接口指定位置的泛型参数对应的实际参数,可以通过泛型参数名称或位置进行查找,以下为工具类代码:

package com.jianggujin.util;

import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

/**
 * 泛型参数工具
 * 
 * @author jianggujin
 *
 */
public class TypeParameterUtils {

    /**
     * 查找真实的类型参数
     * 
     * @param thisClass
     * @param parameterizedSuperclass
     * @param typeParamIndex
     * @return
     */
    public static Class<?> findTypeParameter(Class<?> thisClass, Class<?> parameterizedSuperclass, int typeParamIndex) {
        TypeVariable<?>[] typeParams = parameterizedSuperclass.getTypeParameters();
        String typeParamName = null;
        if (typeParamIndex > -1 && typeParamIndex < typeParams.length) {
            typeParamName = typeParams[typeParamIndex].getName();
        }
        // 未找到对应泛型参数
        if (typeParamName == null) {
            throw new IllegalStateException(
                    "unknown type parameter position'" + typeParamIndex + "': " + parameterizedSuperclass);
        }
        return findTypeParameter(thisClass, thisClass, parameterizedSuperclass, typeParamName, typeParamIndex);
    }

    /**
     * 查找真实的类型参数
     * 
     * @param thisClass
     * @param parameterizedSuperclass
     * @param typeParamName
     * @return
     */
    public static Class<?> findTypeParameter(Class<?> thisClass, Class<?> parameterizedSuperclass,
            String typeParamName) {
        int typeParamIndex = -1;
        TypeVariable<?>[] typeParams = parameterizedSuperclass.getTypeParameters();
        for (int i = 0; i < typeParams.length; i++) {
            if (typeParamName.equals(typeParams[i].getName())) {
                typeParamIndex = i;
                break;
            }
        }
        // 未找到对应泛型参数
        if (typeParamIndex < 0) {
            throw new IllegalStateException(
                    "unknown type parameter '" + typeParamName + "': " + parameterizedSuperclass);
        }
        return findTypeParameter(thisClass, thisClass, parameterizedSuperclass, typeParamName, typeParamIndex);
    }

    private static Class<?> findTypeParameter(Class<?> currentClass, Class<?> thisClass,
            Class<?> parameterizedSuperclass, String typeParamName, int typeParamIndex) {
        Class<?> superClass = currentClass.getSuperclass();
        if (superClass != null && parameterizedSuperclass.isAssignableFrom(superClass)) {
            if (superClass == parameterizedSuperclass) {
                return findTypeParameter(currentClass.getGenericSuperclass(), currentClass, thisClass,
                        parameterizedSuperclass, typeParamName, typeParamIndex);
            }
            return findTypeParameter(superClass, thisClass, parameterizedSuperclass, typeParamName, typeParamIndex);
        }
        Class<?>[] interfaces = currentClass.getInterfaces();
        for (int pos = 0; pos < interfaces.length; pos++) {
            Class<?> clazz = interfaces[pos];
            if (parameterizedSuperclass.isAssignableFrom(clazz)) {
                if (clazz == parameterizedSuperclass) {
                    return findTypeParameter(currentClass.getGenericInterfaces()[pos], currentClass, thisClass,
                            parameterizedSuperclass, typeParamName, typeParamIndex);
                }
                return findTypeParameter(clazz, thisClass, parameterizedSuperclass, typeParamName, typeParamIndex);
            }
        }
        return fail(thisClass, typeParamName);
    }

    private static Class<?> findTypeParameter(Type genericType, Class<?> currentClass, Class<?> thisClass,
            Class<?> parameterizedSuperclass, String typeParamName, int typeParamIndex) {
        if (!(genericType instanceof ParameterizedType)) {
            return Object.class;
        }

        Type[] actualTypeParams = ((ParameterizedType) genericType).getActualTypeArguments();

        Type actualTypeParam = actualTypeParams[typeParamIndex];
        if (actualTypeParam instanceof ParameterizedType) {
            actualTypeParam = ((ParameterizedType) actualTypeParam).getRawType();
        }
        if (actualTypeParam instanceof Class) {
            return (Class<?>) actualTypeParam;
        }
        if (actualTypeParam instanceof GenericArrayType) {
            Type componentType = ((GenericArrayType) actualTypeParam).getGenericComponentType();
            if (componentType instanceof ParameterizedType) {
                componentType = ((ParameterizedType) componentType).getRawType();
            }
            if (componentType instanceof Class) {
                return Array.newInstance((Class<?>) componentType, 0).getClass();
            }
        }
        if (actualTypeParam instanceof TypeVariable) {
            // Resolved type parameter points to another type parameter.
            TypeVariable<?> v = (TypeVariable<?>) actualTypeParam;
            currentClass = thisClass;
            if (!(v.getGenericDeclaration() instanceof Class)) {
                return Object.class;
            }

            parameterizedSuperclass = (Class<?>) v.getGenericDeclaration();
            typeParamName = v.getName();
            if (parameterizedSuperclass.isAssignableFrom(thisClass)) {
                return findTypeParameter(currentClass, thisClass, parameterizedSuperclass, typeParamName,
                        typeParamIndex);
            } else {
                return Object.class;
            }
        }
        return fail(thisClass, typeParamName);
    }

    private static Class<?> fail(Class<?> type, String typeParamName) {
        throw new IllegalStateException(
                "cannot determine the type of the type parameter '" + typeParamName + "': " + type);
    }
}

编写一个简单的单元测试查看运行结果

package test;

import java.util.Date;

import org.junit.Test;

import com.jianggujin.util.TypeParameterUtils;

public class Test{
    @Test
    public void test() {
        System.err.println(TypeParameterUtils.findTypeParameter(B.class, Parent.class, "T"));
        System.err.println(TypeParameterUtils.findTypeParameter(StringMapper.class, Mapper.class, "T"));
        System.err.println(TypeParameterUtils.findTypeParameter(M.class, Mapper.class, "T"));
        System.err.println(TypeParameterUtils.findTypeParameter(K.class, Mapper.class, "T"));
        System.err.println(TypeParameterUtils.findTypeParameter(B.class, Parent.class, 0));
        System.err.println(TypeParameterUtils.findTypeParameter(StringMapper.class, Mapper.class, 0));
        System.err.println(TypeParameterUtils.findTypeParameter(M.class, Mapper.class, 0));
        System.err.println(TypeParameterUtils.findTypeParameter(K.class, Mapper.class, 0));
    }

    static class Parent<T, P> {
    }

    static class Child<O> extends Parent<O, String> {
    }

    static class V extends Child<String> {
    }

    static class B extends V {
    }

    static interface Mapper<T> {
    }

    static interface StringMapper extends Mapper<String> {
    }

    static interface SubMapper<T> extends Mapper<T> {
    }

    static class M implements SubMapper<Date> {
    }

    static class K extends M {
    }
}

运行单元测试,会在控制台输出如下内容:

class java.lang.String
class java.lang.String
class java.util.Date
class java.util.Date
class java.lang.String
class java.lang.String
class java.util.Date
class java.util.Date

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 17中,可以使用反射来获取类型。面是一种常见的方法: 1. 首先,通过反射获取目标类的Class对象。假设目标类为`MyClass`,可以使用`MyClass.class`或者`Class.forName("com.example.MyClass")`来获取。 2. 使用`getDeclaredField`方法获取目标字段的Field对象。假设目标字段为`myField`,可以使用`Class.getDeclaredField("myField")`来获取。 3. 通过Field对象的`getGenericType`方法获取字段的类型。这将返回一个Type对象,表示字段的实际类型。 4. 如果字段的类型参数类型(即包含参数),可以通过Type对象的一些方法来获取参数的信息。例如,可以使用`ParameterizedType`接口来获取参数类型的原始类型参数列表。 下面是一个示例代码,演示了如何使用反射获取类型: ```java import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; public class Main { public static void main(String[] args) throws NoSuchFieldException { // 获取目标类的Class对象 Class<MyClass> clazz = MyClass.class; // 获取目标字段的Field对象 Field field = clazz.getDeclaredField("myField"); // 获取字段的类型 Type fieldType = field.getGenericType(); // 如果字段的类型参数类型 if (fieldType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) fieldType; // 获取参数类型的原始类型 Type rawType = parameterizedType.getRawType(); System.out.println("Raw type: " + rawType); // 获取参数类型参数列表 Type[] typeArguments = parameterizedType.getActualTypeArguments(); for (Type typeArgument : typeArguments) { System.out.println("Type argument: " + typeArgument); } } } } class MyClass { private List<String> myField; } ``` 在上面的示例中,我们通过反射获取了`MyClass`类中名为`myField`的字段的类型。输出结果如下: ``` Raw type: interface java.util.List Type argument: class java.lang.String ``` 这表明`myField`字段的类型是`List<String>`,其中`List`是原始类型,`String`是参数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值