-3

Suppose the arguments given in the String array are:

String [] args = {"ABC","5", "4.4","true"};

Now I want to call the constructor that takes a String, integer, double and Boolean respectively by calling the method class.getConstructor(args).

I am trying to parse parameters the user passes to my program on the command line and want to configure my class accordingly.

How will I do this?

azhar baloch
  • 527
  • 6
  • 11
  • what ?? not sure what are you asking ? Are you saying how to call constructor with mentioned 4 args ? – Ravi Sep 29 '17 at 19:00
  • BTW, what are you trying to achieve ? – Ravi Sep 29 '17 at 19:01
  • 1
    `getConstructor(String.class, int.class, double.class, boolean.class)` is your way to go – Felix Sep 29 '17 at 19:02
  • @Ravi, yes I want to call the constructor with given 4 args in this case, but it may be 3, 2 or none at all. I hope you understand the question. – azhar baloch Sep 29 '17 at 19:03
  • @rollback but I don't know whether the first argument is a String, int or double. User can give the arguments in any order he wants, I just have to call the appropriate constructor. – azhar baloch Sep 29 '17 at 19:04
  • 2
    This is non trivial. If even you don't know what type those arguments are meant to be, you need to try all combinations (of constructors and possible deserializations of those arguments), until one sticks. Java won't guess what you mean, since even you don't know what you mean. – Sotirios Delimanolis Sep 29 '17 at 19:05
  • Have you considered using a map instead? – Juan Carlos Mendoza Sep 29 '17 at 19:10
  • @SotiriosDelimanolis I know what I mean, may be I could not explain well, or it is challenging enough to be answered by you. How can you say that "java won't guess what you mean", Is java created by you? – azhar baloch Sep 29 '17 at 19:16
  • 3
    What? In a previous comment, you said _I don't know whether the first argument is a String, int or double. User can give the arguments in any order he wants_. That's what I'm referring to. If you don't know the order, what type each argument should be converted to, then you have no choice but to test each possibility. – Sotirios Delimanolis Sep 29 '17 at 19:17

2 Answers2

2

As a string like "5" can mean a String, an integer, or a double, it's impossible to go from the args array forward to the matching constructor. As @Sotirios commented, you have to look at all constructors of the class, try to parse the args elements with the appropriate functions, and if that succeeds, then the constructor is applicable.

Then calling the constructor will be the easiest part of that project.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7
1

The method getConstructor(Class<?>... parameterTypes) you mention belongs to the Reflection Framework. While reflection does have its merits and is sometimes the only way to solve a problem, you should avoid it, unless you have a good reason.

It is hard and tedious to get code right using reflection as the compiler can't detect most errors. For general use it's just a code smell.

The usual way to initialize a class is to just call the respective constructor directly, as @Ralf Kleberhoff describes: MyClass instance = new MyClass(stringParam, intParam, doubleParam, booleanParam);.

If most of the parameters are optional, you usually only pass the required parameters to the constructor. This avoids an abundance of different constructors (to be tested and documented). The constructor usually sets the optional parameters to reasonable default values. These optional parameters are then set using setters if they are available.

As you don't really describe, what you want to achieve (instead of your current problem), we can only guess...

My guess would be that you try to parse parameters the user passes to your program on the command line and want to configure your class accordingly.

My Answer to that would just be: Don't write that code! There are many good libraries out there. These are tested and offer many goodies and examples. If you insist on reinventing the wheel, these are a good starting point to see how this could be done.

sruetti
  • 532
  • 2
  • 7