Re: Create an Object from an Array Class



Z wrote:
I am using reflection to getmethod(methodName, Class parameters) and
invoke(object, obj[] parameters) methods from some Objects.

My problem is:

Say: I want to invoke a method that takes String[] object as parameter
(I was able to getmethod correctly). When I try to create an object
(to pass as args to invoke):

Object someObject= (Object) String[].class.newInstance();

(Note that the object is not always an instance of String[]... it
could be an instance of SomeOtherClass[])

RTFM:
[ From Class.newInstance(): ]
Throws:
[ ... ]
InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
[ ... ]

Arrays cannot be created with a new instance, because they are a special-case class.

My question is:

Does the "[L" at the beginning and the ";" at the end have a meaning?

Yes, this is the internal representation of the class (it means that this is an array of java.lang.String's).

How can I solve this problem?

What you are probable intending is something like this:

Method m = Class.forName("some.pkg.Type").getMethod("main",
String[].class);
m.invoke(null, new String[0]);

I am using IntelliJ 6.0.5.

Java version matters, not IDE version.

--
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
.



Relevant Pages

  • Re: Create an Object from an Array Class
    ... I want to invoke a method that takes String[] object as parameter ... this is an array of java.lang.String's). ... create a class of the field type ...
    (comp.lang.java.programmer)
  • Re: reference methods or fields by "name"
    ... > contained in a string? ... returns the method you want to call on obj. ... To invoke it: ... Tony ...
    (comp.lang.java)
  • Re: Writing a .txt file
    ... you already know how to output the values to stdout. ... invoke your program like this: ... pointer/size combo. ... my goal ist not to write the array in the file only ...
    (microsoft.public.vc.language)
  • RE: help with array of functions
    ... If you are attempting to use different functions withint he same array then: ... Declare delegate types ... that dynamic invoke is quite slow ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: What is wrong?
    ... HTH ... "Andy Dorph" wrote: ... > When I invoke it, I get an array that is filled (determined with the IsArray ...
    (microsoft.public.excel.programming)