Skip to content

Commit 6cfa597

Browse files
Fixed CheckStyle issues, and matched existing code style of codebase.
1 parent ee538c0 commit 6cfa597

File tree

3 files changed

+122
-139
lines changed

3 files changed

+122
-139
lines changed

hamcrest/src/main/java/org/hamcrest/reflection/Visibility.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
import java.lang.reflect.Modifier;
55
import java.util.Objects;
66

7-
public enum Visibility
8-
{
7+
/**
8+
* Represents the 4 states of visibility.
9+
*
10+
* @author JJ Brown
11+
*/
12+
enum Visibility {
913
PUBLIC("public"),
1014
PROTECTED("protected"),
1115
PACKAGE_PROTECTED("package-protected (no modifiers)"),
1216
PRIVATE("private");
1317

14-
public String getDescription()
15-
{
18+
public String getDescription() {
1619
return description;
1720
}
1821

hamcrest/src/main/java/org/hamcrest/reflection/VisibilityMatcher.java

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,51 @@
1212
* This class is intentionally not exposed to the public API, to help keep implementation details hidden (and easy to change).
1313
* Please use {@link VisibilityMatchers} to instantiate instances of this class.
1414
*
15-
* @param <T>
16-
* the type of the element being matched; could be anything
15+
* @param <T> the type of the element being matched; could be anything
16+
* @author JJ Brown
1717
* @see VisibilityMatchers
1818
*/
19-
class VisibilityMatcher<T> extends BaseMatcher<T>
20-
{
21-
private final Visibility expectedVisibility;
19+
class VisibilityMatcher<T> extends BaseMatcher<T> {
20+
private final Visibility expectedVisibility;
2221

23-
VisibilityMatcher(Visibility expectedVisibility)
24-
{
25-
this.expectedVisibility = expectedVisibility;
26-
}
22+
VisibilityMatcher(Visibility expectedVisibility) {
23+
this.expectedVisibility = expectedVisibility;
24+
}
2725

28-
@Override
29-
public boolean matches(Object actual)
30-
{
31-
if (actual == null)
32-
{
33-
return false;
34-
}
35-
if (actual instanceof Class)
36-
{
37-
return expectedVisibility == Visibility.of((Class<?>) actual);
38-
}
39-
if (actual instanceof Member)
40-
{
41-
return expectedVisibility == Visibility.of((Member) actual);
42-
}
43-
return false;
26+
@Override
27+
public boolean matches(Object actual) {
28+
if (actual == null) {
29+
return false;
4430
}
45-
46-
@Override public void describeTo(Description description)
47-
{
48-
description.appendText("is ").appendText(expectedVisibility.getDescription());
31+
if (actual instanceof Class) {
32+
return expectedVisibility == Visibility.of((Class<?>) actual);
4933
}
34+
if (actual instanceof Member) {
35+
return expectedVisibility == Visibility.of((Member) actual);
36+
}
37+
return false;
38+
}
39+
40+
@Override
41+
public void describeTo(Description description) {
42+
description.appendText("is ").appendText(expectedVisibility.getDescription());
43+
}
5044

51-
@Override public void describeMismatch(Object item, Description description)
52-
{
53-
if (item == null)
54-
{
55-
description.appendText("was null");
56-
}
57-
else if (item instanceof Class)
58-
{
59-
description.appendText("was a ")
60-
.appendText(Visibility.of((Class<?>) item).getDescription())
61-
.appendText(" class");
62-
}
63-
else if (item instanceof Member)
64-
{
65-
description.appendText("was a ")
66-
.appendText(Visibility.of((Member) item).getDescription())
67-
.appendText(" ")
68-
.appendText(item.getClass().getName());
69-
}
70-
else
71-
{
72-
description.appendText("was " + item.getClass().getName() + " instead of a reflective element like a Class<T>, Constructor<T>, or Method");
73-
}
45+
@Override
46+
public void describeMismatch(Object item, Description description) {
47+
if (item == null) {
48+
description.appendText("was null");
49+
} else if (item instanceof Class) {
50+
description.appendText("was a ")
51+
.appendText(Visibility.of((Class<?>) item).getDescription())
52+
.appendText(" class");
53+
} else if (item instanceof Member) {
54+
description.appendText("was a ")
55+
.appendText(Visibility.of((Member) item).getDescription())
56+
.appendText(" ")
57+
.appendText(item.getClass().getName());
58+
} else {
59+
description.appendText("was " + item.getClass().getName() + " instead of a reflective element like a Class<T>, Constructor<T>, or Method");
7460
}
61+
}
7562
}

hamcrest/src/main/java/org/hamcrest/reflection/VisibilityMatchers.java

Lines changed: 76 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,92 +5,85 @@
55
/**
66
* Defines matchers that check the visibility of reflective objects like {@link java.lang.Class} or {@link java.lang.reflect.Method}.
77
* {@code null} values never match, nor do normal objects; these simply do not match, without raising an Exception.
8+
*
9+
* @author JJ Brown
810
*/
9-
public class VisibilityMatchers
10-
{
11-
// Each matcher is stateless and can match any type, so the individual instances are only made once and stored here for re-use.
12-
private static final VisibilityMatcher<?> PUBLIC = new VisibilityMatcher<>(Visibility.PUBLIC);
13-
private static final VisibilityMatcher<?> PROTECTED = new VisibilityMatcher<>(Visibility.PROTECTED);
14-
private static final VisibilityMatcher<?> PACKAGE_PROTECTED = new VisibilityMatcher<>(Visibility.PACKAGE_PROTECTED);
15-
private static final VisibilityMatcher<?> PRIVATE = new VisibilityMatcher<>(Visibility.PRIVATE);
11+
public class VisibilityMatchers {
12+
// Each matcher is stateless and can match any type, so the individual instances are only made once and stored here for re-use.
13+
private static final VisibilityMatcher<?> PUBLIC = new VisibilityMatcher<>(Visibility.PUBLIC);
14+
private static final VisibilityMatcher<?> PROTECTED = new VisibilityMatcher<>(Visibility.PROTECTED);
15+
private static final VisibilityMatcher<?> PACKAGE_PROTECTED = new VisibilityMatcher<>(Visibility.PACKAGE_PROTECTED);
16+
private static final VisibilityMatcher<?> PRIVATE = new VisibilityMatcher<>(Visibility.PRIVATE);
1617

17-
/**
18-
* Matchers reflective elements that have public visibility.
19-
* Specifically, this matcher only matches elements marked with the keyword {@code public}.
20-
* <br>
21-
* This method matches {@link Class} objects or other {@link java.lang.reflect.Member reflective objects}
22-
* like {@link java.lang.reflect.Field} or {@link java.lang.reflect.Method} used in reflection.
23-
* Any other kind of object, or {@code null} values, do not match (but will not cause an Exception).
24-
*
25-
* @param <T>
26-
* 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-
public static <T> Matcher<T> isPublic()
31-
{
32-
// Each matcher is stateless and can match any type (the generic <T> is for type safety at the use site),
33-
// so it's fine to cast the non-reifiable generic type here at runtime and re-use the same instance.
34-
return (Matcher<T>) PUBLIC;
35-
}
18+
/**
19+
* 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+
public static <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+
}
3635

37-
/**
38-
* Matchers reflective elements that have protected visibility.
39-
* Specifically, this matcher only matches elements marked with the keyword {@code protected}; it does NOT match public or private elements.
40-
* <br>
41-
* This method matches {@link Class} objects or other {@link java.lang.reflect.Member reflective objects}
42-
* like {@link java.lang.reflect.Field} or {@link java.lang.reflect.Method} used in reflection.
43-
* Any other kind of object, or {@code null} values, do not match (but will not cause an Exception).
44-
*
45-
* @param <T>
46-
* the type of the object being matched
47-
* @return a matcher that matches reflective elements with exactly the given level of visibility
48-
*/
49-
@SuppressWarnings("unchecked")
50-
public static <T> Matcher<T> isProtected()
51-
{
52-
// Each matcher is stateless and can match any type (the generic <T> is for type safety at the use site),
53-
// so it's fine to cast the non-reifiable generic type here at runtime and re-use the same instance.
54-
return (Matcher<T>) PROTECTED;
55-
}
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+
public static <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+
}
5653

57-
/**
58-
* Matchers reflective elements that have package-protected visibility.
59-
* Specifically, this matcher only matches elements not marked with any of the visibility keywords {@code public}, {@code protected}, or {@code private}.
60-
* <br>
61-
* This method matches {@link Class} objects or other {@link java.lang.reflect.Member reflective objects}
62-
* like {@link java.lang.reflect.Field} or {@link java.lang.reflect.Method} used in reflection.
63-
* Any other kind of object, or {@code null} values, do not match (but will not cause an Exception).
64-
*
65-
* @param <T>
66-
* the type of the object being matched
67-
* @return a matcher that matches reflective elements with exactly the given level of visibility
68-
*/
69-
@SuppressWarnings("unchecked")
70-
public static <T> Matcher<T> isPackageProtected()
71-
{
72-
// Each matcher is stateless and can match any type (the generic <T> is for type safety at the use site),
73-
// so it's fine to cast the non-reifiable generic type here at runtime and re-use the same instance.
74-
return (Matcher<T>) PACKAGE_PROTECTED;
75-
}
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
64+
*/
65+
@SuppressWarnings("unchecked")
66+
public static <T> Matcher<T> isPackageProtected() {
67+
// Each matcher is stateless and can match any type (the generic <T> is for type safety at the use site),
68+
// so it's fine to cast the non-reifiable generic type here at runtime and re-use the same instance.
69+
return (Matcher<T>) PACKAGE_PROTECTED;
70+
}
7671

77-
/**
78-
* Matchers reflective elements that have private visibility.
79-
* Specifically, this matcher only matches elements marked with the keyword {@link private}.
80-
* <br>
81-
* This method matches {@link Class} objects or other {@link java.lang.reflect.Member reflective objects}
82-
* like {@link java.lang.reflect.Field} or {@link java.lang.reflect.Method} used in reflection.
83-
* Any other kind of object, or {@code null} values, do not match (but will not cause an Exception).
84-
*
85-
* @param <T>
86-
* the type of the object being matched
87-
* @return a matcher that matches reflective elements with exactly the given level of visibility
88-
*/
89-
@SuppressWarnings("unchecked")
90-
public static <T> Matcher<T> isPrivate()
91-
{
92-
// Each matcher is stateless and can match any type (the generic <T> is for type safety at the use site),
93-
// so it's fine to cast the non-reifiable generic type here at runtime and re-use the same instance.
94-
return (Matcher<T>) PRIVATE;
95-
}
72+
/**
73+
* Matchers reflective elements that have private visibility.
74+
* Specifically, this matcher only matches elements marked with the keyword {@link private}.
75+
* <br>
76+
* This method matches {@link Class} objects or other {@link java.lang.reflect.Member reflective objects}
77+
* like {@link java.lang.reflect.Field} or {@link java.lang.reflect.Method} used in reflection.
78+
* Any other kind of object, or {@code null} values, do not match (but will not cause an Exception).
79+
*
80+
* @param <T> the type of the object being matched
81+
* @return a matcher that matches reflective elements with exactly the given level of visibility
82+
*/
83+
@SuppressWarnings("unchecked")
84+
public static <T> Matcher<T> isPrivate() {
85+
// Each matcher is stateless and can match any type (the generic <T> is for type safety at the use site),
86+
// so it's fine to cast the non-reifiable generic type here at runtime and re-use the same instance.
87+
return (Matcher<T>) PRIVATE;
88+
}
9689
}

0 commit comments

Comments
 (0)