Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
/ BigChallenge Public archive

关于java语法的问题,工作中遇到的泛型反射父类的全局函数,没有直接的办法解决。

Notifications You must be signed in to change notification settings

lipangit/BigChallenge

Repository files navigation

泛型反射父类的全局函数

####这个是一个关于java语法的问题,从工作中的代码抽象出来的,归根结底是取得泛型的Class,同事们都说没办法

####结构很简单

public class BaseMessage {
    public static String parseFromString(String string) {
        System.out.println("BaseMessage ref function " + string);
        return string;
    }
}
public class ArticalMessage extends BaseMessage {
}

####这里的两个方法,第二个方法通过传参的方式回避了这个问题,第一个方法想直接反射,却报错

public class PbRequest<T extends BaseMessage> {
    public void run_T_StaticFunction() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<T> entityClass = (Class<T>) ((ParameterizedType) BaseMessage.class.getClass().getGenericSuperclass()).getActualTypeArguments()[0];//这行报错
        java.lang.reflect.Method parseFrom = entityClass.getMethod("parseFromString", String.class);
        String str = (String) parseFrom.invoke(null, "Some String");
        System.out.println(str);
    }
    public void run_T_StaticFunction(Class<T> entityClass) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//        Class<T> entityClass = (Class<T>) ((ParameterizedType) BaseMessage.class.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        java.lang.reflect.Method parseFrom = entityClass.getMethod("parseFromString", String.class);
        String str = (String) parseFrom.invoke(null, "Some String");
        System.out.println(str);
    }
}

####目的很明确让 run_T_StaticFunction() 能通过运行

public class RunMe {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        PbRequest<ArticalMessage> pb = new PbRequest<>();
        //这个不报错
        pb.run_T_StaticFunction(ArticalMessage.class);
        //现在的目的是让这个也不报错
        //pb.run_T_StaticFunction();
    }
}

About

关于java语法的问题,工作中遇到的泛型反射父类的全局函数,没有直接的办法解决。

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages