|
6 | 6 | public final class MethodValidator { |
7 | 7 |
|
8 | 8 | private final ImmutableSet<Method> allowedMethods; |
9 | | - private final ImmutableSet<Class<?>> allowedDeclaredMethodsFromClasses; |
10 | | - private final ImmutableSet<String> allowedDeclaredMethodsFromPackages; |
11 | | - private final ImmutableSet<Class<?>> allowedResultClasses; |
12 | | - private final ImmutableSet<String> allowedResultPackages; |
| 9 | + private final ImmutableSet<String> allowedDeclaredMethodsFromCanonicalClassPrefixes; |
| 10 | + private final ImmutableSet<String> allowedDeclaredMethodsFromCanonicalClassNames; |
| 11 | + private final ImmutableSet<String> allowedResultCanonicalClassPrefixes; |
| 12 | + private final ImmutableSet<String> allowedResultCanonicalClassNames; |
13 | 13 |
|
14 | 14 | public static MethodValidator create(MethodValidatorConfig methodValidatorConfig) { |
15 | 15 | return new MethodValidator(methodValidatorConfig); |
16 | 16 | } |
17 | 17 |
|
18 | 18 | private MethodValidator(MethodValidatorConfig methodValidatorConfig) { |
19 | 19 | this.allowedMethods = methodValidatorConfig.allowedMethods(); |
20 | | - this.allowedDeclaredMethodsFromClasses = |
21 | | - methodValidatorConfig.allowedDeclaredMethodsFromClasses(); |
22 | | - this.allowedDeclaredMethodsFromPackages = |
23 | | - methodValidatorConfig.allowedDeclaredMethodsFromPackages(); |
24 | | - this.allowedResultClasses = methodValidatorConfig.allowedResultClasses(); |
25 | | - this.allowedResultPackages = methodValidatorConfig.allowedResultPackages(); |
| 20 | + this.allowedDeclaredMethodsFromCanonicalClassPrefixes = |
| 21 | + methodValidatorConfig.allowedDeclaredMethodsFromCanonicalClassPrefixes(); |
| 22 | + this.allowedDeclaredMethodsFromCanonicalClassNames = |
| 23 | + methodValidatorConfig.allowedDeclaredMethodsFromCanonicalClassNames(); |
| 24 | + this.allowedResultCanonicalClassPrefixes = |
| 25 | + ImmutableSet |
| 26 | + .<String>builder() |
| 27 | + .addAll(methodValidatorConfig.allowedResultCanonicalClassPrefixes()) |
| 28 | + .addAll(methodValidatorConfig.allowedDeclaredMethodsFromCanonicalClassPrefixes()) |
| 29 | + .build(); |
| 30 | + this.allowedResultCanonicalClassNames = |
| 31 | + ImmutableSet |
| 32 | + .<String>builder() |
| 33 | + .addAll(methodValidatorConfig.allowedResultCanonicalClassNames()) |
| 34 | + .addAll(methodValidatorConfig.allowedDeclaredMethodsFromCanonicalClassNames()) |
| 35 | + .build(); |
26 | 36 | } |
27 | 37 |
|
28 | 38 | public Method validateMethod(Method m) { |
| 39 | + if (m == null) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + String canonicalDeclaringClassName = m.getDeclaringClass().getCanonicalName(); |
29 | 43 | return ( |
30 | | - m == null || |
31 | 44 | allowedMethods.contains(m) || |
32 | | - allowedDeclaredMethodsFromClasses.contains(m.getDeclaringClass()) || |
33 | | - allowedDeclaredMethodsFromPackages |
| 45 | + allowedDeclaredMethodsFromCanonicalClassNames.contains( |
| 46 | + canonicalDeclaringClassName |
| 47 | + ) || |
| 48 | + allowedDeclaredMethodsFromCanonicalClassPrefixes |
34 | 49 | .stream() |
35 | | - .anyMatch(p -> m.getDeclaringClass().getPackageName().startsWith(p)) |
| 50 | + .anyMatch(canonicalDeclaringClassName::startsWith) |
36 | 51 | ) |
37 | 52 | ? m |
38 | 53 | : null; |
39 | 54 | } |
40 | 55 |
|
41 | 56 | public Object validateResult(Object o) { |
| 57 | + if (o == null) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + String canonicalClassName = o.getClass().getCanonicalName(); |
42 | 61 | return ( |
43 | | - o == null || |
44 | | - allowedResultClasses.contains(o.getClass()) || |
45 | | - allowedResultPackages |
46 | | - .stream() |
47 | | - .anyMatch(p -> o.getClass().getPackageName().startsWith(p)) |
48 | | - ); |
| 62 | + allowedResultCanonicalClassNames.contains(canonicalClassName) || |
| 63 | + allowedResultCanonicalClassPrefixes |
| 64 | + .stream() |
| 65 | + .anyMatch(canonicalClassName::startsWith) |
| 66 | + ) |
| 67 | + ? o |
| 68 | + : null; |
49 | 69 | } |
50 | 70 | } |
0 commit comments