Skip to content

Commit

Permalink
SONARJAVA-176 Skip annotations parameters (#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohops authored and saberduck committed Mar 21, 2017
1 parent f2d6f2d commit 2377761
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,22 @@ private void checkVariable(VariableTree tree) {
}

private void checkAssignment(AssignmentExpressionTree tree) {
if (isFileNameVariable(getVariableIdentifier(tree))) {
if (isFileNameVariable(getVariableIdentifier(tree)) && !isPartOfAnnotation(tree)) {
checkExpression(tree.expression());
}
}

private static boolean isPartOfAnnotation(AssignmentExpressionTree tree) {
Tree parent = tree.parent();
while (parent != null) {
if (parent.is(Tree.Kind.ANNOTATION)) {
return true;
}
parent = parent.parent();
}
return false;
}

private static boolean isFileNameVariable(@Nullable IdentifierTree variable) {
return variable != null && VARIABLE_NAME_PATTERN.matcher(variable.name()).find();
}
Expand Down
17 changes: 15 additions & 2 deletions java-checks/src/test/files/checks/HardcodedURICheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@
import java.net.URISyntaxException;

class A {

public static @interface MyAnnotation {
String stuff() default "none";
String path() default "/";
}

String fileName = "//my-network-drive/folder/file.txt"; // Noncompliant
String[] stuffs = new String[1];

void foo(String s) throws URISyntaxException {
@MyAnnotation(stuff = "yolo", path = "/{var}/bulu/stuff") // Compliant - annotations are ignored
void bar(String var) { }

@MyAnnotation(stuff = "/{var}/bulu/stuff") // Compliant - not a path assignmnet
void qix(String var) { }

@MyAnnotation(path = "/{var}/bulu/stuff") // Compliant - annotations are ignored
void foo(String s, String var) throws URISyntaxException {
new Object();

new URI(s); // Compliant
Expand All @@ -32,7 +45,7 @@ void foo(String s) throws URISyntaxException {
fileNAME = s + "\\\\" + s; // Noncompliant {{Remove this hard-coded path-delimiter.}}t
fileNAME = s + "hello" + s; // Compliant
fileNAME = "c:\\blah\\blah\\blah.txt"; // Noncompliant

int fIleNaMe = 14 - 2;

String v1 = s + "//" + s; // Compliant - not a file name
Expand Down

0 comments on commit 2377761

Please sign in to comment.