diff options
Diffstat (limited to 'common/src/main/java/org/onap/so/openpojo/rules/HasEqualsAndHashCodeRule.java')
-rw-r--r-- | common/src/main/java/org/onap/so/openpojo/rules/HasEqualsAndHashCodeRule.java | 84 |
1 files changed, 41 insertions, 43 deletions
diff --git a/common/src/main/java/org/onap/so/openpojo/rules/HasEqualsAndHashCodeRule.java b/common/src/main/java/org/onap/so/openpojo/rules/HasEqualsAndHashCodeRule.java index 0c7d6add73..351c5b0977 100644 --- a/common/src/main/java/org/onap/so/openpojo/rules/HasEqualsAndHashCodeRule.java +++ b/common/src/main/java/org/onap/so/openpojo/rules/HasEqualsAndHashCodeRule.java @@ -22,12 +22,9 @@ package org.onap.so.openpojo.rules; import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.anything; - import java.lang.reflect.Method; import java.lang.reflect.Parameter; - import org.hamcrest.Matcher; - import com.openpojo.reflection.PojoClass; import com.openpojo.validation.affirm.Affirm; import com.openpojo.validation.rule.Rule; @@ -37,45 +34,46 @@ import com.openpojo.validation.rule.Rule; */ public class HasEqualsAndHashCodeRule implements Rule { - private final Matcher m; - public HasEqualsAndHashCodeRule() { - m = anything(); - } - - public HasEqualsAndHashCodeRule(Matcher m) { - this.m = m; - } - @Override - public void evaluate(PojoClass pojoClass) { - Class<?> clazz = pojoClass.getClazz(); - if (anyOf(m).matches(clazz)) { - boolean hasEquals = false; - boolean hasHashCode = false; - final String name = clazz.getSimpleName(); - final Method[] methods; - if (clazz.getSuperclass().equals(Object.class)) { - methods = clazz.getDeclaredMethods(); - } else { - methods = clazz.getMethods(); - } - for (Method method : methods) { - Parameter[] parameters = method.getParameters(); - if ("equals".equals(method.getName()) && boolean.class.equals(method.getReturnType()) && parameters.length == 1 && Object.class.equals(parameters[0].getType())) { - hasEquals = true; - } else if ("hashCode".equals(method.getName()) && int.class.equals(method.getReturnType())) { - hasHashCode = true; - } - } - - if (!hasEquals) { - Affirm.fail(String.format( - "[%s] does not override equals", name)); - } - if (!hasHashCode) { - Affirm.fail(String.format( - "[%s] does not override hashCode", name)); - } - } - } + private final Matcher m; + + public HasEqualsAndHashCodeRule() { + m = anything(); + } + + public HasEqualsAndHashCodeRule(Matcher m) { + this.m = m; + } + + @Override + public void evaluate(PojoClass pojoClass) { + Class<?> clazz = pojoClass.getClazz(); + if (anyOf(m).matches(clazz)) { + boolean hasEquals = false; + boolean hasHashCode = false; + final String name = clazz.getSimpleName(); + final Method[] methods; + if (clazz.getSuperclass().equals(Object.class)) { + methods = clazz.getDeclaredMethods(); + } else { + methods = clazz.getMethods(); + } + for (Method method : methods) { + Parameter[] parameters = method.getParameters(); + if ("equals".equals(method.getName()) && boolean.class.equals(method.getReturnType()) + && parameters.length == 1 && Object.class.equals(parameters[0].getType())) { + hasEquals = true; + } else if ("hashCode".equals(method.getName()) && int.class.equals(method.getReturnType())) { + hasHashCode = true; + } + } + + if (!hasEquals) { + Affirm.fail(String.format("[%s] does not override equals", name)); + } + if (!hasHashCode) { + Affirm.fail(String.format("[%s] does not override hashCode", name)); + } + } + } } |