com.xenoamess.p3c.pmd.lang.java.rule.other.AvoidDoubleOrFloatEqualCompareRule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of p3c-pmd Show documentation
Show all versions of p3c-pmd Show documentation
Alibaba Java Coding Guidelines PMD implementations(XenoAmess
TPM)
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);
}
}