简体   繁体   English

确定通用类型字段的类类型

[英]Determine class type of Generic typed field

I am getting NoSuchFieldException for the following piece of code: 我正在为下面的代码获取NoSuchFieldException

public class MultipleSorting<T> extends Observable {
    private SelectItem[] criteria1;
    private SelectItem[] order1;
    private SelectItem[] criteria2;
    private SelectItem[] order2;
    private SelectItem[] criteria3;
    private SelectItem[] order3;

    private T criteriaType;

    private T selectedCriteria1;
    private SortOrder selectedOrder1;
    private T selectedCriteria2;
    private SortOrder selectedOrder2;
    private T selectedCriteria3;
    private SortOrder selectedOrder3;    

    private Boolean[] enabledRows = new Boolean[]{Boolean.TRUE, Boolean.FALSE, Boolean.FALSE};

    private Boolean addButtonVisible1 = Boolean.TRUE;
    private Boolean addButtonVisible2 = Boolean.FALSE;
    private Boolean addButtonVisible3 = Boolean.FALSE;

    public MultipleSorting() {
        super();
    }

    private Class<T> getCriteriaClass() throws NoSuchFieldException {
        Field field = this.getClass().getField("criteriaType");
        field.setAccessible(true);
        return (Class<T>)field.getType();
    }

    public void addOrRemoveRow(ActionEvent event) {
        // other codes
        Method setSelectedCriteriaMethod = getClass().getDeclaredMethod("setSelectedCriteria" + (index + 1), new Class[]{getCriteriaClass()});  
        // other codes
    }

    // getters and setters
}

I am getting the exception when I invoke the method getCriteriaClass() . 调用方法getCriteriaClass()时遇到异常。 The criteriaType doesn't have any getter and seeter method. criteriaType没有任何吸气剂和吸气剂方法。 Also this field is not initialized. 同样,该字段未初始化。 That is why I cannot call criteriaType.getClass() as it is throwing NullPointerException . 这就是为什么我无法调用criteriaType.getClass()因为它引发了NullPointerException

My aim is to determine the class type of T and I don't want to pass the class of T in the constructor of this MultipleSorting class. 我的目的是确定T的类类型,并且我不想在此MultipleSorting类的构造函数中传递T的类。

I am unable to understand why I am getting NoSuchFieldException . 我不明白为什么我得到NoSuchFieldException Any pointer would be very helpful to me. 任何指针对我都会非常有帮助。

If you look at the JavaDoc of getField() , you see the problem: 如果查看getField()JavaDoc ,则会看到问题:

Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object. 返回一个Field对象,该对象反映此Class对象表示的类或接口的指定公共成员字段

You need to use: 您需要使用:

Field field = this.getClass().getDeclaredField("criteriaType");

From the JavaDoc of getDeclaredField() : getDeclaredField()JavaDoc中

Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object. 返回一个Field对象,该对象反映此Class对象表示的类或接口的指定声明字段。

Note that getDeclaredField() , unlike getField() , won't find inherited fields. 请注意,与getField()不同, getDeclaredField()不会找到继承的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM