1 /*
   2  * Copyright (c) 2012 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 8005042
  27  * @summary Check behavior of Method.isDefault
  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import java.lang.reflect.*;
  32 import java.lang.annotation.*;
  33 import java.util.*;
  34 
  35 public class IsDefaultTest {
  36     public static void main(String... argv) throws Exception {
  37         int failures = 0;
  38         int visitationCount = 0;
  39 
  40         List<Class<?>> classList = new ArrayList<>();
  41         classList.add(TestType1.class);
  42         classList.add(TestType2.class);
  43         classList.add(TestType3.class);
  44         classList.add(TestType4.class);
  45 
  46         for(Class<?> clazz: classList) {
  47             for(Method method: clazz.getDeclaredMethods()) {
  48                 ExpectedIsDefault expectedIsDefault = method.getAnnotation(ExpectedIsDefault.class);
  49                 if (expectedIsDefault != null) {
  50                     visitationCount++;
  51                     boolean expected = expectedIsDefault.value();
  52                     boolean actual   = method.isDefault();
  53 
  54                     if (actual != expected) {
  55                         failures++;
  56                         System.err.printf("ERROR: On %s expected isDefualt of ''%s''; got ''%s''.\n",
  57                                           method.toString(), expected, actual);
  58                     }
  59                 }
  60             }
  61         }
  62 
  63         if (visitationCount == 0) {
  64             System.err.println("Test failed because no methods checked.");
  65             throw new RuntimeException();
  66         }
  67 
  68         if (failures > 0) {
  69             System.err.println("Test failed.");
  70             throw new RuntimeException();
  71         }
  72     }
  73 }
  74 
  75 interface TestType1 {
  76     @ExpectedIsDefault(false)
  77     void foo();
  78 
  79     @ExpectedIsDefault(true)
  80     default void bar() {}; // Default method
  81 }
  82 
  83 class TestType2 {
  84     @ExpectedIsDefault(false)
  85     void bar() {};
  86 }
  87 
  88 class TestType3 implements TestType1 {
  89     @ExpectedIsDefault(false)
  90     public void foo(){}
  91 
  92     @ExpectedIsDefault(false)
  93     @Override
  94     public void bar() {};
  95 }
  96 
  97 @interface TestType4 {
  98     @ExpectedIsDefault(false)
  99     String value();
 100 
 101     @ExpectedIsDefault(false)
 102     String anotherValue() default "";
 103 }
 104 
 105 @Retention(RetentionPolicy.RUNTIME)
 106 @interface ExpectedIsDefault {
 107     boolean value();
 108 }