• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method.getReturnType() and byte[]  RSS feed

 
Mitch Christensen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Java reflection, Method.getReturnType() returns a Class. One of the methods I'm looking for (during reflection) returns a byte[]. What is the best way to test for this?

I discovered that testing for Method.getReturnType().getName().equals("[B") works, but is the best way to check the return type?

Thanks in advance.
-Mitch
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16049
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try comparing it with the Class object that represents byte[], like this:
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
Try comparing it with the Class object that represents byte[], like this:


Prefer the reference equality comparison: m.getReturnType() == byte[].class
 
Lyn Yang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a similar problem but harder. I use java reflection to get back a Class. I need to check whether it is subclass of Collection, therefore I can't use equal to do the checking. I tried the isInstance(), but it doesn't seem to work.

Any advices?

Thanks!
 
Mitch Christensen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try using Class.isAssignableFrom()?

see (http://download.java.net/jdk6/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class))
 
Lyn Yang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mitch Christensen:
Did you try using Class.isAssignableFrom()?

see (http://download.java.net/jdk6/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class))


Thanks, it works!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!