BOOL and JavaLangBoolean problems

133 views
Skip to first unread message

Edouard Robinet

unread,
Oct 30, 2012, 12:03:40 PM10/30/12
to j2objc-...@googlegroups.com
Hello !

I'm having a problem using j2objc for my project.

Some translated classes are using classes that I have implemented in native Objective-C.

In those classes, I'm returning JavaLangBoolean (example : I'm returning wether or not a NSDictionnary contains a key).
But sometimes the translated classes use this :
if ([myDictionnary containsKeyWithMyObject:theObject])

instead of :
if ([[myDictionnary containsKeyWithMyObject:theObject] booleanValue])

As a result, and because I'm returning an instance of JavaLangBoolean (that contains the BOOL value), if (...) returns YES, because my instance of JavaLangBoolean isn't nil, BUT the bool value in it is NO.

I don't know if i'm very clear, but if somebody can help me with this, it's been 1 week I'm struggling with this functionality ...

Thanks in advance for anyone that has an answer about that !

Cheers ;)

Tom Ball

unread,
Oct 30, 2012, 12:54:41 PM10/30/12
to j2objc-discuss
Your native methods should return a BOOL, not JavaLangBoolean.  Java autoboxing (compiler conversion between primitive types and their java.lang wrapper classes) requires Java type information that your native classes won't provide (since they aren't written in Java :-).  

If you post a more complete example, I can give more specific advice.

Tom


--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group, send email to j2objc-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Edouard Robinet

unread,
Oct 30, 2012, 1:03:28 PM10/30/12
to j2objc-...@googlegroups.com
But when I make my native functions returning BOOL instead of JavaLangBoolean, I get compile errors ...

Tom Ball

unread,
Oct 30, 2012, 2:04:05 PM10/30/12
to j2objc-discuss
"If you post a more complete example, I can give more specific advice." :-)

Edouard Robinet

unread,
Oct 31, 2012, 4:22:08 AM10/31/12
to j2objc-...@googlegroups.com
Here, I put on github a basic example of the bugged feature : https://github.com/Biness/j2objc-example

The original class is the Java class I am translating.

The translated class is the Objective-C equivalent of the aforementioned Java class.

In native class, you have the implementation of ContainsKeyWithMProperty of PropertyValueDictionnary.

You can see that in the first line of the translated class, it just checks if the JavaLangBoolean I'm returning is not nil, and not the booleanValue inside it as in the second line.

If you need some additional information, just let me know ...

2012/10/30 Tom Ball <tb...@google.com>
"If you post a more complete example, I can give more specific advice." :-)


On Tue, Oct 30, 2012 at 10:03 AM, Edouard Robinet <edouard...@gmail.com> wrote:
If you post a more complete example, I can give more specific advice.

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group, send email to j2objc-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Edouard Robinet
Etudiant EPITECH 5ème année

Tom Ball

unread,
Oct 31, 2012, 12:44:09 PM10/31/12
to j2objc-discuss
You're missing the critical piece: where is the Java class that defines the API which your native class implements?  From the Java compiler's perspective, there is no native code, only Java.  Without that API, when you run javac on the class it will report "no such method" when it tries to resolve "_currentValues.ContainsKey(prop)".  Without that API, I have no idea what type information the compiler has regarding that expression.

Independently, why is JavaLangBoolean used for an obviously boolean result?  For example, Collection.contains() returns a boolean, not a Boolean.  Wrapper classes are normally used when an Object is needed, otherwise primitives should be used for clarity and slightly better performance.  You are aware that the compiler has to add Boolean.valueOf(boolean) and Boolean.booleanValue() methods wherever it needs to box or unbox those primitive expressions, right?  Autoboxing can make Java code easier to read by hiding all of those method calls, but the .class file still has all of them.

Tom

Edouard Robinet

unread,
Nov 5, 2012, 3:07:00 AM11/5/12
to j2objc-...@googlegroups.com
Oh, right ! Actually my engine that I translate sometimes call methods and expects a Boolean, and sometimes a boolean ...

Ok, thanks a lot, I will change this in my engine, it was the reason of my errors. :)


2012/10/31 Tom Ball <tb...@google.com>
Reply all
Reply to author
Forward
0 new messages