|
19 | 19 | import java.util.ArrayList; |
20 | 20 | import java.util.List; |
21 | 21 | import org.junit.jupiter.api.Test; |
| 22 | +import org.sonar.java.model.expression.LiteralTreeImpl; |
22 | 23 | import org.sonar.plugins.java.api.semantic.MethodMatchers; |
23 | 24 | import org.sonar.plugins.java.api.tree.BaseTreeVisitor; |
24 | 25 | import org.sonar.plugins.java.api.tree.ClassTree; |
25 | 26 | import org.sonar.plugins.java.api.tree.CompilationUnitTree; |
26 | 27 | import org.sonar.plugins.java.api.tree.IdentifierTree; |
| 28 | +import org.sonar.plugins.java.api.tree.LambdaExpressionTree; |
| 29 | +import org.sonar.plugins.java.api.tree.LiteralTree; |
27 | 30 | import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; |
28 | 31 | import org.sonar.plugins.java.api.tree.MethodInvocationTree; |
29 | 32 | import org.sonar.plugins.java.api.tree.MethodTree; |
| 33 | +import org.sonar.plugins.java.api.tree.NewClassTree; |
| 34 | +import org.sonar.plugins.java.api.tree.ParenthesizedTree; |
30 | 35 | import org.sonar.plugins.java.api.tree.Tree; |
| 36 | +import org.sonar.plugins.java.api.tree.VariableTree; |
31 | 37 |
|
32 | 38 | import static org.assertj.core.api.Assertions.assertThat; |
33 | 39 | import static org.junit.jupiter.api.Assertions.assertFalse; |
34 | 40 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 41 | +import static org.sonar.java.checks.helpers.MethodTreeUtils.lamdaArgumentAt; |
| 42 | +import static org.sonar.java.checks.helpers.MethodTreeUtils.parentMethodInvocationOfArgumentAtPos; |
35 | 43 |
|
36 | 44 | class MethodTreeUtilsTest { |
37 | 45 |
|
@@ -139,6 +147,55 @@ public void visitMethodInvocation(MethodInvocationTree tree) { |
139 | 147 | assertThat(MethodTreeUtils.consecutiveMethodInvocation(((MemberSelectExpressionTree) toStringMethod.parent()).identifier())).isEmpty(); |
140 | 148 | } |
141 | 149 |
|
| 150 | + @Test |
| 151 | + void parent_method_invocation_of_argument_at_pos() { |
| 152 | + CompilationUnitTree compilationUnitTree = JParserTestUtils.parse(""" |
| 153 | + class A { |
| 154 | + int field1 = 1; |
| 155 | + int field2 = Math.max((2), 3); |
| 156 | + Thread field3 = new Thread("4"); |
| 157 | + } |
| 158 | + """); |
| 159 | + ClassTree classTree = (ClassTree) compilationUnitTree.types().get(0); |
| 160 | + List<Tree> members = classTree.members(); |
| 161 | + LiteralTree literal1 = (LiteralTree) ((VariableTree) members.get(0)).initializer(); |
| 162 | + MethodInvocationTree mathMax = (MethodInvocationTree) ((VariableTree) members.get(1)).initializer(); |
| 163 | + LiteralTree literal2 = (LiteralTree) ((ParenthesizedTree) mathMax.arguments().get(0)).expression(); |
| 164 | + LiteralTree literal3 = (LiteralTree) mathMax.arguments().get(1); |
| 165 | + NewClassTree newThread = (NewClassTree) ((VariableTree) members.get(2)).initializer(); |
| 166 | + LiteralTree literal4 = (LiteralTree) newThread.arguments().get(0); |
| 167 | + LiteralTreeImpl expressionWithoutParent = new LiteralTreeImpl(Tree.Kind.STRING_LITERAL, null); |
| 168 | + |
| 169 | + assertThat(parentMethodInvocationOfArgumentAtPos(null, 0)).isNull(); |
| 170 | + assertThat(parentMethodInvocationOfArgumentAtPos(expressionWithoutParent, 0)).isNull(); |
| 171 | + assertThat(parentMethodInvocationOfArgumentAtPos(literal1, 0)).isNull(); |
| 172 | + assertThat(parentMethodInvocationOfArgumentAtPos(literal2, 0)).isSameAs(mathMax); |
| 173 | + assertThat(parentMethodInvocationOfArgumentAtPos(literal2, 1)).isNull(); |
| 174 | + assertThat(parentMethodInvocationOfArgumentAtPos(literal2, 2)).isNull(); |
| 175 | + assertThat(parentMethodInvocationOfArgumentAtPos(literal3, 0)).isNull(); |
| 176 | + assertThat(parentMethodInvocationOfArgumentAtPos(literal3, 1)).isSameAs(mathMax); |
| 177 | + assertThat(parentMethodInvocationOfArgumentAtPos(literal4, 0)).isNull(); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + void lamda_argument_ar() { |
| 182 | + CompilationUnitTree compilationUnitTree = JParserTestUtils.parse(""" |
| 183 | + class A { |
| 184 | + Runnable lambda1 = () -> {}; |
| 185 | + java.util.function.Consumer<String> lambda2 = a -> {}; |
| 186 | + } |
| 187 | + """); |
| 188 | + ClassTree classTree = (ClassTree) compilationUnitTree.types().get(0); |
| 189 | + List<Tree> members = classTree.members(); |
| 190 | + LambdaExpressionTree lambda1 = (LambdaExpressionTree) ((VariableTree) members.get(0)).initializer(); |
| 191 | + LambdaExpressionTree lambda2 = (LambdaExpressionTree) ((VariableTree) members.get(1)).initializer(); |
| 192 | + |
| 193 | + assertThat(lamdaArgumentAt(null, 0)).isNull(); |
| 194 | + assertThat(lamdaArgumentAt(lambda1, 0)).isNull(); |
| 195 | + assertThat(lamdaArgumentAt(lambda2, 0)).isSameAs(lambda2.parameters().get(0)); |
| 196 | + assertThat(lamdaArgumentAt(lambda2, 1)).isNull(); |
| 197 | + } |
| 198 | + |
142 | 199 | private MethodTree parseMethod(String code) { |
143 | 200 | CompilationUnitTree compilationUnitTree = JParserTestUtils.parse(code); |
144 | 201 | ClassTree classTree = (ClassTree) compilationUnitTree.types().get(0); |
|
0 commit comments