You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added matchers to check the visibility (public/private) of various reflective elements.
This is helpful, for example, when enforcing the scope of a public-facing API with a test,
and provides stronger documentation for the future than mere comments.
* Matchers reflective elements that have public visibility.
20
+
* Specifically, this matcher only matches elements marked with the keyword {@code public}.
21
+
* <br>
22
+
* This method matches {@link Class} objects or other {@link java.lang.reflect.Member reflective objects}
23
+
* like {@link java.lang.reflect.Field} or {@link java.lang.reflect.Method} used in reflection.
24
+
* Any other kind of object, or {@code null} values, do not match (but will not cause an Exception).
25
+
*
26
+
* @param <T> the type of the object being matched
27
+
* @return a matcher that matches reflective elements with exactly the given level of visibility
28
+
*/
29
+
@SuppressWarnings("unchecked")
30
+
publicstatic <T> Matcher<T> isPublic() {
31
+
// Each matcher is stateless and can match any type (the generic <T> is for type safety at the use site),
32
+
// so it's fine to cast the non-reifiable generic type here at runtime and re-use the same instance.
33
+
return (Matcher<T>) PUBLIC;
34
+
}
35
+
36
+
/**
37
+
* Matchers reflective elements that have protected visibility.
38
+
* Specifically, this matcher only matches elements marked with the keyword {@code protected}; it does NOT match public or private elements.
39
+
* <br>
40
+
* This method matches {@link Class} objects or other {@link java.lang.reflect.Member reflective objects}
41
+
* like {@link java.lang.reflect.Field} or {@link java.lang.reflect.Method} used in reflection.
42
+
* Any other kind of object, or {@code null} values, do not match (but will not cause an Exception).
43
+
*
44
+
* @param <T> the type of the object being matched
45
+
* @return a matcher that matches reflective elements with exactly the given level of visibility
46
+
*/
47
+
@SuppressWarnings("unchecked")
48
+
publicstatic <T> Matcher<T> isProtected() {
49
+
// Each matcher is stateless and can match any type (the generic <T> is for type safety at the use site),
50
+
// so it's fine to cast the non-reifiable generic type here at runtime and re-use the same instance.
51
+
return (Matcher<T>) PROTECTED;
52
+
}
53
+
54
+
/**
55
+
* Matchers reflective elements that have package-protected visibility.
56
+
* Specifically, this matcher only matches elements not marked with any of the visibility keywords {@code public}, {@code protected}, or {@code private}.
57
+
* <br>
58
+
* This method matches {@link Class} objects or other {@link java.lang.reflect.Member reflective objects}
59
+
* like {@link java.lang.reflect.Field} or {@link java.lang.reflect.Method} used in reflection.
60
+
* Any other kind of object, or {@code null} values, do not match (but will not cause an Exception).
61
+
*
62
+
* @param <T> the type of the object being matched
63
+
* @return a matcher that matches reflective elements with exactly the given level of visibility
assertMismatchDescription("was java.lang.Object instead of a reflective element like a Class<T>, Constructor<T>, or Method", isPackageProtected(), newObject());
0 commit comments