a little bug with Character.equals:

4 views
Skip to first unread message

sgurin

unread,
Oct 10, 2008, 3:49:52 PM10/10/08
to Java2Script
Hi all

the following expression

(new Character('c')).equals('c')

is true in java and false in javascript j2s translated code.

I think this is due to java.lang.Character javascript "equal" method:

equals:
function(obj){return this==obj;}

This is a very small detail, but can be the cause of time wastings....

cheers!

sgurin

unread,
Oct 10, 2008, 4:12:13 PM10/10/08
to Java2Script
well, with this dirty patch:

/**
* @j2sNative
* java.lang.Character.prototype.equals=function(obj){return
this.toString().equals(obj+"");}
*/{}

now the expression

(new Character('c')).equals('c')

evaluates ok. i don't know the "side effects"

p/d if I would like to contribute, how/where these kind of patches
are impacted in j2s project? (in j2slib part or if in eclipse jdt
translator part, etc ?), Now i'm using java static code with this
patches and seems to work well but...

Zhou Renjian

unread,
Oct 10, 2008, 11:24:22 PM10/10/08
to java2...@googlegroups.com
I prefer to a more decent fix by adding two methods into Charater.java:

    /**
     * Returns a hash code for this <code>Character</code>.
     * @return  a hash code value for this object.
     */
    public int hashCode() {
        return (int)value;
    }

    /**
     * Compares this object against the specified object.
     * The result is <code>true</code> if and only if the argument is not
     * <code>null</code> and is a <code>Character</code> object that
     * represents the same <code>char</code> value as this object.
     *
     * @param   obj   the object to compare with.
     * @return  <code>true</code> if the objects are the same;
     *          <code>false</code> otherwise.
     */
    public boolean equals(Object obj) {
        if (obj instanceof Character) {
            return value == ((Character)obj).charValue();
        }
        return false;
    }

From test, it fixed the bug.

The Java source:

        Character character = new Character('c');
        boolean equals = character.equals('f');
        System.out.println(equals);

will be compiled as:

var character =  new Character ('c');
var equals = character.equals (new Character ('f'));
System.out.println (equals);

The compiler will box 'f' into Character object in the equals method.

For being a contributor of Java2Script project, please let us know your user id on Sourceforge.net first, and then we will add you as a developer or as other roles accordingly, so you can touch the sources in SVN and help.

For a bug fix, We prefer to modifying the source files, recompiling and repacking j2slib and other plugins.

Regards,
Zhou Renjian

Sebastian Gurin

unread,
Oct 15, 2008, 9:21:52 AM10/15/08
to java2...@googlegroups.com
Zhou, thanks for the reply. I'm agree with your sollution, too. Since we are talking about these small errors, it seems Boolean.equals has the same problem of Character's equals(). for example:

Boolean b = new Boolean(true);
System.out.println(b.equals(true));

prints "true" in java and "false" in javascript. The sollution seems to be analog to Character's.

And another small detail, Boolean class has not the getName() method... so Boolean.class.getName() translated js code will fail to execute...

My sourceforge's id is cancerbero_sgx. I have experience developing eclipse plugins (including jdt based ones), and years using java and javascript/html/css but I'm not sure how can I help in j2s. I'm experimenting with j2s and never used it for production...

I have some experimental projects based on j2script like 1)java wrappers for yui js toolkit dom-event functions 2) lots of tests cases about java to javascript code translation correctness... 3) a small/lightweight java.util alternative , etc, but i'm not sure how these are related o be useful to j2s...

regards,


--
Sebastian Gurin <sgu...@softpoint.org>

Zhou Renjian

unread,
Oct 16, 2008, 11:42:30 AM10/16/08
to java2...@googlegroups.com
Hi Sebastian,

I just added you into Java2Script developer list. So you should have ccess to shell server group space and have access to Subversion repository right now.

By the way, I would like to talk a little more about Java2Script project's current development status. It is a long time since last release (1.0.0 at Oct 2007). And development is still active. Some new features, like supporting Eclipse 3.4, adding Java2Script desktop and compound pipe for Comet applications, are already available in SVN. We are going to make some milestone releases for 2.0.0 in the coming days.

There are lots of todos, for example, SWT 3.4 update (Currently Java2Script SWT is based on SWT 3.1), JFace support, Swing support, and others. Integrating Java2Script's libraries with other JavaScript libraries is another job. And lots of other jobs outside my mind...

Regards,
Zhou Renjian

Sebastian Gurin

unread,
Oct 17, 2008, 9:08:40 AM10/17/08
to java2...@googlegroups.com
On Thu, 16 Oct 2008 23:42:30 +0800
"Zhou Renjian" <zhour...@gmail.com> wrote:

> Hi Sebastian,
>

hi zhou!

> I just added you into Java2Script developer list. So you should have ccess
> to shell server group space and have access to Subversion repository right
> now.

thanks!

>
> By the way, I would like to talk a little more about Java2Script project's
> current development status. It is a long time since last release (1.0.0 at
> Oct 2007). And development is still active. Some new features, like
> supporting Eclipse 3.4, adding Java2Script desktop and compound pipe for
> Comet applications, are already available in SVN. We are going to make some
> milestone releases for 2.0.0 in the coming days.
>

IMHO there are a *lot* of interested things that can be (re) implemented in javascript but the tricky thing is offer the user a flexible sollution. In this sense j2script made it well in packaging the j2slib, java libs swt and ajax libs separately. For example, in my experience/opinion/taste, java.util and swt are "too much" for current javascript engines but j2s let me choose not to use them, or better said use my own visual / collection / etc sollutions. And that is good. My point is that j2s users and needs are several and different and that is a good idea give flexibility not forcing the user to one sollution.

> There are lots of todos, for example, SWT 3.4 update (Currently Java2Script
> SWT is based on SWT 3.1), JFace support, Swing support, and others.
> Integrating Java2Script's libraries with other JavaScript libraries is
> another job. And lots of other jobs outside my mind...

yes as I said, there are lots of things we are tempted to support. In my case where performance is an issue, i cant afford the use of java.util.HashMap when javascript have an excelent/LW/native map implementation, So I created a very small Collection, Iterator, List, Map class hierrarchy for exploiting this.

Another expierience of mine: SWT is a great toolkit for the desktop and the j2s implementation is simply great. But some people (like me) are more interesting on using web concepts like stylesheets, sintactic document, templates, that swt was simply not designed to support. I know j2s was created for easily migrate an swt app to the browser, but I think other users needs (like write web apps from scratch) have to be taken in account too.

Well, I hope you can understand my point (and my bad english).

Cheers


--
Sebastian Gurin <sgu...@softpoint.org>

Reply all
Reply to author
Forward
0 new messages