• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trouble with reflection

 
anitha nair
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi forum,

Please help me out with this problem.

I am getting the parameter types of a method in a class as

Class[] parameterTypes=method.getPrameterTypes();

I need to test if the parameter types are of primitive types i.e int or String or long etc...If its not it should show a message box.
For that i used the following code.The trouble is that it doesnt seems to test the whole conditions.It seems to check only the first condition and for all other conditions it gives a negative result.i.e for String,long,float etc it shows the message box whereas it should be executing else condition instead.Please tell me whats wrong with it.



Thank you all in advance.
[ August 08, 2006: Message edited by: Bear Bibeault ]
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that Class.toString returns the result you're expecting? toString returns a string representation, but not necessarily the one you would expect. Try the getName method instead. Also, don't call it 9 times - call it once, and store the result in a variable.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would seem much better to test the class of the arguments rather than the class name. You can then simplify the problem to checking if the class of the parameters is in an allowable set. For example -
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just updated teh code so theat it woll work with Java prior to 1.5.

 
Singhal Anuj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I do not think this code will compile in java 1.4

Regards,
Anuj
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anitha nair:

I need to test if the parameter types are of primitive types i.e int or String or long etc...


Just for the record, String is not a primitive type.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably a waste of time since the OP did not seem interested but ...



[ August 08, 2006: Message edited by: James Sabre ]
[ August 08, 2006: Message edited by: James Sabre ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic