TestIsFunctionalInterface.java revision 1558:01af1b5c631d
1/*
2 * Copyright (c) 2013, 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 * @build   JavacTestingAbstractProcessor TestIsFunctionalInterface
31 * @compile -processor TestIsFunctionalInterface TestIsFunctionalInterface.java
32 */
33
34import java.util.Set;
35import javax.annotation.processing.*;
36import javax.lang.model.SourceVersion;
37import static javax.lang.model.SourceVersion.*;
38import javax.lang.model.element.*;
39import javax.lang.model.util.*;
40import static javax.lang.model.util.ElementFilter.*;
41import static javax.tools.Diagnostic.Kind.*;
42import static javax.tools.StandardLocation.*;
43import java.io.*;
44
45/**
46 * Test basic workings of Elements.isFunctionalInterface
47 */
48public class TestIsFunctionalInterface extends JavacTestingAbstractProcessor {
49    private int count = 0;
50    public boolean process(Set<? extends TypeElement> annotations,
51                           RoundEnvironment roundEnv) {
52        if (!roundEnv.processingOver()) {
53            for(TypeElement type : typesIn(roundEnv.getElementsAnnotatedWith(ExpectedIsFunInt.class))) {
54                count++;
55                System.out.println(type);
56                if (elements.isFunctionalInterface(type) !=
57                    type.getAnnotation(ExpectedIsFunInt.class).value()) {
58                    messager.printMessage(ERROR,
59                                          "Mismatch between expected and computed isFunctionalInterface",
60                                          type);
61                }
62            }
63        } else {
64            if (count <= 0)
65                messager.printMessage(ERROR, "No types with ExpectedIsFunInt processed.");
66            }
67    return true;
68    }
69}
70
71@interface ExpectedIsFunInt {
72    boolean value();
73}
74
75// Examples below from the lambda specification documents.
76
77@ExpectedIsFunInt(false) // Equals is already an implicit member
78interface Foo1 { boolean equals(Object obj); }
79
80@ExpectedIsFunInt(true) // Bar has one abstract non-Object method
81interface Bar1 extends Foo1 { int compare(String o1, String o2); }
82
83
84@ExpectedIsFunInt(true) // Comparator has one abstract non-Object method
85interface LocalComparator<T> {
86 boolean equals(Object obj);
87 int compare(T o1, T o2);
88}
89
90@ExpectedIsFunInt(false) // Method Object.clone is not public
91interface Foo2 {
92  int m();
93  Object clone();
94}
95
96interface X1 { int m(Iterable<String> arg); }
97interface Y1 { int m(Iterable<String> arg); }
98@ExpectedIsFunInt(true) // Two methods, but they have the same signature
99interface Z1 extends X1, Y1 {}
100
101interface X2 { Iterable m(Iterable<String> arg); }
102interface Y2 { Iterable<String> m(Iterable arg); }
103@ExpectedIsFunInt(true) // Y.m is a subsignature & return-type-substitutable
104interface Z2 extends X2, Y2 {}
105
106interface Action<T> {
107    T doit();
108}
109@ExpectedIsFunInt(true)
110interface LocalExecutor { <T> T execute(Action<T> a); }
111
112interface X5 { <T> T execute(Action<T> a); }
113interface Y5 { <S> S execute(Action<S> a); }
114@ExpectedIsFunInt(true) // Functional: signatures are "the same"
115interface Exec5 extends X5, Y5 {}
116