Acquiring generic class type

前端 未结 5 1645
轻奢々
轻奢々 2021-01-02 19:40

Hi is there any way in Java to get staticly generic class type

I have ended up with construct

    List l = new ArrayList&         


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-01-02 20:30

    Basically, just cast around to fool the compiler. At runtime it's a simple Class anyway.

    a utility method:

    public static >
    Class cast(Class classC, Class classE)
    {
        return (Class)classC;
    }
    
    Class> c = 
            cast(List.class, TaskLocalConstraints.class);
    

    If you need a real Type complete with runtime generic type info, that's a different story.

提交回复
热议问题