1 19 20 package org.netbeans.modules.j2ee.jpa.verification.rules.entity; 21 22 import java.util.Collections ; 23 import javax.lang.model.element.ExecutableElement; 24 import javax.lang.model.element.TypeElement; 25 import javax.lang.model.util.ElementFilter; 26 import org.netbeans.modules.j2ee.jpa.verification.JPAClassRule; 27 import org.netbeans.modules.j2ee.jpa.verification.JPAClassRule.ClassConstraints; 28 import org.netbeans.modules.j2ee.jpa.verification.common.ProblemContext; 29 import org.netbeans.spi.editor.hints.ErrorDescription; 30 import org.openide.util.NbBundle; 31 32 36 public class IdClassOverridesEqualsAndHashCode extends JPAClassRule { 37 38 public IdClassOverridesEqualsAndHashCode() { 39 setClassContraints(Collections.singleton(ClassConstraints.IDCLASS)); 40 } 41 42 @Override public ErrorDescription[] apply(TypeElement subject, ProblemContext ctx){ 43 boolean hasEquals = false; 44 boolean hasHashCode = false; 45 46 for (ExecutableElement method : ElementFilter.methodsIn(subject.getEnclosedElements())){ 47 String methodName = method.getSimpleName().toString(); 48 49 if ("equals".equals(methodName) && method.getParameters().size() == 1){ 51 52 if ("java.lang.Object".equals(method.getParameters().get(0).asType().toString())){ hasEquals = true; 54 } 55 } 56 else{ 57 if ("hashCode".equals(methodName) && method.getParameters().size() == 0){ hasHashCode = true; 59 } 60 } 61 62 if (hasHashCode && hasEquals){ 63 return null; 64 } 65 } 66 67 return new ErrorDescription[]{createProblem(subject, ctx)}; 68 } 69 70 @Override public String getDescription(){ 71 return NbBundle.getMessage(IdDefinedInHierarchy.class, "MSG_IdClassDoesNotOverrideEquals"); 72 } 73 } 74 | Popular Tags |