• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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!
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic