Java generics, get Class of generic parameter

后端 未结 2 2057
天命终不由人
天命终不由人 2021-01-31 07:50

I have an abstract class:

public abstract class RootProcessor {
    Class clazz;
}

I need to fill ClassT clazz;

相关标签:
2条回答
  • 2021-01-31 08:18

    There is a post of the same subject: Reflecting generics

    And a class that implement it:TypeArgumentsUtils.java

    An example is in the unit test.

    So if you have this class:

    public class BarProcessor extends RootProcessor<Bar> {
        public BarProcessor() {
        }
    }
    

    than you would get the first parameter with:

    Class barClass = TypeArgumentsUtils.getFirstTypeArgument(
            RootProcessor.class, BarProcessor.class);
    
    0 讨论(0)
  • 2021-01-31 08:19

    The typesafe, but boilerplatey way to do this is to pass the Class<T> token "where the compiler can see it":

    public abstract class RootProcessor<T> {
        Class<T> clazz;
    
        protected RootProcessor<T>(Class<T> clazz) {
            this.clazz = clazz;
        }
    }
    
    public class FooProcessor extends RootProcessor<Foo> {
        public FooProcessor() {
            super(Foo.class);
        }
    }
    

    If you're doing an unchecked cast but you "know what you're doing" and want the compiler to stop complaining, the correct approach would be localising the non-type-safe-but-you-know-they-work bits and using @SuppressWarnings:

    public abstract class RootProcessor<T> {
        Class<T> clazz;
        { initClazz(); }
    
        @SuppressWarnings("unchecked")
        private void initClazz() {
            // the usual verbiage you already have in your question
            this.clazz = this.getClass().getGenericSuperclass().yadda().blah();
        }
    }
    

    (I won't hold this against you :P)

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