Thu, 31 Aug 2017 15:17:03 +0800
merge
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 */
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 */
34 import java.util.Set;
35 import javax.annotation.processing.*;
36 import javax.lang.model.SourceVersion;
37 import static javax.lang.model.SourceVersion.*;
38 import javax.lang.model.element.*;
39 import javax.lang.model.util.*;
40 import static javax.lang.model.util.ElementFilter.*;
41 import static javax.tools.Diagnostic.Kind.*;
42 import static javax.tools.StandardLocation.*;
43 import java.io.*;
45 /**
46 * Test basic workings of Elements.isFunctionalInterface
47 */
48 public 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 }
71 @interface ExpectedIsFunInt {
72 boolean value();
73 }
75 // Examples below from the lambda specification documents.
77 @ExpectedIsFunInt(false) // Equals is already an implicit member
78 interface Foo1 { boolean equals(Object obj); }
80 @ExpectedIsFunInt(true) // Bar has one abstract non-Object method
81 interface Bar1 extends Foo1 { int compare(String o1, String o2); }
84 @ExpectedIsFunInt(true) // Comparator has one abstract non-Object method
85 interface LocalComparator<T> {
86 boolean equals(Object obj);
87 int compare(T o1, T o2);
88 }
90 @ExpectedIsFunInt(false) // Method Object.clone is not public
91 interface Foo2 {
92 int m();
93 Object clone();
94 }
96 interface X1 { int m(Iterable<String> arg); }
97 interface Y1 { int m(Iterable<String> arg); }
98 @ExpectedIsFunInt(true) // Two methods, but they have the same signature
99 interface Z1 extends X1, Y1 {}
101 interface X2 { Iterable m(Iterable<String> arg); }
102 interface Y2 { Iterable<String> m(Iterable arg); }
103 @ExpectedIsFunInt(true) // Y.m is a subsignature & return-type-substitutable
104 interface Z2 extends X2, Y2 {}
106 interface Action<T> {
107 T doit();
108 }
109 @ExpectedIsFunInt(true)
110 interface LocalExecutor { <T> T execute(Action<T> a); }
112 interface X5 { <T> T execute(Action<T> a); }
113 interface Y5 { <S> S execute(Action<S> a); }
114 @ExpectedIsFunInt(true) // Functional: signatures are "the same"
115 interface Exec5 extends X5, Y5 {}