IsDefaultTest.java revision 9330:8b1f1c2a400f
1/*
2 * Copyright (c) 2012, 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 8005042
27 * @summary Check behavior of Method.isDefault
28 * @author Joseph D. Darcy
29 */
30
31import java.lang.reflect.*;
32import java.lang.annotation.*;
33import java.util.*;
34
35public 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        classList.add(TestType2.nestedTestType2.class);
46        classList.add(TestType5.class);
47        classList.add(TestType5.nestedTestType5.class);
48        classList.add(TestType6.class);
49        classList.add(TestType6.nestedTestType6.class);
50        classList.add(TestType7.class);
51
52        for(Class<?> clazz: classList) {
53            for(Method method: clazz.getDeclaredMethods()) {
54                ExpectedIsDefault expectedIsDefault = method.getAnnotation(ExpectedIsDefault.class);
55                if (expectedIsDefault != null) {
56                    visitationCount++;
57                    boolean expected = expectedIsDefault.value();
58                    boolean actual   = method.isDefault();
59
60                    if (actual != expected) {
61                        failures++;
62                        System.err.printf("ERROR: On %s expected isDefault of ''%s''; got ''%s''.\n",
63                                          method.toString(), expected, actual);
64                    }
65                }
66            }
67        }
68
69        if (visitationCount == 0) {
70            System.err.println("Test failed because no methods checked.");
71            throw new RuntimeException();
72        }
73
74        if (failures > 0) {
75            System.err.println("Test failed.");
76            throw new RuntimeException();
77        }
78    }
79}
80
81interface TestType1 {
82    @ExpectedIsDefault(false)
83    void foo();
84
85    @ExpectedIsDefault(true)
86    default void bar() {}; // Default method
87
88    @ExpectedIsDefault(true)
89    default void bar(int i) {}; // Default method
90
91    @ExpectedIsDefault(true)
92    default void bar(String i) {}; // Default method
93}
94
95class TestType2 {
96    @ExpectedIsDefault(false)
97    void bar() {};
98
99    interface nestedTestType2 {
100        @ExpectedIsDefault(true)
101        default void nestedBar() {};
102   }
103}
104
105class TestType3 implements TestType1 {
106    @ExpectedIsDefault(false)
107    public void foo(){}
108
109    @ExpectedIsDefault(false)
110    @Override
111    public void bar() {};
112
113    @ExpectedIsDefault(false)
114    @Override
115    public void bar(int i) {};
116}
117
118@interface TestType4 {
119    @ExpectedIsDefault(false)
120    String value();
121
122    @ExpectedIsDefault(false)
123    String anotherValue() default "";
124}
125
126interface TestType5 {
127    @ExpectedIsDefault(false)
128    abstract void aFoo();
129
130    @ExpectedIsDefault(false)
131    static void sFoo() {};
132
133    @ExpectedIsDefault(true)
134    public default void pBar() {};
135
136    @ExpectedIsDefault(true)
137    public default String sBar() {return "";};
138
139    interface nestedTestType5{
140        @ExpectedIsDefault(false)
141        void nestedFoo();
142
143        @ExpectedIsDefault(true)
144        default void nestedBar() {};
145    }
146}
147
148class TestType6{
149    interface nestedTestType6 {
150        @ExpectedIsDefault(true)
151        default void nestedBar() {};
152
153        @ExpectedIsDefault(false)
154        void nestedFoo();
155   }
156
157    @ExpectedIsDefault(false)
158    void foo(nestedTestType6 n) {}
159}
160
161class TestType7 implements TestType6.nestedTestType6 {
162
163    @ExpectedIsDefault(false)
164    public void nestedFoo() {}
165
166    @ExpectedIsDefault(false)
167    @Override
168    public void nestedBar() {};
169}
170
171@Retention(RetentionPolicy.RUNTIME)
172@interface ExpectedIsDefault {
173    boolean value();
174}
175