1 /*
   2  * Copyright (c) 2019, 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 8222430
  27  * @summary Test various predicates of ElementKind.
  28  */
  29 
  30 import java.util.Set;
  31 import java.util.function.Predicate;
  32 import javax.lang.model.element.ElementKind;
  33 
  34 /**
  35  * Test the isClass, isField, and isInterface predicates of ElementKind.
  36  */
  37 public class TestElementKindPredicates {
  38     public static void main(String... args) {
  39         Set<ElementKind> ALL_KINDS = Set.of(ElementKind.values());
  40 
  41         // isClass: Returns true if this is a kind of class: either CLASS or ENUM.
  42         test(ALL_KINDS,
  43              (ElementKind k) -> Set.of(ElementKind.CLASS,
  44                                        ElementKind.ENUM,
  45                                        ElementKind.RECORD).contains(k),
  46              (ElementKind k) -> k.isClass(), "isClass");
  47 
  48         // isField: Returns true if this is a kind of field: either FIELD or ENUM_CONSTANT.
  49         test(ALL_KINDS,
  50              (ElementKind k) -> Set.of(ElementKind.FIELD,
  51                                        ElementKind.ENUM_CONSTANT).contains(k),
  52              (ElementKind k) -> k.isField(), "isField");
  53 
  54         // isInterface: Returns true if this is a kind of interface: either INTERFACE or ANNOTATION_TYPE.
  55         test(ALL_KINDS,
  56              (ElementKind k) -> Set.of(ElementKind.INTERFACE,
  57                                        ElementKind.ANNOTATION_TYPE).contains(k),
  58              (ElementKind k) -> k.isInterface(), "isInterface");
  59     }
  60 
  61     private static void test(Set<ElementKind> kinds,
  62                              Predicate<ElementKind> expectedPred,
  63                              Predicate<ElementKind> actualPred,
  64                              String errorMessage) {
  65         for(ElementKind kind : kinds) {
  66             boolean expected = expectedPred.test(kind);
  67             boolean actual = actualPred.test(kind);
  68 
  69             if (expected != actual) {
  70                 throw new RuntimeException("Error testing ElementKind." + errorMessage + "(" +  kind +
  71                                            "):\texpected " + expected + "\tgot " + actual);
  72             }
  73         }
  74     }
  75 }