lundi 25 février 2019

Does Constructor dot newInstance use reflection?

Let MyClass be the class represented by this java code:

public MyClass 
{
    private String foo;
    private Integer bar;
    public MyClass(byte[] contents) { ... }
}

Let myConstructor be the following Constructor instance:

Constructor myConstructor = MyClass.class.getDeclaredMethod(byte[].class);

My question is the following

Does this code use reflection?

byte[]  contents   = new byte[]{0,1,2};
MyClass myInstance = myConstructor.newInstance(contents);

or is equivalent, once that myConstructor is instantiated, to the following code?

byte[] contents = new byte[]{0,1,2};
MyClass myInstance = new MyClass(contents);

The equivalence relation I'm thinking about is that .newInstance(byte[] contents) access directly to the constructor in the same way as the new and the only reflection operation is finding the constructor.

Kind regards





Aucun commentaire:

Enregistrer un commentaire