okaywit / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Make Futures.preferringStrings newWithCause more robust #1459

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Sometimes Subclasses of Throwable pass null as cause when no cause is passed in 
a constructor (see Jersey WebApplicationException for example).

Sorting of constructors can lead to single-arg String constructor being used 
instead of a (String, Throwable) constructor - the subsequent initCause()-call 
(Futures:1408) fails in that case.

Proposal (sorry no patch handy): Replace WITH_STRING_PARAM_FIRST:

private static final Ordering<Constructor<?>> WITH_STRING_PARAM_FIRST = Ordering
    .natural().onResultOf(new Function<Constructor<?>, Integer>() {
        @Override
        public Integer apply(Constructor<?> input) {
            int points = 0;
            List<Class<?>> list = asList(input.getParameterTypes());
            if (list.contains(String.class)) {
                points++;
                points++;
            }
            if (list.contains(Throwable.class)) {
                points++;
            }
            return points;
        }
    }).reverse();

References:

Futures line 1408: 
https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/com
mon/util/concurrent/Futures.java#1408

Jersey bug: https://java.net/jira/browse/JERSEY-1954 

Original issue reported on code.google.com by georg.koester@gmail.com on 27 Jun 2013 at 9:30

GoogleCodeExporter commented 9 years ago
The whole system of exception chaining is a little strange, and I can imagine 
that this problem will come up elsewhere. We might as well work around it as 
you suggest, since there's little cost to us. Thanks for reporting to Jersey, 
as well.

Original comment by cpov...@google.com on 27 Jun 2013 at 3:14

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08