[resteasy-dev] Lambda question

Steven Schlansker sschlansker at opentable.com
Mon Apr 3 14:45:59 EDT 2017


> On Apr 3, 2017, at 11:07 AM, Rebecca Searls <rsearls at redhat.com> wrote:
> 
> 
> This may be the cause as discussed here
> http://stackoverflow.com/questions/22588518/lambda-expression-and-generic-method
> 
>      You can't use a lambda expression for a functional interface, if the method in the functional 
>      interface has type parameters.  JSE spec section:
>      http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.27.3

I think that's slightly different -- that is if *the method has type parameters*
whereas in most cases it is the *class* that has the type parameter.

> 
> 
> 
> 
> ----- Original Message -----
>> From: "Ron Sigal" <rsigal at redhat.com>
>> To: resteasy-dev at lists.jboss.org
>> Sent: Monday, April 3, 2017 1:11:38 PM
>> Subject: [resteasy-dev] Lambda question
>> 
>> 
>> 
>> This question arises in https://issues.jboss.org/browse/RESTEASY-1209
>> "org.jboss.resteasy.util.Types#getActualTypeArgumentsOfAnInterface not
>> working for lambda's." The reporter wants to do this:
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> ResteasyProviderFactory factory = new ResteasyProviderFactory();
>> factory.register((ContextResolver<String>) type -> "foo bar");
>> 
>> 
>> 
>> but it doesn't work. I find that if I do this:
>> 
>> 
>> 
>> ContextResolver<String> resolver1 = new ContextResolver<String>() {
>> 
>> @Override
>> public String getContext(Class<?> type)
>> {
>> return null;
>> }
>> };
>> for (Type type : resolver1.getClass().getGenericInterfaces())
>> {
>> System.out.println(type);
>> }
>> I get
>> 
>> 
>> 
>> javax.ws.rs.ext.ContextResolver<java.lang.String>
>> but if I do
>> 
>> 
>> 
>> ContextResolver<String> resolver2 = type -> "foo";
>> for (Type type : resolver2.getClass().getGenericInterfaces())
>> {
>> System.out.println(type);
>> }
>> I get
>> 
>> 
>> 
>> interface javax.ws.rs.ext.ContextResolver
>> Same with
>> 
>> 
>> 
>> ContextResolver<String> resolver2 = ((ContextResolver<String>)type -> "foo");
>> So, it seems that, for lambdas, Java doesn't keep the implementation type and
>> value of the type variable at run time. Does anyone have any ideas or
>> tricks?
>> 
>> Thanks,
>> Ron
>> 
>> 
>> 
>> 
>> --
>> My company's smarter than your company (unless you work for Red Hat)
>> 
>> _______________________________________________
>> resteasy-dev mailing list
>> resteasy-dev at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/resteasy-dev
>> 
> _______________________________________________
> resteasy-dev mailing list
> resteasy-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/resteasy-dev




More information about the resteasy-dev mailing list