|
16 | 16 |
|
17 | 17 | package com.google.gson.common;
|
18 | 18 |
|
19 |
| -import org.junit.Assert; |
20 |
| - |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.lang.reflect.Modifier; |
21 | 21 | import java.util.Collection;
|
| 22 | +import java.util.LinkedHashSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Set; |
| 25 | +import org.junit.Assert; |
22 | 26 |
|
23 | 27 | /**
|
24 | 28 | * Handy asserts that we wish were present in {@link Assert}
|
@@ -49,4 +53,53 @@ public static void assertEqualsAndHashCode(Object a, Object b) {
|
49 | 53 | Assert.assertFalse(a.equals(null));
|
50 | 54 | Assert.assertFalse(a.equals(new Object()));
|
51 | 55 | }
|
| 56 | + |
| 57 | + private static boolean isProtectedOrPublic(Method method) { |
| 58 | + int modifiers = method.getModifiers(); |
| 59 | + return Modifier.isProtected(modifiers) || Modifier.isPublic(modifiers); |
| 60 | + } |
| 61 | + |
| 62 | + private static String getMethodSignature(Method method) { |
| 63 | + StringBuilder builder = new StringBuilder(method.getName()); |
| 64 | + builder.append('('); |
| 65 | + |
| 66 | + String sep = ""; |
| 67 | + for (Class<?> paramType : method.getParameterTypes()) { |
| 68 | + builder.append(sep).append(paramType.getName()); |
| 69 | + sep = ","; |
| 70 | + } |
| 71 | + |
| 72 | + builder.append(')'); |
| 73 | + return builder.toString(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Asserts that {@code subClass} overrides all protected and public methods declared by |
| 78 | + * {@code baseClass} except for the ones whose signatures are in {@code ignoredMethods}. |
| 79 | + */ |
| 80 | + public static void assertOverridesMethods(Class<?> baseClass, Class<?> subClass, List<String> ignoredMethods) { |
| 81 | + Set<String> requiredOverriddenMethods = new LinkedHashSet<>(); |
| 82 | + for (Method method : baseClass.getDeclaredMethods()) { |
| 83 | + // Note: Do not filter out `final` methods; maybe they should not be `final` and subclass needs |
| 84 | + // to override them |
| 85 | + if (isProtectedOrPublic(method)) { |
| 86 | + requiredOverriddenMethods.add(getMethodSignature(method)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + for (Method method : subClass.getDeclaredMethods()) { |
| 91 | + requiredOverriddenMethods.remove(getMethodSignature(method)); |
| 92 | + } |
| 93 | + |
| 94 | + for (String ignoredMethod : ignoredMethods) { |
| 95 | + boolean foundIgnored = requiredOverriddenMethods.remove(ignoredMethod); |
| 96 | + if (!foundIgnored) { |
| 97 | + throw new IllegalArgumentException("Method '" + ignoredMethod + "' does not exist or is already overridden"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (!requiredOverriddenMethods.isEmpty()) { |
| 102 | + Assert.fail(subClass.getSimpleName() + " must override these methods: " + requiredOverriddenMethods); |
| 103 | + } |
| 104 | + } |
52 | 105 | }
|
0 commit comments