Task: I have a class name. With the help of reflection, I get all the available designers out of it and invite the user to choose one. After he chooses, he enters the desired parameters of a specific constructor and an object is created according to this. Question: how in Constructor.newInstance (params) in place of params shove everything that the user wants to ask? For example, I have a constructor with fields (int, String), how to make an object on this k?

    2 answers 2

    Judging by the docks , you need to pass Object [] with your arguments as params. Those. with a view constructor

    (int, String)

    need something like this:

    Integer firstArg=42; String secondArg="Ответ на сей вопрос я нагуглил за 20 секунд!"; Object[] args=new Object[]{firstArg, secondArg); Constructor.newInstance(args); 

    Moreover, it is not primitives that are to be transferred to the method, but their wrappers are objects.


    In general, according to this , it is done like this:

    1) Get an array of constructors.

    2) Method

     getParameterTypes() 

    Get the array of constructor argument types.

    3) Create an array of variables of recognized types and create an object as described above.

    • You google primitive. And if I have 3 arguments, or less, or more? - 111xbot111
    • @ 111xbot111, why not? Just a little bit wrong. It is necessary yes, an array of Objects to betray, but not separate arguments. The answer is corrected. - YurySPb
    • @Yuriy SPb, that's not the point. Rather, not only in that. Well, for example: you have 3 ka in the class: (int), (long, String, double), (boolean, int, int). First, the user wants to create an object by the first one, and then he wants by 3. In this case, we cannot bind to the predefined types of variables (as you say Integer firstArg; String secondArg;), or their number ( but suddenly not two, but three, not three, but one, and so on) - 111xbot111
    • I need to solve not the given example, but the general case, if I may say so. - 111xbot111
    • @ 111xbot111, I must admit that I did not quite understand what the problem was. That you cannot get a list of the types (classes) of the arguments of the constructor? - YurySPb
    1. Create an array of type class.
    2. Read user input.
    3. We learn type, we push in an array.
    4. Repeat 2-3 if needed.
    5. The array is passed as a parameter to .newInstance() .
    • @Yuriy SPb, there are no comments anymore. Yes, conversion is not a question at all. I understood what to do so: 1) I get a string 2) if int / float / ... {conversion} 3) I add the newly created object to the array 4) I repeat as much as I need 5) I insert into newInstans. Question: how can I reduce the 2nd move, otherwise it will hurt a lot if the check for each type is done. Thanks for the help. - 111xbot111
    • @ 111xbot111, well, it's easier, I think, to skip through the swich operator. It will be more beautiful and less code. Or pervert and through the same reflection to create objects Integer , Boolean , etc. and they call valueOf ("user input"); But I am not sure that all types have such a method and will work it out correctly. So what I would do through swich . - YurySPb