This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes casting the return value from method.invoke() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "casting the return value from method.invoke()" Watch "casting the return value from method.invoke()" New topic
Author

casting the return value from method.invoke()

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 376
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?



Correlation does not prove causality.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 376
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


....
Kiaamaa Liammes
Ranch Hand

Joined: Oct 03, 2009
Posts: 32

if you getting the object as string then you can try this



SCJP ,SCWCD
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 376
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.
 
GeeCON Prague 2014
 
subject: casting the return value from method.invoke()