Skip to content

Commit 96972e3

Browse files
Drastically shrank implementation by refactoring common code structure into a common class.
1 parent 6b880bf commit 96972e3

File tree

13 files changed

+195
-268
lines changed

13 files changed

+195
-268
lines changed

hamcrest/src/main/java/org/hamcrest/visibility/AbstractVisibilityMatcher.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

hamcrest/src/main/java/org/hamcrest/visibility/IsPackageProtected.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

hamcrest/src/main/java/org/hamcrest/visibility/IsPrivate.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

hamcrest/src/main/java/org/hamcrest/visibility/IsProtected.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

hamcrest/src/main/java/org/hamcrest/visibility/IsPublic.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.hamcrest.visibility;
2+
3+
public enum Visibility
4+
{
5+
PUBLIC("public"),
6+
PROTECTED("protected"),
7+
PACKAGE_PROTECTED("package-protected (no modifiers)"),
8+
PRIVATE("private");
9+
10+
public String getDescription()
11+
{
12+
return description;
13+
}
14+
15+
private final String description;
16+
17+
Visibility(String description){
18+
this.description = description;
19+
}
20+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.hamcrest.visibility;
2+
3+
import org.hamcrest.BaseMatcher;
4+
import org.hamcrest.Description;
5+
6+
import java.lang.reflect.Member;
7+
8+
/**
9+
* Matches the visibility of a reflective element, like a {@link Class} or a {@link java.lang.reflect.Method},
10+
* to make assertions about the scope of a module's API.
11+
*
12+
* @param <T> the type of the element being matched; could be anything
13+
* @see VisibilityMatchers
14+
*/
15+
class VisibilityMatcher<T> extends BaseMatcher<T>
16+
{
17+
private final Visibility expectedVisibility;
18+
19+
VisibilityMatcher(Visibility expectedVisibility)
20+
{
21+
this.expectedVisibility = expectedVisibility;
22+
}
23+
24+
@Override
25+
public boolean matches(Object actual)
26+
{
27+
if (actual == null)
28+
{
29+
return false;
30+
}
31+
if (actual instanceof Class)
32+
{
33+
return expectedVisibility == VisibilityUtils.getVisibility((Class<?>) actual);
34+
}
35+
if (actual instanceof Member)
36+
{
37+
return expectedVisibility == VisibilityUtils.getVisibility((Member) actual);
38+
}
39+
return false;
40+
}
41+
42+
@Override public void describeTo(Description description)
43+
{
44+
description.appendText("is ").appendText(expectedVisibility.getDescription());
45+
}
46+
47+
@Override public void describeMismatch(Object item, Description description)
48+
{
49+
if (item == null)
50+
{
51+
description.appendText("was null");
52+
}
53+
else if (item instanceof Class)
54+
{
55+
description.appendText("was a ")
56+
.appendText(VisibilityUtils.getVisibility((Class<?>) item).getDescription())
57+
.appendText(" class");
58+
}
59+
else if (item instanceof Member)
60+
{
61+
description.appendText("was a ")
62+
.appendText(VisibilityUtils.getVisibility((Member) item).getDescription())
63+
.appendText(" ")
64+
.appendText(item.getClass().getName());
65+
}
66+
else
67+
{
68+
description.appendText("was " + item.getClass().getName() + " instead of a reflective element like a Class<T>, Constructor<T>, or Method");
69+
}
70+
}
71+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.hamcrest.visibility;
2+
3+
import org.hamcrest.Matcher;
4+
5+
public class VisibilityMatchers
6+
{
7+
public static <T> Matcher<T> isPublic()
8+
{
9+
return new VisibilityMatcher<>(Visibility.PUBLIC);
10+
}
11+
12+
public static <T> Matcher<T> isProtected()
13+
{
14+
return new VisibilityMatcher<>(Visibility.PROTECTED);
15+
}
16+
17+
public static <T> Matcher<T> isPackageProtected()
18+
{
19+
return new VisibilityMatcher<>(Visibility.PACKAGE_PROTECTED);
20+
}
21+
22+
public static <T> Matcher<T> isPrivate()
23+
{
24+
return new VisibilityMatcher<>(Visibility.PRIVATE);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)