Skip to content

Commit

Permalink
Fixed CheckStyle issues, and matched existing code style of codebase.
Browse files Browse the repository at this point in the history
  • Loading branch information
brownian-motion committed May 1, 2020
1 parent ee538c0 commit 6cfa597
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 139 deletions.
11 changes: 7 additions & 4 deletions hamcrest/src/main/java/org/hamcrest/reflection/Visibility.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
import java.lang.reflect.Modifier;
import java.util.Objects;

public enum Visibility
{
/**
* Represents the 4 states of visibility.
*
* @author JJ Brown
*/
enum Visibility {
PUBLIC("public"),
PROTECTED("protected"),
PACKAGE_PROTECTED("package-protected (no modifiers)"),
PRIVATE("private");

public String getDescription()
{
public String getDescription() {
return description;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,51 @@
* This class is intentionally not exposed to the public API, to help keep implementation details hidden (and easy to change).
* Please use {@link VisibilityMatchers} to instantiate instances of this class.
*
* @param <T>
* the type of the element being matched; could be anything
* @param <T> the type of the element being matched; could be anything
* @author JJ Brown
* @see VisibilityMatchers
*/
class VisibilityMatcher<T> extends BaseMatcher<T>
{
private final Visibility expectedVisibility;
class VisibilityMatcher<T> extends BaseMatcher<T> {
private final Visibility expectedVisibility;

VisibilityMatcher(Visibility expectedVisibility)
{
this.expectedVisibility = expectedVisibility;
}
VisibilityMatcher(Visibility expectedVisibility) {
this.expectedVisibility = expectedVisibility;
}

@Override
public boolean matches(Object actual)
{
if (actual == null)
{
return false;
}
if (actual instanceof Class)
{
return expectedVisibility == Visibility.of((Class<?>) actual);
}
if (actual instanceof Member)
{
return expectedVisibility == Visibility.of((Member) actual);
}
return false;
@Override
public boolean matches(Object actual) {
if (actual == null) {
return false;
}

@Override public void describeTo(Description description)
{
description.appendText("is ").appendText(expectedVisibility.getDescription());
if (actual instanceof Class) {
return expectedVisibility == Visibility.of((Class<?>) actual);
}
if (actual instanceof Member) {
return expectedVisibility == Visibility.of((Member) actual);
}
return false;
}

@Override
public void describeTo(Description description) {
description.appendText("is ").appendText(expectedVisibility.getDescription());
}

@Override public void describeMismatch(Object item, Description description)
{
if (item == null)
{
description.appendText("was null");
}
else if (item instanceof Class)
{
description.appendText("was a ")
.appendText(Visibility.of((Class<?>) item).getDescription())
.appendText(" class");
}
else if (item instanceof Member)
{
description.appendText("was a ")
.appendText(Visibility.of((Member) item).getDescription())
.appendText(" ")
.appendText(item.getClass().getName());
}
else
{
description.appendText("was " + item.getClass().getName() + " instead of a reflective element like a Class<T>, Constructor<T>, or Method");
}
@Override
public void describeMismatch(Object item, Description description) {
if (item == null) {
description.appendText("was null");
} else if (item instanceof Class) {
description.appendText("was a ")
.appendText(Visibility.of((Class<?>) item).getDescription())
.appendText(" class");
} else if (item instanceof Member) {
description.appendText("was a ")
.appendText(Visibility.of((Member) item).getDescription())
.appendText(" ")
.appendText(item.getClass().getName());
} else {
description.appendText("was " + item.getClass().getName() + " instead of a reflective element like a Class<T>, Constructor<T>, or Method");
}
}
}
159 changes: 76 additions & 83 deletions hamcrest/src/main/java/org/hamcrest/reflection/VisibilityMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,85 @@
/**
* Defines matchers that check the visibility of reflective objects like {@link java.lang.Class} or {@link java.lang.reflect.Method}.
* {@code null} values never match, nor do normal objects; these simply do not match, without raising an Exception.
*
* @author JJ Brown
*/
public class VisibilityMatchers
{
// Each matcher is stateless and can match any type, so the individual instances are only made once and stored here for re-use.
private static final VisibilityMatcher<?> PUBLIC = new VisibilityMatcher<>(Visibility.PUBLIC);
private static final VisibilityMatcher<?> PROTECTED = new VisibilityMatcher<>(Visibility.PROTECTED);
private static final VisibilityMatcher<?> PACKAGE_PROTECTED = new VisibilityMatcher<>(Visibility.PACKAGE_PROTECTED);
private static final VisibilityMatcher<?> PRIVATE = new VisibilityMatcher<>(Visibility.PRIVATE);
public class VisibilityMatchers {
// Each matcher is stateless and can match any type, so the individual instances are only made once and stored here for re-use.
private static final VisibilityMatcher<?> PUBLIC = new VisibilityMatcher<>(Visibility.PUBLIC);
private static final VisibilityMatcher<?> PROTECTED = new VisibilityMatcher<>(Visibility.PROTECTED);
private static final VisibilityMatcher<?> PACKAGE_PROTECTED = new VisibilityMatcher<>(Visibility.PACKAGE_PROTECTED);
private static final VisibilityMatcher<?> PRIVATE = new VisibilityMatcher<>(Visibility.PRIVATE);

/**
* Matchers reflective elements that have public visibility.
* Specifically, this matcher only matches elements marked with the keyword {@code public}.
* <br>
* This method matches {@link Class} objects or other {@link java.lang.reflect.Member reflective objects}
* like {@link java.lang.reflect.Field} or {@link java.lang.reflect.Method} used in reflection.
* Any other kind of object, or {@code null} values, do not match (but will not cause an Exception).
*
* @param <T>
* the type of the object being matched
* @return a matcher that matches reflective elements with exactly the given level of visibility
*/
@SuppressWarnings("unchecked")
public static <T> Matcher<T> isPublic()
{
// Each matcher is stateless and can match any type (the generic <T> is for type safety at the use site),
// so it's fine to cast the non-reifiable generic type here at runtime and re-use the same instance.
return (Matcher<T>) PUBLIC;
}
/**
* Matchers reflective elements that have public visibility.
* Specifically, this matcher only matches elements marked with the keyword {@code public}.
* <br>
* This method matches {@link Class} objects or other {@link java.lang.reflect.Member reflective objects}
* like {@link java.lang.reflect.Field} or {@link java.lang.reflect.Method} used in reflection.
* Any other kind of object, or {@code null} values, do not match (but will not cause an Exception).
*
* @param <T> the type of the object being matched
* @return a matcher that matches reflective elements with exactly the given level of visibility
*/
@SuppressWarnings("unchecked")
public static <T> Matcher<T> isPublic() {
// Each matcher is stateless and can match any type (the generic <T> is for type safety at the use site),
// so it's fine to cast the non-reifiable generic type here at runtime and re-use the same instance.
return (Matcher<T>) PUBLIC;
}

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

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

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

0 comments on commit 6cfa597

Please sign in to comment.