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

com.xenoamess.p3c.pmd.lang.java.rule.other.AvoidDoubleOrFloatEqualCompareRule Maven / Gradle / Ivy

There is a newer version: 2.2.1.0x
Show newest version
package com.xenoamess.p3c.pmd.lang.java.rule.other;

import com.xenoamess.p3c.pmd.lang.java.rule.AbstractAliRule;
import com.xenoamess.p3c.pmd.lang.java.util.ViolationUtils;
import net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;

import java.util.List;

/**
 * @author changle.lq
 * @date 2018/12/12
 */
public class AvoidDoubleOrFloatEqualCompareRule extends AbstractAliRule {

    private static final String FLOAT = "float";
    private static final String DOUBLE = "double";
    private static final int LIST_SIZE = 2;

    @Override
    public Object visit(ASTEqualityExpression node, Object data) {
        List list = node.findDescendantsOfType(ASTPrimaryExpression.class);
        if (list.size() != LIST_SIZE) {
            return super.visit(node, data);
        }

        ASTPrimaryExpression left = list.get(0);
        ASTPrimaryExpression right = list.get(1);
        Class leftType = left.getType();
        Class rightType = right.getType();
        if (leftType == null || rightType == null) {
            return super.visit(node, data);
        }

        if (FLOAT.equals(leftType.getName()) && FLOAT.equals(rightType.getName())) {
            ViolationUtils.addViolationWithPrecisePosition(this, node, data);
        } else if (DOUBLE.equals(leftType.getName()) && DOUBLE.equals(rightType.getName())) {
            ViolationUtils.addViolationWithPrecisePosition(this, node, data);
        }
        return super.visit(node, data);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy