Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hasSerialVersionUIDField() #11

Open
sdavids opened this issue Sep 3, 2017 · 0 comments
Open

Add hasSerialVersionUIDField() #11

sdavids opened this issue Sep 3, 2017 · 0 comments

Comments

@sdavids
Copy link

sdavids commented Sep 3, 2017

A lot of beans are also Serializable.

Please consider a matcher for checking whether an explicit serialVersionUID has been set.

    @Factory
    public static Matcher<Class<?>> hasSerialVersionUIDField() {
        return new HasSerialVersionUIDFieldMatcher();
    }
public final class HasSerialVersionUIDFieldMatcher extends TypeSafeDiagnosingMatcher<Class<?>> {

    @Override
    protected boolean matchesSafely(Class<?> item, Description mismatchDescription) {
        boolean hasExplicitSerialVersionUIDField = hasExplicitSerialVersionUIDField(item);
        if (!hasExplicitSerialVersionUIDField) {
            mismatchDescription
                    .appendText("bean of type ")
                    .appendValue(item.getName())
                    .appendText(" does not have a serialVersionUID field");
        }
        return hasExplicitSerialVersionUIDField;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("bean class has a serialVersionUID field");
    }

    private boolean hasExplicitSerialVersionUIDField(Class<?> item) {
        try {
            item.getDeclaredField("serialVersionUID");
        } catch (NoSuchFieldException ignored) {
            return false;
        }
        return true;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants