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 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.*; 44 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 } 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 78 interface Foo1 { boolean equals(Object obj); } 79 80 @ExpectedIsFunInt(true) // Bar has one abstract non-Object method 81 interface Bar1 extends Foo1 { int compare(String o1, String o2); } 82 83 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 } 89 90 @ExpectedIsFunInt(false) // Method Object.clone is not public 91 interface Foo2 { 92 int m(); 93 Object clone(); 94 } 95 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 {} 100 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 {} 105 106 // interface X3 { int m(Iterable<String> arg); } 107 // interface Y3 { int m(Iterable<Integer> arg); } 108 // @ExpectedIsFunInt(false) // No method has a subsignature of all abstract methods 109 // interface Z3 extends X3, Y3 {} 110 111 // interface X4 { int m(Iterable<String> arg, Class c); } 112 // interface Y4 { int m(Iterable arg, Class<?> c); } 113 // @ExpectedIsFunInt(false) // No method has a subsignature of all abstract methods 114 // interface Z4 extends X4, Y4 {} 115 116 // @ExpectedIsFunInt(false) // Foo3 is _not_ functional: different signatures for m 117 // interface Foo3<T, N extends Number> { 118 // void m(T arg); 119 // void m(N arg); 120 // } 121 // @ExpectedIsFunInt(false) // Bar3 is _not_ functional: different signatures for m 122 // interface Bar3 extends Foo3<String, Integer> {} 123 // @ExpectedIsFunInt(true) // Baz3 is functional: same signature for m 124 // interface Baz3 extends Foo3<Integer, Integer> {} 125 126 interface Action<T> { 127 T doit(); 128 } 129 @ExpectedIsFunInt(true) 130 interface LocalExecutor { <T> T execute(Action<T> a); } 131 132 interface X5 { <T> T execute(Action<T> a); } 133 interface Y5 { <S> S execute(Action<S> a); } 134 @ExpectedIsFunInt(true) // Functional: signatures are "the same" 135 interface Exec5 extends X5, Y5 {}