How can I... make a random number of arguments call?
Joined: Oct 11, 2009
(Whoa, that's what 'random' means, like in 'random acces')
It's not really important (maybe). I'm just learning, and I'm a begginer, and I haven't got to the reflection yet, but I just have ideas , and I wonder if this is possible at all, maybe even how?
So... can I call a method programmatically? I think that probably I can, with reflection. Can I call an arbitrary number of arguments programmatically, or just arbitrary arguments? (not var-args)
Actually I do have some idea that attracts me all the time in that direction, and that's why I ask this question which is way over my current knowledge, so that I can sleep better. Thanks.
: (Like for example that elements of an ArrayList are each solo one argument in a method call. Not that ArrayList is one argument, but arguments become all and each elements separatedly, of an ArrayList, together with the variable number of arguments, call, and the cherry on top. Hm?)
The reflection API allows you to discover things about Java classes, such as which constructors, methods and fields a class has, at runtime, and it makes it also possible to call constructors and methods on objects in a dynamic way (i.e. without the need to know beforehand, at compile time, which methods there are).
I found this for the begining, which answers a part of my wondering. ( may-be even all )
This little program is a complete, if minimal, test framework. It takes a list of class names on the command line. For each class name, it instantiates the class using its parameterless constructor and invokes a parameterless method called test. If the instantiation or invocation throws an exception, the test is deemed to have failed. The program prints each failure, followed by a summary of the test results. The reflective instantiation and invocation no longer require explicit array creation, because the getMethod and invoke methods accept a variable argument list. The program also uses the new printf facility, which relies on varargs. The program reads much more naturally than it would without varargs.