test/tools/javac/processing/model/util/elements/TestIsFunctionalInterface.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial