Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How to invoke a method with primitives through reflection  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to invoek a method which takes primitive as parameter through reflection..
import java.lang.reflect.* ;
class ReflectionTest {
public static void main(String []args) throws Exception
{
A a = new A() ;
Class c = a.getClass() ;
/*
Method m = c.getMethod("longWithoutParam", null);
m.invoke(a, null);
*/
Class longClass[] = new Class [1];
longClass [0] = Long.TYPE ;
System.out.println( longClass[0].isPrimitive() );
Method m = c.getMethod("longWithParam", longClass);
m.invoke(a, longClass);

}
}

class A {
public void longWithParam( long l ) {
System.out.println("longWithParam method invoked") ;
}
public void longWithoutParam( ) {
System.out.println("longWithoutParam method invoked") ;
}
}

After compilation and trying to run says 'argument type mismatch' can some body explain me what is happening...
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reflection only works on OBJECTS. Primitives are not objects.
See http://www.javaranch.com/ubb/Forum1/HTML/001421.html
 
Suren Babu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot cindy...
But still after changing to Object, i am getting 'argument type mismatch'. Could you help me on this...
Here is the code...
import java.lang.reflect.* ;
class ReflectionTest {
public static void main(String []args) throws Exception
{
A a = new A() ;
Class c = a.getClass() ;
/*
Method m = c.getMethod("longWithoutParam", null);
m.invoke(a, null);
*/
Class longClass[] = new Class [1];
longClass [0] = Class.forName("java.lang.Long") ;
//System.out.println( longClass[0].isPrimitive() );
Method m = c.getMethod("longWithParam", longClass);
m.invoke(a, (Object [])longClass); // Runtime error here

}
}
class A {
public void longWithParam( java.lang.Long l ) {
System.out.println("longWithParam method invoked") ;
}
public void longWithoutParam( ) {
System.out.println("longWithoutParam method invoked") ;
}
}

[This message has been edited by Suren Babu (edited June 05, 2001).]
 
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Suren Babu:
> I want to invoek a method which takes
> primitive as parameter through reflection..
> ...
> longClass [0] = Long.TYPE ;
> System.out.println( longClass[0].isPrimitive() );
> Method m = c.getMethod("longWithParam", longClass);
> m.invoke(a, longClass);

try this in your main()
<pre>
A a = new A() ;
Class c = a.getClass() ;
Class[] longClass = new Class[1];
longClass[0] = long.class;
try
{
Method m = c.getMethod("longWithParam", longClass);
m.invoke(a, new Object[] {new Long(5L)});
}
catch (NoSuchMethodException e)
{
/* EMPTY */
}
catch (Exception e)
{
e.printStackTrace();
}
</pre>
HTH,
Junilu
 
Suren Babu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Small correction to your code. Instead of this line...
longClass[0] = long.class;
if you replace with
longClass[0] = Class.forName("java.lang.Long") ;
it works. I still don't know why it didn't work in previous case.
Any Idea?
Thanks
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It didn't work because of this line
m.invoke(a, longClass);
The second parameter is supposed to be an Object[] that contains valid arguments to the method you are invoking. A.longWithParam() expected a long argument, while longClass[0] is a Class object. Casting longClass to Object[] will not help either. Note the difference between your m.invoke() and the one I gave.
BTW, my code works even without the change you suggested. In fact, the change you suggested results in a NoSuchMethodException. Your original line of code would have also worked.
longClass[0] = Long.TYPE;
Junilu

[This message has been edited by JUNILU LACAR (edited June 05, 2001).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be sure to check out this thread as well.
 
Suren Babu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I tried the same code what you posted, but still i am getting the following error at runtime...
Exception in thread "main" java.lang.NoSuchMethodException
at java.lang.Class.getMethod0(Native Method)
at java.lang.Class.getMethod(Unknown Source)
at ReflectionTest.main(ReflectionTest.java:24)
Here is the code i tried...
A a = new A() ;
Class c = a.getClass() ;
Class[] longClass = new Class[1];
longClass[0] = long.class;
try
{
Method m = c.getMethod("longWithParam", longClass);
m.invoke(a, new Object[] {new Long(5L)});
}
catch (NoSuchMethodException e)
{
e.printStackTrace(); // prints this
}
catch (Exception e)
{
e.printStackTrace();
}
No Clue why this happening...
One interesting thing is...
instead of this line... longClass[0] = long.class ;
if i replace by this...
longClass[0] = Class.forName("java.lang.Long") ;
it works perfectly.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case:
longClass[0] = long.class;
the class that you are loading is the "Class" class for the primitive long.
In this case
longClass[0] = Class.forName("java.lang.Long") ;
the class that you are loading is the "Long" wrapper class.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!