简体   繁体   中英

Class.getConstructor function parameters

I'm studying this piece of code and got stucked in the commented row:

protected <T> T creaOggetto(Class<T> classe, int id) {
    try {
        Package pacchetto = classe.getPackage();
        String nomePacchetto = pacchetto.getName();
        String nomeClasse = classe.getSimpleName();
        String nomeClasseRisultato = nomePacchetto + ".impl." + nomeClasse + "Impl";
        Class<?> classeRisultato = Class.forName(nomeClasseRisultato);
        Constructor<?> costruttore = classeRisultato.getConstructor(new Class[] {int.class});

        @SuppressWarnings("unchecked")
        T risultato = (T)costruttore.newInstance(new Object[] {id});

        return risultato;
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
    return null;
}

I know that getConstructor() returns the constructor object for that class but (new Class[] {int.class}) confuses me, what is his purpose?

According Java docs:

public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object. The parameterTypes parameter is an array of Class objects that identify the constructor's formal parameter types, in declared order. If this Class object represents an inner class declared in a non-static context, the formal parameter types include the explicit enclosing instance as the first parameter.

So, classeRisultato.getConstructor(new Class[] {int.class}); returns the constructor that accepts one and only one int parameter or NoSuchMethodException if it doesn't exist.

In code you posted, note that, using that constructor, it creates a new instance of that class passing the id , that is the actual int argument:

T risultato = (T)costruttore.newInstance(new Object[] {id});

The getConstructor(new Class[]); method takes in an array of Classes that represent the parameters of the constructor you want to get. For example, if you had a constructor in class X,

public X(int a, int b)

you could obtain said constructor using X.getClass().getConstructor(new Class[] {int.class, int.class}); The two int.class parameters in the array represent the two ints the the constructor take in.

Java Documentation

Since there are potentially multiple constructors, you need to specify the signature of the constructor to get the one you want when you call Class.getConstuctor.

The signature includes the type parameters to the constructor. The signature also includes the method name, but for a constructor the method name is always the class name in the source code, and it is always "<init>" in the compiled code, so you need not specify that.

So if you have new Class[] {int.class} then you are saying the signature consists of a single parameter of type "int". Which means you want the constructor X(int x) for class name X (note it is only the type of the parameters that matters, not the names of the parameter variables).

Also note that int.class resolves to java.lang.Integer.TYPE, which is used to denote the primitive type "int".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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