jOOQ / jOOR

jOOR - Fluent Reflection in Java jOOR is a very simple fluent API that gives access to your Java Class structures in a more intuitive way. The JDK's reflection APIs are hard and verbose to use. Other languages have much simpler constructs to access type meta information at runtime. Let us make Java reflection better.
http://www.jooq.org/products
Apache License 2.0
2.81k stars 376 forks source link

Rethrow method internal exceptions #62

Closed lars-sh closed 6 years ago

lars-sh commented 6 years ago

Expected behavior and actual behavior:

When calling a constructor or method, which is throwing an Exception, I'd expect that exception to be thrown as is - that's at least what I expect from a fluent API.

Steps to reproduce the problem:

// Currently throws a ReflectionException
// though (at least) I would expect it to throw a NullPointerException
Reflect.on(Example.class).create();

static class Example {
    Example() {
        throw new NullPointerException("Just an example!");
    }
}

Suggested change

Handling the InvocationTargetException and rethrowing its cause at below positions should handle things as described.

} catch (InvocationTargetException e) {
            throw (RuntimeException) e.getCause();

Versions:

lukaseder commented 6 years ago

There are many ways we could do this. The status quo will always wrap an exception in a ReflectException. Your option could work just as well, but I don't see a compelling enough advantage right now to change the behaviour incompatibly.

lars-sh commented 6 years ago

Sure, compatibility is a point. I just wonder what developers might expect from the API.

At least for me it's the expectation, that Reflect.on(Example.class).create().get() works mostly the same way new Example() does - except, that reflection is used to achieve things. ReflectException is what I expect once there's a problem with reflection stuff.

lukaseder commented 6 years ago

@lars-sh I would expect a third party reflection API to work like the JDK reflection API, i.e. to throw their own exceptions. Look at your sample code. You didn't catch the NPE, but a InvocationTargetException.

Again, there are many ways to do things. I don't really disagree with your approach, but the ship has sailed for jOOR, unless there are very compelling reasons to change the current behaviour.