Skip to content

Commit efd3065

Browse files
committed
SONARJAVA-1437 Update subset of rules to highlight precise location
1 parent f8170fc commit efd3065

73 files changed

Lines changed: 188 additions & 133 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
'commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java':[
3-
327,
4-
861,
3+
326,
4+
860,
55
],
66
}

java-checks/src/main/java/org/sonar/java/checks/AvoidDESCheck.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,18 @@ protected List<MethodMatcher> getMethodInvocationMatchers() {
5555
protected void onMethodInvocationFound(MethodInvocationTree mit) {
5656
ExpressionTree firstArg = mit.arguments().get(0);
5757
ExpressionTree defaultPropertyValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(firstArg);
58-
if (defaultPropertyValue != null) {
59-
firstArg = defaultPropertyValue;
58+
if (defaultPropertyValue == null) {
59+
defaultPropertyValue = firstArg;
6060
}
61-
if (firstArg != null && firstArg.is(Tree.Kind.STRING_LITERAL)) {
62-
String algo = LiteralUtils.trimQuotes(((LiteralTree) firstArg).value());
63-
checkIssue(mit, algo);
61+
if (defaultPropertyValue != null && defaultPropertyValue.is(Tree.Kind.STRING_LITERAL)) {
62+
checkIssue(firstArg, (LiteralTree) defaultPropertyValue);
6463
}
6564
}
6665

67-
private void checkIssue(Tree tree, String algorithm) {
68-
String[] transformationElements = algorithm.split("/");
66+
private void checkIssue(ExpressionTree argumentForReport, LiteralTree argument) {
67+
String[] transformationElements = LiteralUtils.trimQuotes(argument.value()).split("/");
6968
if (transformationElements.length > 0 && isExcludedAlgorithm(transformationElements[0])) {
70-
addIssue(tree, "Use the recommended AES (Advanced Encryption Standard) instead.");
69+
reportIssue(argumentForReport, "Use the recommended AES (Advanced Encryption Standard) instead.");
7170
}
7271
}
7372

java-checks/src/main/java/org/sonar/java/checks/CloneMethodCallsSuperCloneCheck.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void visitNode(Tree tree) {
6666
@Override
6767
public void leaveNode(Tree tree) {
6868
if (isCloneMethod(tree) && !foundSuperClone) {
69-
addIssue(tree, "Use super.clone() to create and seed the cloned instance to be returned.");
69+
reportIssue(((MethodTree) tree).simpleName(), "Use super.clone() to create and seed the cloned instance to be returned.");
7070
}
7171
}
7272

@@ -86,14 +86,14 @@ private static boolean isSuperCloneCall(Tree tree) {
8686
MethodInvocationTree mit = (MethodInvocationTree) tree;
8787

8888
return mit.arguments().isEmpty() &&
89-
mit.methodSelect().is(Kind.MEMBER_SELECT) &&
90-
isSuperClone((MemberSelectExpressionTree) mit.methodSelect());
89+
mit.methodSelect().is(Kind.MEMBER_SELECT) &&
90+
isSuperClone((MemberSelectExpressionTree) mit.methodSelect());
9191
}
9292

9393
private static boolean isSuperClone(MemberSelectExpressionTree tree) {
9494
return "clone".equals(tree.identifier().name()) &&
95-
tree.expression().is(Kind.IDENTIFIER) &&
96-
"super".equals(((IdentifierTree) tree.expression()).name());
95+
tree.expression().is(Kind.IDENTIFIER) &&
96+
"super".equals(((IdentifierTree) tree.expression()).name());
9797
}
9898

9999
}

java-checks/src/main/java/org/sonar/java/checks/CompareToReturnValueCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private class ReturnStatementVisitor extends BaseTreeVisitor {
8787
@Override
8888
public void visitReturnStatement(ReturnStatementTree tree) {
8989
if (returnsIntegerMinValue(tree.expression())) {
90-
addIssue(tree, "Simply return -1");
90+
reportIssue(tree.expression(), "Simply return -1");
9191
}
9292
}
9393

java-checks/src/main/java/org/sonar/java/checks/DefaultInitializedFieldCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void checkVariable(VariableTree member) {
7171
if (initializer != null) {
7272
initializer = ExpressionsHelper.skipParentheses(initializer);
7373
if (isDefault(initializer, member.type().symbolType().isPrimitive())) {
74-
addIssue(member, "Remove this initialization to \"" + ((LiteralTree) initializer).value() + "\", the compiler will do that for you.");
74+
reportIssue(initializer, "Remove this initialization to \"" + ((LiteralTree) initializer).value() + "\", the compiler will do that for you.");
7575
}
7676
}
7777
}

java-checks/src/main/java/org/sonar/java/checks/FinalizeFieldsSetCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void visitClass(ClassTree tree) {
8484
@Override
8585
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
8686
if (isFieldAssignment(tree) && isNullAssignment(tree)) {
87-
addIssue(tree, "Remove this nullification of \"" + getFieldName(tree) + "\".");
87+
reportIssue(tree.expression(), "Remove this nullification of \"" + getFieldName(tree) + "\".");
8888
}
8989
}
9090

java-checks/src/main/java/org/sonar/java/checks/GarbageCollectorCalledCheck.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
@SqaleConstantRemediation("30min")
4545
public class GarbageCollectorCalledCheck extends SubscriptionBaseVisitor {
4646

47-
4847
@Override
4948
public List<Tree.Kind> nodesToVisit() {
5049
return ImmutableList.of(Tree.Kind.METHOD_INVOCATION);
@@ -56,7 +55,7 @@ public void visitNode(Tree tree) {
5655
if (mit.arguments().isEmpty() && mit.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
5756
MemberSelectExpressionTree mset = (MemberSelectExpressionTree) mit.methodSelect();
5857
if (isGarbageCollectorCall(mset)) {
59-
addIssue(tree, "Don't try to be smarter than the JVM, remove this call to run the garbage collector.");
58+
reportIssue(mset.identifier(), "Don't try to be smarter than the JVM, remove this call to run the garbage collector.");
6059
}
6160
}
6261
}
@@ -68,12 +67,12 @@ private static boolean isGarbageCollectorCall(MemberSelectExpressionTree mset) {
6867
return "System".equals(((IdentifierTree) mset.expression()).name());
6968
} else if (mset.expression().is(Tree.Kind.METHOD_INVOCATION)) {
7069
MethodInvocationTree mit = (MethodInvocationTree) mset.expression();
71-
if(mit.arguments().isEmpty() && mit.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
70+
if (mit.arguments().isEmpty() && mit.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
7271
MemberSelectExpressionTree subMset = (MemberSelectExpressionTree) mit.methodSelect();
7372
//detect call to Runtime.getRuntime().gc()
7473
return "getRuntime".equals(subMset.identifier().name())
75-
&& subMset.expression().is(Tree.Kind.IDENTIFIER)
76-
&& "Runtime".equals(((IdentifierTree) subMset.expression()).name());
74+
&& subMset.expression().is(Tree.Kind.IDENTIFIER)
75+
&& "Runtime".equals(((IdentifierTree) subMset.expression()).name());
7776
}
7877
}
7978
}

java-checks/src/main/java/org/sonar/java/checks/IgnoredReturnValueCheck.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.sonar.plugins.java.api.semantic.Symbol;
2929
import org.sonar.plugins.java.api.semantic.Type;
3030
import org.sonar.plugins.java.api.tree.ExpressionStatementTree;
31+
import org.sonar.plugins.java.api.tree.IdentifierTree;
3132
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
3233
import org.sonar.plugins.java.api.tree.Tree;
3334
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
@@ -68,7 +69,8 @@ public void visitNode(Tree tree) {
6869
MethodInvocationTree mit = (MethodInvocationTree) est.expression();
6970
Type methodType = mit.symbolType();
7071
if (!returnsVoid(methodType) && isCheckedType(mit)) {
71-
addIssue(tree, "The return value of \"" + methodName(mit) + "\" must be used.");
72+
IdentifierTree methodName = MethodsHelper.methodName(mit);
73+
reportIssue(methodName, "The return value of \"" + methodName.name() + "\" must be used.");
7274
}
7375
}
7476
}
@@ -87,7 +89,4 @@ private static boolean returnsVoid(Type methodType) {
8789
return methodType.isVoid() || methodType.isUnknown();
8890
}
8991

90-
private static String methodName(MethodInvocationTree mit) {
91-
return MethodsHelper.methodName(mit).name();
92-
}
9392
}

java-checks/src/main/java/org/sonar/java/checks/IndexOfStartPositionCheck.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.sonar.check.Priority;
2525
import org.sonar.check.Rule;
2626
import org.sonar.java.checks.helpers.ExpressionsHelper;
27+
import org.sonar.java.checks.helpers.MethodsHelper;
2728
import org.sonar.java.checks.methods.MethodMatcher;
2829
import org.sonar.java.model.LiteralUtils;
2930
import org.sonar.java.tag.Tag;
@@ -85,7 +86,7 @@ private void checkIndexOfInvocation(MethodInvocationTree mit, ExpressionTree oth
8586
}
8687
Long otherValue = LiteralUtils.longLiteralValue(other);
8788
if (otherValue != null && otherValue != -1 && otherValue != 0) {
88-
addIssue(mit, "Use \".indexOf(" + replaceMessage + ",n) > -1\" instead.");
89+
reportIssue(MethodsHelper.methodName(mit), "Use \".indexOf(" + replaceMessage + ",n) > -1\" instead.");
8990
}
9091
}
9192
}

java-checks/src/main/java/org/sonar/java/checks/InstanceOfAlwaysTrueCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void visitNode(Tree tree) {
5353
Type expressionType = instanceOfTree.expression().symbolType();
5454
Type instanceOf = instanceOfTree.type().symbolType();
5555
if (expressionType.isSubtypeOf(instanceOf)) {
56-
addIssue(tree, "Remove this useless \"instanceof\" operator; it will always return \"true\". ");
56+
reportIssue(instanceOfTree.instanceofKeyword(), "Remove this useless \"instanceof\" operator; it will always return \"true\". ");
5757
}
5858
}
5959
}

0 commit comments

Comments
 (0)