This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 253891 - Use anonymous inner class hint unable to derive return type of method
Summary: Use anonymous inner class hint unable to derive return type of method
Status: RESOLVED DUPLICATE of bug 254165
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0.2
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-07-28 11:12 UTC by Jachym_Vojtek
Modified: 2015-09-24 14:33 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jachym_Vojtek 2015-07-28 11:12:34 UTC
package lambdatests;

public class UnableToDeriveReturnType {

    void test4() {
        foo((paramToCheck) -> {
            if (paramToCheck != null) {
                return true;
            } else {
                return "String";
            }
        }, "b");
    }

    <T, R> void foo(PossibleFunctionalInterface<T, R> pfi, T paramToCheck) {
        R res = pfi.doSomething(paramToCheck);
        System.out.println(res.toString());
    }

    interface PossibleFunctionalInterface<T, R> {
        R doSomething(T paramToCheck);
    }
}

Use anonymous inner class applied on the lambda specified in method test4 creates code:
    void test4() {
        foo(new PossibleFunctionalInterface<String, >() {

            public doSomething(String paramToCheck) {
                if (paramToCheck != null) {
                    return true;
                } else {
                    return "String";
                }
            }
        }, "b");
    }

which is not compilable.

I would expect that in this case the resulting code should be:
    void test4() {
        foo(new PossibleFunctionalInterface<String, Object>() {

            public Object doSomething(String paramToCheck) {
                if (paramToCheck != null) {
                    return true;
                } else {
                    return "String";
                }
            }
        }, "b");
    }
Comment 1 Jachym_Vojtek 2015-07-29 07:43:14 UTC
Surprisingly NetBeans can infer the suitable types in the following example:

interface GenericTypesUsed<R, P extends R> {
    R doSomething(P param);
}

<R, P extends R> void genericTypesUsed(GenericTypesUsed<R, P> gtu, P param) {
    R res = gtu.doSomething(param);
    System.out.println(res.toString());
}

void testGenericTypesUsed() {
    genericTypesUsed((ArrayList p) -> {
        if (System.currentTimeMillis() % 2 == 0) {
            return Collections.EMPTY_LIST;
        } else {
            return new java.util.Date();
        }
    }, new ArrayList());
}

=>

void testGenericTypesUsed() {
    genericTypesUsed(new GenericTypesUsed<Object, ArrayList>() {
        public Object doSomething(ArrayList p) {
            if (System.currentTimeMillis() % 2 == 0) {
                return Collections.EMPTY_LIST;
            } else {
                return new java.util.Date();
            }
        }
    }, new ArrayList());
}
Comment 2 Svata Dedic 2015-09-24 14:33:13 UTC

*** This bug has been marked as a duplicate of bug 254165 ***