All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.p3c.pmd.lang.java.rule.set.EqualsHashCodeRule Maven / Gradle / Ivy

package com.alibaba.p3c.pmd.lang.java.rule.set;

/*
 * Copyright 1999-2017 Alibaba Group.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.alibaba.p3c.pmd.lang.java.rule.AbstractAliRule;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import org.jaxen.JaxenException;

import java.util.List;

/**
 * [Mendatory] Equals method must be with hashCode() method.
 *
 * @author leonard99559
 * @date 2019/10/16
 */
public class EqualsHashCodeRule extends AbstractAliRule {

    @Override
    public Object visit(ASTCompilationUnit rootNode, Object data) {

        try {
            List nodeList = rootNode.findChildNodesWithXPath(
                    "//ClassOrInterfaceBodyDeclaration[./Annotation[@AnnotationName = 'Override']]" +
                            "//MethodDeclaration[@MethodName = 'hashCode' or @MethodName = 'equals']");
            if (nodeList.size() == 1) {
                addViolationWithMessage(data, nodeList.get(0),
                        "java.set.EqualsHashCodeRule.rule.msg");
            }
        } catch (JaxenException e) {
            e.printStackTrace();
        }
        return super.visit(rootNode, data);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy