简体   繁体   English

如何在构造函数中获取具有泛型参数的类的构造函数

[英]How to get constructor of a class with a generic parameter in the constructor

The problem: To get parameterized constructor reference of subclasses of A (such as class H shown below) in class C. 问题:要获取类C中A的子类(例如下面显示的类H)的参数化构造函数引用。

Class A<T extends B> {
  public A(T objectT, D objectD, E objectE, F objectF) {

  }

  public T aMethodWithTAsReturnType();
}

Class B { }

Class C<T extends A<?>> {
  private Constructor<?> classAConstructor;
  public C(Class<T> classA) {
     classAConstructor=classA.getConstructor(B.class, D.class,E.class,F.class)
  }
}

Class H extends A<X> {
  public H(X objectX, D objectD, E objectE, F objectF) {}
}

Class X extends B {}

new C(new H(objectX,objectD,objectE,objectF));

The above code configuration would result in a NoSuchMethodException when a new Class C object is created because it cannot find the constructor for Class A 创建新的Class C对象时,上述代码配置将导致NoSuchMethodException ,因为它找不到Class A的构造函数

I am now trying to use: 我现在正在尝试使用:

Method methodInA = classA.getMethod('aMethodWithTAsReturnType')
Class<?> TClassInA = methodInA.getReturnType();

as a replacement for B.class in the classA.getConstructor line as I'm guessing that's the issue here because class B is a super type of T (in Class A). 作为classA.getConstructor行中B.class的替代品,因为我猜这里是问题所在,因为类B是T的超类型(在类A中)。

However... when I used methodInA.getReturnType() it returns B.class! 但是...当我使用methodInA.getReturnType()时,它返回B.class! rather than a class that has extended B. I then found a getGenericReturnType() method but it returns a Type object. 而不是扩展了B的类。然后,我找到了getGenericReturnType()方法,但是它返回Type对象。

This works for me: 这对我有用:

public class G {
    static class A<T extends B> {
        A(T objectT, D objectD, E objectE, F objectF) { }
    }

    static class B { }

    static class C<XT extends B, T extends A<XT>> {
        private Constructor<?> classAConstructor;
        public C(Class<T> classA, Class<XT> classX) throws SecurityException, 
            NoSuchMethodException {
            classAConstructor = classA.getConstructor(classX, D.class,
                    E.class, F.class);
        }
    }
    static class D { }
    static class E { }
    static class F { }

    static class X extends B { }

    public static class H extends A<X> {
        public H(X objectT, D objectD, E objectE, F objectF) {
            super(objectT, objectD, objectE, objectF);
        }
    }

    public static void main(String[] args) throws SecurityException,
        NoSuchMethodException {
        new C<X, H>(H.class, X.class);
    }
}

UPDATE: You will need to find some way of passing the parameterisation of A into C. I've updated your code to illustrate one way of doing so. 更新:您将需要找到一种将A的参数化传递给C的方法。我已经更新了您的代码以说明这样做的一种方法。

getConstructor() returns only public constructors. getConstructor()仅返回公共构造函数。 Try with getDeclaredConstructor() . 尝试使用getDeclaredConstructor()

EDIT: 编辑:

The problem is that the H constructor doesn't take a B as argument, but a X (which is a subclass of B). 问题在于H构造函数没有将B作为参数,而是X(它是B的子类)。 The signature of the constructor is thus H(X, D, E, F) . 因此,构造函数的签名为H(X, D, E, F) This constructor only accepts instances of X as its first argument. 此构造函数仅接受X实例作为其第一个参数。 It doesn't accept any kind of B instances, but only instances of X . 它不接受任何类型的B实例,仅接受X实例。

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

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