• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to invoke a new Integer object using reflection?  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am very tired right now, I hope this makes some sense.

I was wondering, how do I invoke a new Integer object using reflection.

I am using reflection to find out what type parameter primitive type I need to supply to a method.

So I get a property to test, parse out the primitive class type for the parameter for the method. Now I want to use refection to create a new object of the primitive type I found.

For example

Let's say
Class[] param = new Class[1];
param[0] = (Class) Integer.Type; //I found this out through doing some parsing


Object[] object = new Object[1];
object[0] = param[0].newInstance();// This is where things fall apart. I don't want to use new Integer(0); for example.

Any ideas how to do this?


Cheers,

Drew

 
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer does not have a default constructor which you need to supply int or String as argument.
You may want to read
http://java.sun.com/developer/technicalArticles/ALT/Reflection/#6
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!