• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Method.getReturnType() and byte[]

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try comparing it with the Class object that represents byte[], like this:
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic