Git Product home page Git Product logo

Comments (17)

siggemannen avatar siggemannen commented on August 19, 2024 1

Hi.
I was thinking different exception type. I'm trying to replace apache beanutils, and they throw a NestedNullException in case some of the nested params was null. But it would work with a flag or something in the BeanException, like: "boolean nestedNull", that says a property chain had a null value somewhere. Because otherwise all lib users need to add another checked exception.

What i'm looking for is to be able to just return null if any of the nested properties are null, but detect incorrect property names and throw exceptions.

Hmm, i tested hasProperty but both returned false!

X bean = new X();
System.out.println(BeanUtil.declared.hasProperty(bean, "nested.a")); // false
bean.nested = new Y(); // Nested must be inited first!
System.out.println(BeanUtil.declared.hasProperty(bean, "nested.a")); // true

from jodd-util.

siggemannen avatar siggemannen commented on August 19, 2024 1

Hi. I think the behaviour in:

nested.a -> null exception
nested.b -> not found exception
nested.a.foo -> null exception (!!!)

is good enough!

from jodd-util.

siggemannen avatar siggemannen commented on August 19, 2024 1

Hi,and thanks a lot!

I tested the snapshot and it worked fine, except for one test case i had.
Writing through a null property throws jodd.bean.exception.PropertyNotFoundBeanException.
I think for symmetry it should throw NullPropertyBeanException, what do you think?

My tests look something like this:

 //This passes
    @Test(expected=NullPropertyBeanException.class)
    public void test_utils_read_through_non_nulls_2()
    {
        X bean = new X();
        BeanUtils.getValue(bean, "nested.a");
    }
    
 //This fails with java.lang.Exception: Unexpected exception, expected<jodd.bean.exception.NullPropertyBeanException> but was<jodd.bean.exception.PropertyNotFoundBeanException>

@Test(expected=NullPropertyBeanException.class)
    public void test_utils_write_through_nulls()
    {
        X bean = new X();
        BeanUtils.setValue(bean, 5, "nested.a");
    }

from jodd-util.

igr avatar igr commented on August 19, 2024 1

Absolutely! Hold my beer... coding it :)

from jodd-util.

igr avatar igr commented on August 19, 2024

👋

Make sense! When you say "different exception", do you mean A) a different exception type or B) simply a different message?

Somewhat related: note (in case you didnt know) there is a function that detects existence of a property:

System.out.println(BeanUtil.declared.hasProperty(new X(), "nested.a")); // true
System.out.println(BeanUtil.declared.hasProperty(new X(), "nested.b")); // false

Thank you for the feedback!

from jodd-util.

igr avatar igr commented on August 19, 2024

Cool, I can refine the exception types - add different subtypes, including the new one for your case.

p.s. I will check the example with hasProperty, I am pretty sure it worked this morning :)

from jodd-util.

igr avatar igr commented on August 19, 2024

Under development, this week is just crazy :(

from jodd-util.

siggemannen avatar siggemannen commented on August 19, 2024

It's cool, take your time! I will be happy to test it out =)

from jodd-util.

igr avatar igr commented on August 19, 2024

@siggemannen

The issue I am facing is this: in both cases, the nested of the new X() is null. BeanUtil simply stops there, not capable to continue any further. Since this is the same cause, the same exception would be thrown.

Otherwise, we would need to validate the whole path. This validation is not guaranteed, as it might depend on runtime (for lists, maps etc). Next, the whole path could be longer, e.g. nested.a.foo -> this one should throw the same exception as nested.b, right?

Note that the following works:

assertTrue(BeanUtil.declared.hasProperty(new X(), "nested.a"));
assertFalse(BeanUtil.declared.hasProperty(new X(), "nested.na"));

What I can do is to actually only cover the case of the immediate child: nested.foo when nested is null. So:

nested.a -> null exception
nested.b -> not found exception
nested.a.foo -> null exception (!!!)

wdyt?

from jodd-util.

igr avatar igr commented on August 19, 2024

Here it is, the test is passing... I will release a snapshot for you to try it

from jodd-util.

igr avatar igr commented on August 19, 2024

The snapshot is published (hopfeully:)

from jodd-util.

igr avatar igr commented on August 19, 2024

Done, releasing during the day (need to migrate from travis, damn :(

from jodd-util.

igr avatar igr commented on August 19, 2024

Snapshot published 🍭

from jodd-util.

siggemannen avatar siggemannen commented on August 19, 2024

Awesome! It works for me now. Many thanks. If you have time to do a specific snap for the jodd.bean-package, it would be cool! But i can wait for proper release too

from jodd-util.

igr avatar igr commented on August 19, 2024

6.1.0 released :)

from jodd-util.

siggemannen avatar siggemannen commented on August 19, 2024

Just a little followup. I've now been running BeanUtils and it works great =) Thanks a bunch

from jodd-util.

igr avatar igr commented on August 19, 2024

Glad to hear so!!!

from jodd-util.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.