• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, 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 package test.java.util.Properties;
25 
26 import java.lang.reflect.Method;
27 import java.lang.reflect.Modifier;
28 import java.util.*;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31 
32 /*
33  * @test
34  * @bug 8029891
35  * @summary Test that the Properties class overrides all public+protected
36  *          methods of all ancestor classes and interfaces
37  * @run main CheckOverrides
38  */
39 public class CheckOverrides {
40 
main(String[] args)41     public static void main(String[] args) {
42         Set<MethodSignature> pMethodSignatures =
43             Stream.of(Properties.class.getDeclaredMethods())
44                 .filter(CheckOverrides::isMethodOfInterest)
45                 .map(MethodSignature::new)
46                 .collect(Collectors.toSet());
47 
48         Map<MethodSignature, Method> unoverriddenMethods = new HashMap<>();
49         for (Class<?> superclass = Properties.class.getSuperclass();
50              superclass != Object.class;
51              superclass = superclass.getSuperclass()) {
52             Stream.of(superclass.getDeclaredMethods())
53                 .filter(CheckOverrides::isMethodOfInterest)
54                 .forEach(m -> unoverriddenMethods.putIfAbsent(new MethodSignature(m), m));
55         }
56         unoverriddenMethods.keySet().removeAll(pMethodSignatures);
57 
58         if (!unoverriddenMethods.isEmpty()) {
59             throw new RuntimeException(
60                 "The following methods should be overridden by Properties class:\n" +
61                     unoverriddenMethods.values().stream()
62                         .map(Method::toString)
63                         .collect(Collectors.joining("\n  ", "  ", "\n"))
64             );
65         }
66     }
67 
isMethodOfInterest(Method method)68     static boolean isMethodOfInterest(Method method) {
69         int mods = method.getModifiers();
70         return !Modifier.isStatic(mods) &&
71             (Modifier.isPublic(mods) || Modifier.isProtected(mods));
72     }
73 
74     static class MethodSignature {
75         final Class<?> returnType;
76         final String name;
77         final Class<?>[] parameterTypes;
78 
MethodSignature(Method method)79         MethodSignature(Method method) {
80             this(method.getReturnType(), method.getName(), method.getParameterTypes());
81         }
82 
MethodSignature(Class<?> returnType, String name, Class<?>[] parameterTypes)83         private MethodSignature(Class<?> returnType, String name, Class<?>[] parameterTypes) {
84             this.returnType = returnType;
85             this.name = name;
86             this.parameterTypes = parameterTypes;
87         }
88 
89         @Override
equals(Object o)90         public boolean equals(Object o) {
91             if (this == o) return true;
92             if (o == null || getClass() != o.getClass()) return false;
93             MethodSignature that = (MethodSignature) o;
94             if (!returnType.equals(that.returnType)) return false;
95             if (!name.equals(that.name)) return false;
96             return Arrays.equals(parameterTypes, that.parameterTypes);
97         }
98 
99         @Override
hashCode()100         public int hashCode() {
101             int result = returnType.hashCode();
102             result = 31 * result + name.hashCode();
103             result = 31 * result + Arrays.hashCode(parameterTypes);
104             return result;
105         }
106     }
107 }
108 
109