Skip to content

Commit e13d168

Browse files
SONARJAVA-4702 Update API with methods from JUtils (#4569)
1 parent 2d4042b commit e13d168

52 files changed

Lines changed: 546 additions & 246 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.

java-checks-aws/src/main/java/org/sonar/java/checks/aws/AwsBuilderMethodFinder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Collections;
2323
import java.util.List;
2424
import java.util.Optional;
25-
import org.sonar.java.model.JUtils;
2625
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
2726
import org.sonar.plugins.java.api.semantic.MethodMatchers;
2827
import org.sonar.plugins.java.api.semantic.Symbol;
@@ -67,7 +66,7 @@ public void visitNode(Tree tree) {
6766
// If the call to build is made on a builder variable, we look into initialization and usages for a call to the method
6867
getIdentifier(invocation).ifPresentOrElse(identifier -> {
6968
Symbol symbol = identifier.symbol();
70-
if (!JUtils.isLocalVariable(symbol) || JUtils.isParameter(symbol)) {
69+
if (!symbol.isLocalVariable() || symbol.isParameter()) {
7170
return;
7271
}
7372
VariableTree declaration = (VariableTree) symbol.declaration();

java-checks-aws/src/main/java/org/sonar/java/checks/aws/AwsConsumerBuilderUsageCheck.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.sonar.java.checks.helpers.ExpressionsHelper;
2727
import org.sonar.java.checks.methods.AbstractMethodDetection;
2828
import org.sonar.java.model.ExpressionUtils;
29-
import org.sonar.java.model.JUtils;
3029
import org.sonar.java.model.Symbols;
3130
import org.sonar.plugins.java.api.semantic.MethodMatchers;
3231
import org.sonar.plugins.java.api.semantic.Symbol;
@@ -118,7 +117,7 @@ private static boolean isVariableContainingABuilderResult(ExpressionTree express
118117
return false;
119118
}
120119
Symbol variable = ((IdentifierTree) expression).symbol();
121-
return JUtils.isLocalVariable(variable) &&
120+
return variable.isLocalVariable() &&
122121
ExpressionsHelper.initializedAndAssignedExpressionStream(variable)
123122
.anyMatch(AwsConsumerBuilderUsageCheck::isBuilder);
124123
}

java-checks-aws/src/main/java/org/sonar/java/checks/aws/AwsLambdaSyncCallCheck.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.Optional;
2626
import java.util.Set;
2727
import java.util.stream.Collectors;
28-
2928
import org.sonar.check.Rule;
3029
import org.sonar.java.checks.helpers.ExpressionsHelper;
3130
import org.sonar.java.model.ExpressionUtils;
@@ -39,9 +38,6 @@
3938
import org.sonar.plugins.java.api.tree.Tree;
4039
import org.sonar.plugins.java.api.tree.VariableTree;
4140

42-
import static org.sonar.java.model.JUtils.isLocalVariable;
43-
import static org.sonar.java.model.JUtils.isParameter;
44-
4541
@Rule(key = "S6246")
4642
public class AwsLambdaSyncCallCheck extends AbstractAwsMethodVisitor {
4743

@@ -89,10 +85,10 @@ private static Optional<String> getSyncCalls(MethodInvocationTree tree) {
8985

9086
// We know there is at least one usage, i.e. the one we just got above.
9187
List<IdentifierTree> localUsages = invokeRequest.symbol().usages().stream()
92-
.filter(u -> isLocalVariable(u.symbol()) && !u.equals(invokeRequest))
88+
.filter(u -> u.symbol().isLocalVariable() && !u.equals(invokeRequest))
9389
.collect(Collectors.toList());
9490

95-
if (isParameter(invokeRequest.symbol())
91+
if (invokeRequest.symbol().isParameter()
9692
|| localUsages.stream().anyMatch(lu -> isArgumentToACall(lu) || statementSetsAsyncCall(lu))
9793
|| declarationSetsAsyncCall(invokeRequest)) {
9894
return Optional.empty();
@@ -113,7 +109,7 @@ private static boolean isArgumentToACall(IdentifierTree invokeRequest) {
113109

114110
private static boolean hasLocalVarDeclaration(IdentifierTree invokeRequest) {
115111
Tree declaration = invokeRequest.symbol().declaration();
116-
return (declaration != null && declaration.is(Tree.Kind.VARIABLE) && isLocalVariable(((VariableTree) declaration).symbol()));
112+
return (declaration != null && declaration.is(Tree.Kind.VARIABLE) && ((VariableTree) declaration).symbol().isLocalVariable());
117113
}
118114

119115
/**

java-checks-common/src/main/java/org/sonar/java/checks/helpers/ExpressionsHelper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import javax.annotation.CheckForNull;
3333
import javax.annotation.Nullable;
3434
import org.sonar.java.model.ExpressionUtils;
35-
import org.sonar.java.model.JUtils;
3635
import org.sonar.plugins.java.api.JavaFileScannerContext;
3736
import org.sonar.plugins.java.api.semantic.Symbol;
3837
import org.sonar.plugins.java.api.semantic.Type;
@@ -255,7 +254,7 @@ public static Optional<Symbol> getInvokedSymbol(MethodInvocationTree mit) {
255254
}
256255

257256
public static boolean isNotReassigned(Symbol symbol) {
258-
return symbol.isFinal() || (symbol.isVariableSymbol() && JUtils.isEffectivelyFinal(((Symbol.VariableSymbol) symbol)));
257+
return symbol.isFinal() || (symbol.isVariableSymbol() && ((Symbol.VariableSymbol) symbol).isEffectivelyFinal());
259258
}
260259

261260
public static List<ExpressionTree> getIdentifierAssignments(IdentifierTree identifier) {

java-checks-common/src/main/java/org/sonar/java/checks/helpers/HardcodedStringExpressionChecker.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.List;
2424
import java.util.Set;
2525
import org.sonar.java.model.ExpressionUtils;
26-
import org.sonar.java.model.JUtils;
2726
import org.sonar.java.model.LiteralUtils;
2827
import org.sonar.plugins.java.api.JavaFileScannerContext;
2928
import org.sonar.plugins.java.api.semantic.MethodMatchers;
@@ -158,12 +157,12 @@ private static boolean isDerivedFromPlainText(IdentifierTree identifier, List<Ja
158157
Set<Symbol> visited) {
159158
Symbol symbol = identifier.symbol();
160159
boolean firstVisit = visited.add(symbol);
161-
if (!firstVisit || !symbol.isVariableSymbol() || JUtils.isParameter(symbol) || isNonFinalField(symbol)) {
160+
if (!firstVisit || !symbol.isVariableSymbol() || symbol.isParameter() || isNonFinalField(symbol)) {
162161
return false;
163162
}
164163
VariableTree variable = (VariableTree) symbol.declaration();
165164
if (variable == null) {
166-
return JUtils.constantValue((Symbol.VariableSymbol) symbol).isPresent();
165+
return ((Symbol.VariableSymbol) symbol).constantValue().isPresent();
167166
}
168167

169168
List<ExpressionTree> assignments = getIdentifierAssignments(identifier);

java-checks-common/src/test/java/org/sonar/java/checks/helpers/ExpressionsHelperTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
import java.lang.reflect.Constructor;
2323
import javax.annotation.Nullable;
24-
2524
import org.junit.jupiter.api.Test;
25+
import org.sonar.plugins.java.api.semantic.Symbol;
2626
import org.sonar.plugins.java.api.tree.IdentifierTree;
2727
import org.sonar.plugins.java.api.tree.MethodTree;
2828

2929
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.Mockito.mock;
31+
import static org.mockito.Mockito.when;
3032

3133
class ExpressionsHelperTest extends JParserTestUtils {
3234

@@ -127,6 +129,23 @@ void variableSwapSOE() {
127129
assertValueResolution(code, null);
128130
}
129131

132+
@Test
133+
void isNotReassignedTest(){
134+
Symbol.VariableSymbol symbol = mock(Symbol.VariableSymbol.class);
135+
136+
when(symbol.isFinal()).thenReturn(true);
137+
assertThat(ExpressionsHelper.isNotReassigned(symbol)).isTrue();
138+
139+
when(symbol.isFinal()).thenReturn(false);
140+
assertThat(ExpressionsHelper.isNotReassigned(symbol)).isFalse();
141+
142+
when(symbol.isVariableSymbol()).thenReturn(true);
143+
assertThat(ExpressionsHelper.isNotReassigned(symbol)).isFalse();
144+
145+
when(symbol.isEffectivelyFinal()).thenReturn(true);
146+
assertThat(ExpressionsHelper.isNotReassigned(symbol)).isTrue();
147+
}
148+
130149
private <T> void assertValueResolution(String code, @Nullable T target) {
131150
MethodTree method = methodTree(code);
132151
IdentifierTree a = variableFromLastReturnStatement(method.block().body());
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2023 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.java.checks.helpers;
21+
22+
import java.util.ArrayList;
23+
import java.util.HashSet;
24+
import java.util.Optional;
25+
import org.junit.jupiter.api.Test;
26+
import org.sonar.plugins.java.api.semantic.Symbol;
27+
import org.sonar.plugins.java.api.tree.IdentifierTree;
28+
import org.sonar.plugins.java.api.tree.Tree;
29+
import org.sonar.plugins.java.api.tree.VariableTree;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.Mockito.mock;
33+
import static org.mockito.Mockito.when;
34+
import static org.sonar.java.checks.helpers.HardcodedStringExpressionChecker.isExpressionDerivedFromPlainText;
35+
36+
class HardcodedStringExpressionCheckerTest {
37+
38+
@Test
39+
void isExpressionDerivedFromPlainTextTest() {
40+
IdentifierTree expression = mock(IdentifierTree.class);
41+
when(expression.kind()).thenReturn(Tree.Kind.IDENTIFIER);
42+
Symbol.VariableSymbol symbol = mockSymbol(true, false, true);
43+
Symbol owner = mockOwner(true);
44+
when(symbol.owner()).thenReturn(owner);
45+
VariableTree declaration = mock(VariableTree.class);
46+
when(symbol.declaration()).thenReturn(declaration);
47+
when(expression.symbol()).thenReturn(symbol);
48+
49+
assertThat(isExpressionDerivedFromPlainText(expression, new ArrayList<>(), new HashSet<>())).isFalse();
50+
51+
when(symbol.declaration()).thenReturn(null);
52+
when(symbol.constantValue()).thenReturn(Optional.of("SOME VALUE"));
53+
assertThat(isExpressionDerivedFromPlainText(expression, new ArrayList<>(), new HashSet<>())).isTrue();
54+
55+
when(symbol.isVariableSymbol()).thenReturn(false);
56+
assertThat(isExpressionDerivedFromPlainText(expression, new ArrayList<>(), new HashSet<>())).isFalse();
57+
58+
when(symbol.isVariableSymbol()).thenReturn(true);
59+
when(symbol.isParameter()).thenReturn(true);
60+
assertThat(isExpressionDerivedFromPlainText(expression, new ArrayList<>(), new HashSet<>())).isFalse();
61+
62+
}
63+
64+
private Symbol mockOwner(boolean isTypeSymbol){
65+
Symbol owner = mock(Symbol.class);
66+
when(owner.isTypeSymbol()).thenReturn(isTypeSymbol);
67+
return owner;
68+
}
69+
70+
private Symbol.VariableSymbol mockSymbol(boolean isVariableSymbol, boolean isParameter, boolean isFinal){
71+
Symbol.VariableSymbol symbol = mock(Symbol.VariableSymbol.class);
72+
when(symbol.isVariableSymbol()).thenReturn(isVariableSymbol);
73+
when(symbol.isParameter()).thenReturn(isParameter);
74+
when(symbol.isFinal()).thenReturn(isFinal);
75+
return symbol;
76+
}
77+
78+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
import static org.sonar.java.checks.helpers.DeprecatedCheckerHelper.deprecatedAnnotation;
3535
import static org.sonar.java.checks.helpers.DeprecatedCheckerHelper.hasJavadocDeprecatedTag;
36-
import static org.sonar.java.model.JUtils.isLocalVariable;
3736

3837
public abstract class AbstractMissingDeprecatedChecker extends IssuableSubscriptionVisitor {
3938

@@ -48,7 +47,7 @@ public List<Kind> nodesToVisit() {
4847

4948
@Override
5049
public void visitNode(Tree tree) {
51-
boolean isLocalVar = tree.is(Tree.Kind.VARIABLE) && isLocalVariable(((VariableTree) tree).symbol());
50+
boolean isLocalVar = tree.is(Tree.Kind.VARIABLE) && ((VariableTree) tree).symbol().isLocalVariable();
5251
AnnotationTree deprecatedAnnotation = deprecatedAnnotation(tree);
5352
boolean hasDeprecatedAnnotation = deprecatedAnnotation != null;
5453
boolean hasJavadocDeprecatedTag = hasJavadocDeprecatedTag(tree);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.sonar.java.checks;
2121

2222
import org.sonar.java.checks.helpers.ExpressionsHelper;
23-
import org.sonar.java.model.JUtils;
2423
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
2524
import org.sonar.plugins.java.api.semantic.Symbol;
2625
import org.sonar.plugins.java.api.semantic.Type;
@@ -62,7 +61,7 @@ private void visitClassTree(ClassTree classTree) {
6261
}
6362

6463
private static boolean isInnerClass(Symbol.TypeSymbol typeSymbol) {
65-
return !typeSymbol.equals(JUtils.outermostClass(typeSymbol));
64+
return !typeSymbol.equals(typeSymbol.outermostClass());
6665
}
6766

6867
protected boolean isSerializable(Type type) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424
import java.util.Set;
2525
import java.util.stream.Collectors;
2626
import org.sonar.check.Rule;
27-
import org.sonar.plugins.java.api.JavaVersionAwareVisitor;
28-
import org.sonar.java.model.JUtils;
2927
import org.sonar.plugins.java.api.JavaFileScanner;
3028
import org.sonar.plugins.java.api.JavaFileScannerContext;
3129
import org.sonar.plugins.java.api.JavaVersion;
30+
import org.sonar.plugins.java.api.JavaVersionAwareVisitor;
3231
import org.sonar.plugins.java.api.semantic.Symbol;
3332
import org.sonar.plugins.java.api.semantic.Symbol.MethodSymbol;
3433
import org.sonar.plugins.java.api.semantic.Type;
@@ -89,7 +88,7 @@ private static boolean isSAM(ClassTree classBody) {
8988
// should be anonymous class of interface and not abstract class
9089
return symbol.interfaces().size() == 1
9190
&& symbol.superClass().is(JAVA_LANG_OBJECT)
92-
&& hasSingleAbstractMethodInHierarchy(JUtils.superTypes(symbol));
91+
&& hasSingleAbstractMethodInHierarchy(symbol.superTypes());
9392
}
9493
return false;
9594
}

0 commit comments

Comments
 (0)