Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 2, 2019
2 parents 3729f7a + 7ab5b79 commit be80278
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.hamcrest.core.CombinableMatcher;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.StringContains;
import org.hamcrest.text.IsEmptyString;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -72,6 +74,20 @@ public void detectLengthComparisons(final String file) throws Exception {
).validate();
}

/**
* UseStringIsEmpty not detect when used String[].length, when checking for
* empty string.
* @throws Exception If something goes wrong.
*/
@Test
public void notDetectOnArrayOfStrings() throws Exception {
new PmdAssert(
"ArrayOfStringsLengthGreaterThanZero.java",
new IsEqual<>(true),
new IsEmptyString()
).validate();
}

/**
* Constructs StringContains matcher for error message.
* @param file File name.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package foo;

public final class ArrayOfStringsLengthGreaterThanZero {

private final String[] strings;

public ArrayOfStringsLengthGreaterThanZero(final String... args) {
this.strings = args.clone();
}

public String[] args() {
return this.strings.clone();
}

public boolean arrayFromArgs(final String... args) {
return args.length > 0;
}

public boolean arrayFromField() {
return this.strings.length > 0;
}

public boolean arrayFromMethod() {
return this.args().length > 0;
}

}

0 comments on commit be80278

Please sign in to comment.