• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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"
Sheriff
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: 12355
201
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: 12355
201
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
Sheriff
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"
Sheriff
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!