Closed Bug 218163 Opened 21 years ago Closed 21 years ago

Simple properties not handled correctly

Categories

(Rhino Graveyard :: Core, defect)

x86
All
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: njouve, Assigned: norrisboyd)

References

Details

Attachments

(4 files)

User-Agent:       Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) Opera 7.11  [en]
Build Identifier: 

When you script a java bean, you cannot acces the bean properties for whom there 
are multiple methods having the name of the getter.
For instance if a bean has the property "name" and an additional getName(Locale 
locale) method, the "name" property won't be available when scripting the bean 
using Rhino.

Reproducible: Always

Steps to Reproduce:
Here is a TestCase exhibiting the problem:

package org.mozilla.javascript;

import java.io.StringReader;
import java.util.Locale;

import junit.framework.Assert;
import junit.framework.TestCase;

/**
 *
 * Tests Case
 * 
 * @author njouve
 * @since 3 sept. 03
 * 
 * $Log$
 */
public class TestPropertyWithMultipleGetters extends TestCase {

    /**
     * Constructor for TestPropertyWithMultipleGetters.
     * @param arg0
     */
    public TestPropertyWithMultipleGetters(String testName) {
        super(testName);
    }

    /**
     * Tests that a bean with a getter having an overloaded version 
     *
     */
    public void testPropertyHandlingWithMultipleGetters() {
        try {
            String propertyValueToSet = "John Doe";
            Context ctx = new Context();
            ctx = Context.enter(ctx);

            Script script = ctx.compileReader(null, new StringReader("this.
name=\"" + propertyValueToSet + "\""), "", 0, "");

            TestBean testBean = new TestBean();

            Scriptable thisObject = new WrapFactory().wrapNewObject(ctx, null, 
testBean);

            script.exec(ctx, thisObject);

            Assert.assertEquals(propertyValueToSet, testBean.getName());
        } catch (EvaluatorException e) {
            e.printStackTrace();
            fail("Expression should have been correctly evaluated" + e.
getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    }

    /**
     * This class is a bean that has a "name" property and an
     * additional method having the same name as the getter but with an 
additional
     * parameter
     * 
     *
     * 
     * @author njouve
     * @since 3 sept. 03
     * 
     * $Log$
     */
    public static class TestBean {

        public TestBean() {
        }

        private String name = null;
        /**
         * @return
         */
        public String getName() {
            return name;
        }

        /**
         * @param string
         */
        public void setName(String string) {
            name = string;
        }

        /**
         * @return
         */
        public String getName(Locale locale) {
            return null;
        }

    }

}

Actual Results:  
This test will fail with the current version, because an EvaluatorException is 
thrown when accessing the "name" property from javascript.

Expected Results:  
The test should succeed, that is no exception should be thrown

I 've found the offending lines in the core and I'd like to sublit the 
correction.
the offending class is org.mozilla.javascript.JavaMembers

 -- Line 398 -- 

 // Grab and inspect the getter method; does it have an empty parameter list,
// with a return value (eg. a getSomething() or isSomething())?
Class[] params;
Method[] getMethods = getJavaMethod.getMethods();
Class type;
if (getMethods != null &&
    getMethods.length == 1 &&
    (type = getMethods[0].getReturnType()) != null &&
    (params = getMethods[0].getParameterTypes()) != null &&
    params.length == 0)
{

It should be replaced by :

// Grab and inspect the getter method; does it have an empty parameter list,
// with a return value (eg. a getSomething() or isSomething())?
Class[] params;
Method[] getMethods = getJavaMethod.getMethods();
Class type = null;
// test each method in the set to found the only one having no parameter
for (int methodIdx = 0; methodIdx < getMethods.length; methodIdx++) {
    Method testedMethod = getMethods[methodIdx];
    if ((testedMethod.getReturnType() != null) &&
	(testedMethod.getParameterTypes() != null) &&
	(testedMethod.getParameterTypes().length == 0)) {
	    type = testedMethod.getReturnType();  
	    params = testedMethod.getParameterTypes();                          
	}
	
}
if (type != null)
{
Nicolas, could you provide a patch against CVS tip as JavaMembers there changed
compared with Rhino 1.5R4.* ?
Summary: Simple properties not handled correctly → Simple properties not handled correctly
cvs diff against latest version of JavaMembers (1.40 at time of writing)
Few notes for the above patch:

diff -r1.40 JavaMembers.java

This diff generates a patch in the default format but patches in unified format
are more readable and resilient to source changes. Please use cvs diff -u
or even 
cvs diff -U 5 
to generate them.

>            if ((method.argTypes != null) && (method.argTypes.length == 0)) {
The check (method.argTypes != null) is redundant here since it always holds even
for 0-arity methods.

>                    if (method.argTypes.length == 0) {
This check is also redundant since it is already covered by the previous if.
Redundant checks removed.
Unified format
I committed Nicolas patch with some formating changes to make code to fit 80 colons.
Status: NEW → RESOLVED
Closed: 21 years ago
Resolution: --- → FIXED
*** Bug 142100 has been marked as a duplicate of this bug. ***
Trageting as resolved against 1.5R5
Target Milestone: --- → 1.5R5
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: