Path: blob/master/test/jdk/java/util/Properties/CheckOverrides.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.reflect.Method;24import java.lang.reflect.Modifier;25import java.util.*;26import java.util.stream.Collectors;27import java.util.stream.Stream;2829/*30* @test31* @bug 802989132* @summary Test that the Properties class overrides all public+protected33* methods of all ancestor classes and interfaces34* @run main CheckOverrides35*/36public class CheckOverrides {3738public static void main(String[] args) {39Set<MethodSignature> pMethodSignatures =40Stream.of(Properties.class.getDeclaredMethods())41.filter(CheckOverrides::isMethodOfInterest)42.map(MethodSignature::new)43.collect(Collectors.toSet());4445Map<MethodSignature, Method> unoverriddenMethods = new HashMap<>();46for (Class<?> superclass = Properties.class.getSuperclass();47superclass != Object.class;48superclass = superclass.getSuperclass()) {49Stream.of(superclass.getDeclaredMethods())50.filter(CheckOverrides::isMethodOfInterest)51.forEach(m -> unoverriddenMethods.putIfAbsent(new MethodSignature(m), m));52}53unoverriddenMethods.keySet().removeAll(pMethodSignatures);5455if (!unoverriddenMethods.isEmpty()) {56throw new RuntimeException(57"The following methods should be overridden by Properties class:\n" +58unoverriddenMethods.values().stream()59.map(Method::toString)60.collect(Collectors.joining("\n ", " ", "\n"))61);62}63}6465static boolean isMethodOfInterest(Method method) {66int mods = method.getModifiers();67return !Modifier.isStatic(mods) &&68(Modifier.isPublic(mods) || Modifier.isProtected(mods));69}7071static class MethodSignature {72final Class<?> returnType;73final String name;74final Class<?>[] parameterTypes;7576MethodSignature(Method method) {77this(method.getReturnType(), method.getName(), method.getParameterTypes());78}7980private MethodSignature(Class<?> returnType, String name, Class<?>[] parameterTypes) {81this.returnType = returnType;82this.name = name;83this.parameterTypes = parameterTypes;84}8586@Override87public boolean equals(Object o) {88if (this == o) return true;89if (o == null || getClass() != o.getClass()) return false;90MethodSignature that = (MethodSignature) o;91if (!returnType.equals(that.returnType)) return false;92if (!name.equals(that.name)) return false;93return Arrays.equals(parameterTypes, that.parameterTypes);94}9596@Override97public int hashCode() {98int result = returnType.hashCode();99result = 31 * result + name.hashCode();100result = 31 * result + Arrays.hashCode(parameterTypes);101return result;102}103}104}105106107108