1/*
2 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8007574
27 * @summary Test Elements.isFunctionalInterface
28 * @author  Joseph D. Darcy
29 * @library /tools/javac/lib
30 * @modules java.compiler
31 *          jdk.compiler
32 * @build   JavacTestingAbstractProcessor TestIsFunctionalInterface
33 * @compile -processor TestIsFunctionalInterface TestIsFunctionalInterface.java
34 */
35
36import java.util.Set;
37import javax.annotation.processing.*;
38import javax.lang.model.SourceVersion;
39import static javax.lang.model.SourceVersion.*;
40import javax.lang.model.element.*;
41import javax.lang.model.util.*;
42import static javax.lang.model.util.ElementFilter.*;
43import static javax.tools.Diagnostic.Kind.*;
44import static javax.tools.StandardLocation.*;
45import java.io.*;
46
47/**
48 * Test basic workings of Elements.isFunctionalInterface
49 */
50public class TestIsFunctionalInterface extends JavacTestingAbstractProcessor {
51    private int count = 0;
52    public boolean process(Set<? extends TypeElement> annotations,
53                           RoundEnvironment roundEnv) {
54        if (!roundEnv.processingOver()) {
55            for(TypeElement type : typesIn(roundEnv.getElementsAnnotatedWith(ExpectedIsFunInt.class))) {
56                count++;
57                System.out.println(type);
58                if (elements.isFunctionalInterface(type) !=
59                    type.getAnnotation(ExpectedIsFunInt.class).value()) {
60                    messager.printMessage(ERROR,
61                                          "Mismatch between expected and computed isFunctionalInterface",
62                                          type);
63                }
64            }
65        } else {
66            if (count <= 0)
67                messager.printMessage(ERROR, "No types with ExpectedIsFunInt processed.");
68            }
69    return true;
70    }
71}
72
73@interface ExpectedIsFunInt {
74    boolean value();
75}
76
77// Examples below from the lambda specification documents.
78
79@ExpectedIsFunInt(false) // Equals is already an implicit member
80interface Foo1 { boolean equals(Object obj); }
81
82@ExpectedIsFunInt(true) // Bar has one abstract non-Object method
83interface Bar1 extends Foo1 { int compare(String o1, String o2); }
84
85
86@ExpectedIsFunInt(true) // Comparator has one abstract non-Object method
87interface LocalComparator<T> {
88 boolean equals(Object obj);
89 int compare(T o1, T o2);
90}
91
92@ExpectedIsFunInt(false) // Method Object.clone is not public
93interface Foo2 {
94  int m();
95  Object clone();
96}
97
98interface X1 { int m(Iterable<String> arg); }
99interface Y1 { int m(Iterable<String> arg); }
100@ExpectedIsFunInt(true) // Two methods, but they have the same signature
101interface Z1 extends X1, Y1 {}
102
103interface X2 { Iterable m(Iterable<String> arg); }
104interface Y2 { Iterable<String> m(Iterable arg); }
105@ExpectedIsFunInt(true) // Y.m is a subsignature & return-type-substitutable
106interface Z2 extends X2, Y2 {}
107
108interface Action<T> {
109    T doit();
110}
111@ExpectedIsFunInt(true)
112interface LocalExecutor { <T> T execute(Action<T> a); }
113
114interface X5 { <T> T execute(Action<T> a); }
115interface Y5 { <S> S execute(Action<S> a); }
116@ExpectedIsFunInt(true) // Functional: signatures are "the same"
117interface Exec5 extends X5, Y5 {}
118