Acquiring generic class type

前端 未结 5 1606
轻奢々
轻奢々 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:15

    The closest workaround I can think of is known as super type tokens. And of course someone thought of it before me ;) Depending on your context you may find the idea useful.

    Cheers!

    0 讨论(0)
  • 2021-01-02 20:25

    I'm not sure if you noticed, but in your example you will get java.util.ArrayList class in "c" variable. I'm not sure if this is your point. However if we asume, that yes it is:

    @SuppressWarnings("unchecked")
    Class<? extends List<TaskLocalConstraints>> c = (Class<? extends List<TaskLocalConstraints>>) ArrayList.class;
    

    However if you need list, use this:

    @SuppressWarnings("unchecked")
    Class<? extends List<TaskLocalConstraints>> c = (Class<? extends List<TaskLocalConstraints>>) List.class;
    

    I added @SuppressWarnings annotation to get rid of warnings. This annotation can be used with any granularity, so you can also flag your local code with it.

    The key idea in this approach is that generics are erasure. There is no information about them in runtime, to above assignments are correct.

    0 讨论(0)
  • 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 <C extends Collection, E, T extends Collection<E>>
    Class<T> cast(Class<C> classC, Class<E> classE)
    {
        return (Class<T>)classC;
    }
    
    Class<List<TaskLocalConstraints>> c = 
            cast(List.class, TaskLocalConstraints.class);
    

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

    0 讨论(0)
  • 2021-01-02 20:30

    You can get the class for List, which would be Class c = List.class. Unfortunately in Java information about generics isn't retained in runtime so after compilation your List<TaskLocalConstraints> will become simply List.

    0 讨论(0)
  • 2021-01-02 20:34

    Since all List<something> classes actually correspond to the same class at runtime, you could do this:

    Class<List<TaskLocalConstraints>> c 
        = (Class<List<TaskLocalConstraints>>) List.class;
    

    But for some reason, Java doesn't like it. Tried it with String:

    Laj.java:9: inconvertible types
    found   : java.lang.Class<java.util.List>
    required: java.lang.Class<java.util.List<java.lang.String>>
    Class<List<String>> c = (Class<List<String>>) List.class;
    

    Well, let's fool it then:

    Class<List<String>> c = 
       (Class<List<String>>) (Class<?>) List.class;
    

    It's silly, but it works. It produces an "unchecked" warning, but so does your example. Note that it doesn't result in the same class as your example, though. Your example returns the actual class of the object, namely ArrayList. This one returns List, obviously.

    0 讨论(0)
提交回复
热议问题