【发布时间】:2018-08-01 00:14:23
【问题描述】:
我有以下代码,我得到一个 SonarQube
更改此条件,使其不会总是评估为假
这行代码 if(hashCodeVariable == null && equalsVariable.equals(EQUALS_METHOD)) { 是给我 sonarqube 错误的代码。
@Override
public void visitNode(Tree tree) {
MethodTree mt = null;
ClassTree classTree = (ClassTree) tree;
if (!hasSemantic()) {
return;
}
for (Tree memberTree : classTree.members()) {
if (memberTree.kind().name().equals(Kind.METHOD.name())) {
mt = (MethodTree) memberTree;
methods.add(mt);
}
}
storeMethodValue(mt);
}
public void storeMethodValue(MethodTree mt) {
String methodName;
try {
for (MethodTree method : methods) {
methodName = PUBLIC + SPACE + method.returnType().symbolType().name()
+ SPACE + method.symbol().name();
checkMethodName(methodName);
}
if(hashCodeVariable.equals(HASH_CODE_METHOD) && equalsVariable == null) {
reportIssue(mt, "in");
return;
}
if(hashCodeVariable == null && equalsVariable.equals(EQUALS_METHOD)) {
reportIssue(mt, "out");
return;
}
}
catch (NullPointerException nullPointer) {
throw nullPointer;
}
}
public void checkMethodName(String methodName) {
if (methodName.equals(EQUALS_METHOD)) {
equalsVariable = methodName;
} else if (methodName.equals(HASH_CODE_METHOD)) {
hashCodeVariable = methodName;
}
}
对于那些熟悉使用Tree 的人来说,我的代码还可以吗?我应该做些什么改变?
【问题讨论】: