Skip to content

Commit

Permalink
#1031.Now StringIsEmptyRule not detect String[].length
Browse files Browse the repository at this point in the history
  • Loading branch information
zCRUSADERz committed May 1, 2019
1 parent 9197c99 commit 7ab5b79
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
Expand Up @@ -36,6 +36,7 @@
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
import net.sourceforge.pmd.lang.java.ast.ASTResultType;
import net.sourceforge.pmd.lang.java.ast.ASTType;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
Expand Down Expand Up @@ -78,35 +79,47 @@ public boolean isTargetMethod(final JavaNameOccurrence occ) {
}

@Override
public Object visit(final ASTVariableDeclaratorId node, final Object data) {
final Node type = node.getTypeNameNode();
if (type != null
|| this.appliesToClassName(node.getNameDeclaration().getTypeImage())
) {
final List<NameOccurrence> declarations = node.getUsages();
this.checkDeclarations(declarations, data);
public Object visit(
final ASTVariableDeclaratorId variable, final Object data
) {
final Node node = variable.getTypeNameNode();
if (node instanceof ASTReferenceType) {
final Class<?> clazz = variable.getType();
final String type = variable.getNameDeclaration().getTypeImage();
if (clazz != null && !clazz.isArray()
&& this.appliesToClassName(type)
) {
final List<NameOccurrence> declarations = variable.getUsages();
this.checkDeclarations(declarations, data);
}
}
node.childrenAccept(this, data);
variable.childrenAccept(this, data);
return data;
}

@Override
public Object visit(final ASTMethodDeclaration node, final Object data) {
final ASTResultType result = node.getResultType();
public Object visit(
final ASTMethodDeclaration declaration, final Object data
) {
final ASTResultType result = declaration.getResultType();
if (!result.isVoid()) {
final ASTType type = (ASTType) result.jjtGetChild(0);
if (this.appliesToClassName(type.getTypeImage())) {
final Scope scope = node.getScope().getParent();
final ASTType node = (ASTType) result.jjtGetChild(0);
final Class<?> clazz = node.getType();
final String type = node.getTypeImage();
if (clazz != null && !clazz.isArray()
&& this.appliesToClassName(type)
) {
final Scope scope = declaration.getScope().getParent();
final MethodNameDeclaration method = new MethodNameDeclaration(
node.getMethodDeclarator()
declaration.getMethodDeclarator()
);
final List<NameOccurrence> declarations = scope
.getDeclarations(MethodNameDeclaration.class)
.get(method);
this.checkDeclarations(declarations, data);
}
}
node.childrenAccept(this, data);
declaration.childrenAccept(this, data);
return data;
}

Expand Down
Expand Up @@ -34,7 +34,6 @@
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.StringContains;
import org.hamcrest.text.IsEmptyString;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -80,7 +79,6 @@ public void detectLengthComparisons(final String file) throws Exception {
* empty string.
* @throws Exception If something goes wrong.
*/
@Disabled
@Test
public void notDetectOnArrayOfStrings() throws Exception {
new PmdAssert(
Expand Down

0 comments on commit 7ab5b79

Please sign in to comment.