Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

casting the return value from method.invoke()

 
Ranch Hand
Posts: 409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a method that is working fine. But the handling of return values from method.invoke() seems kinda funky. The method.invoke() call is in a loop that processes a bunch of files, the names of which are provided by the cmm.filename variable. All of the file handling functions return boolean (whether processing was successful or not). So, As you can see from the if statement, I just want to print an error message of the return value is false.

method.invoke returns an Object, which is received by Object initial. The compiler won't let me cast that directly to boolean .... i.e. initialized = (boolean)method.invoke(hsthis, cmm.filename);

Is there a nicer way to do this? Might there be some way perhaps through a series of method calls like obj.getType().bendOver().kissJava().toBoolean() kind of thing?

 
Sheriff
Posts: 22644
123
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boolean is represented using its wrapper type, Boolean. There are (at least) two ways for this:
The first casts to the wrapper type, then uses the booleanValue() method to get its matching primitive value. Note that since Java 5.0, because of auto-boxing, you don't even need to call booleanValue().

The second compares the result with Boolean.FALSE, a static instance of the wrapper type that represents false. equals only returns true if the passed object is also a Boolean with the same value.
 
Roger F. Gay
Ranch Hand
Posts: 409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:boolean is represented using its wrapper type, Boolean. There are (at least) two ways for this:
The first casts to the wrapper type, then uses the booleanValue() method to get its matching primitive value. Note that since Java 5.0, because of auto-boxing, you don't even need to call booleanValue().

The second compares the result with Boolean.FALSE, a static instance of the wrapper type that represents false. equals only returns true if the passed object is also a Boolean with the same value.



Thanks. That solved my problem and then some.

In my attempt:
boolean != Boolean


....
 
Ranch Hand
Posts: 32
Eclipse IDE Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you getting the object as string then you can try this


 
Roger F. Gay
Ranch Hand
Posts: 409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems kinda cool:

I'll let the called method provide detailed information if there's an error and the calling method simply report a failure if false is returned. Now I'm a speed demon.
 
Rob Spoor
Sheriff
Posts: 22644
123
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need that cast to Boolean (nor the parentheses around false). The equals method takes an Object; internally it will check if it's a Boolean or not.
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic