我有以下代码,我得到了sonarqube
更改此条件,以便它并不总是评估为false
这条代码if(hashCodeVariable == null && equalsVariable.equals(EQUALS_METHOD)) {
是给我带来声音错误的一行。
@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
方面知识渊博的人,我的代码还可以吗?我应该做什么更改?
hashcodevariable上的null测试是毫无意义的,就像在第一种情况下使用了。
if(hashCodeVariable.equals(HASH_CODE_METHOD) && equalsVariable == null) {
...
if(hashCodeVariable == null && equalsVariable.equals(EQUALS_METHOD)) {
hashcodevariais是!null,与第一个If statement中的其他情况一样,将提出NullPoInterException。因此条件是错误的。
在此时hashCodeVariable
是非编号的,因为否则在以前的if-block中会抛出 NullPointerException
。该条件是false
。&&
是短路的,因此false && whatever
是false
,而无需评估任何内容。