commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Maurizio Cucchiara <mcucchi...@apache.org>
Subject Re: [BeanUtils] [OGNL] Debugging BeanMap
Date Sat, 12 Nov 2011 08:48:16 GMT
Hi Chris,
I don't know if BUM allows what you are trying to achieve.
Would you consider to use OGNL instead?

    @Test
    public void testOGNL() throws Exception {
        OgnlRuntime.setNullHandler(Student.class, new StudentNullHandler());
        Student student = new Student();
        Map<String, Object> context = Ognl.createDefaultContext(student);
        Ognl.setValue("id", student, 1);
        Ognl.setValue("course.name", student, "course name");
        assertEquals(1, student.getId());
        assertEquals("course name",student.getCourse().getName());
    }

    private static class StudentNullHandler implements NullHandler {
        public Object nullMethodResult(Map<String, Object> context,
Object target, String methodName, Object[] args) {
            return null;
        }

        public Object nullPropertyValue(Map<String, Object> context,
Object target, Object property) {
            final Course course = new Course();
            ((Student)target).setCourse(course);
            return course;
        }
    }


Twitter     :http://www.twitter.com/m_cucchiara
G+          :https://plus.google.com/107903711540963855921
Linkedin    :http://www.linkedin.com/in/mauriziocucchiara

Maurizio Cucchiara



On 11 November 2011 19:43, Chris Pratt <thechrispratt@gmail.com> wrote:
> I simplified my test case and I think I may have found the culprit.  I was
> expecting the BeanMap to handle dot notation properly, but it appears I was
> mistaken.  The following doesn't seem to work:
>
>  public static void main (String... args) throws ADKException, IOException {
>    Student student = new Student();
>    Map studmap = new BeanMap(student);
>    studmap.put("id",4242);
>    studmap.put("name.firstname","Chris");
>    studmap.put("name.lastname","Pratt");
>    student.dump();
>  } //main
>
>
> I get an IllegalArgumentException that Student has no property called
> name.firstname.  Is there any way around this?  BeanMap isn't really useful
> to me if it doesn't support the object hierarchy that a bean engenders?
>  (*Chris*)
>
> On Thu, Nov 10, 2011 at 5:06 PM, Maurizio Cucchiara
> <mcucchiara@apache.org>wrote:
>
>> I'm not sure this is what you are looking for, I'm assuming you are
>> trying to write into the student bean, I hope this will be useful (it
>> works on my side):
>>
>>    @Test
>>    public void testBeanMap() throws Exception {
>>         Student student = new Student();
>>         BeanMap map = new BeanMap(student);
>>        assertNotNull(map);
>>        map.put("id", 1);
>>        map.put("course", new Course("course name"));
>>        assertEquals(1, student.getId());
>>        assertEquals("course name",student.getCourse().getName());
>>     }
>>
>>
>> Twitter     :http://www.twitter.com/m_cucchiara
>> G+          :https://plus.google.com/107903711540963855921
>> Linkedin    :http://www.linkedin.com/in/mauriziocucchiara
>>
>> Maurizio Cucchiara
>>
>>
>>
>> On 11 November 2011 01:51, Chris Pratt <thechrispratt@gmail.com> wrote:
>> > StringMapAdaptor
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message