langtools/test/tools/javac/generics/diamond/neg/Neg15.java
author sadayapalam
Thu, 26 Nov 2015 17:38:15 +0530
changeset 34474 14deea5f86f1
parent 29776 984a79b71cfe
child 36273 a19af5725d76
permissions -rw-r--r--
8078660: Misleading recommendation from diamond finder. Summary: javac should recommend diamond usage if that could lead to a change in program behavior. Reviewed-by: mcimadamore

/*
 * @test /nodynamiccopyright/
 * @bug 8062373
 *
 * @summary  Test that javac complains when a <> inferred class contains a public method that does override a supertype method.
 * @author sadayapalam
 * @compile/fail/ref=Neg15.out Neg15.java -XDrawDiagnostics
 *
 */

class Neg15 {

    interface Predicate<T> {
        default boolean test(T t) {
            System.out.println("Default method");
            return false;
        }
    }


    static void someMethod(Predicate<? extends Number> p) {
        if (!p.test(null))
            throw new Error("Blew it");
    }

    public static void main(String[] args) {

        someMethod(new Predicate<Integer>() {
            public boolean test(Integer n) {
                System.out.println("Override");
                return true;
            }
            boolean test(Integer n, int i) {
                System.out.println("Override");
                return true;
            }
            protected boolean test(Integer n, int i, int j) {
                System.out.println("Override");
                return true;
            }
            private boolean test(Integer n, int i, long j) {
                System.out.println("Override");
                return true;
            }
        });

        someMethod(new Predicate<>() {
            public boolean test(Integer n) { // bad.
                System.out.println("Override");
                return true;
            }
            boolean test(Integer n, int i) { // bad, package access.
                System.out.println("Override");
                return true;
            }
            protected boolean test(Integer n, int i, int j) { // bad, protected access.
                System.out.println("Override");
                return true;
            }
            private boolean test(Integer n, int i, long j) { // OK, private method.
                System.out.println("Override");
                return true;
            }
        });
    }
}