Skip to content

Commit 5dda9f9

Browse files
SONARJAVA-4540 Support Record's Compact Constructors in MethodTreeImpl (#4522)
1 parent 3618545 commit 5dda9f9

6 files changed

Lines changed: 63 additions & 20 deletions

File tree

java-checks/src/test/files/checks/IndentationCheck_default.java renamed to java-checks-test-sources/src/main/files/non-compiling/checks/IndentationCheck_default.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import java.util.stream.IntStream;
22

33
class TestSwitch {
4+
5+
public record QonId(String value) {
6+
public QonId { // Noncompliant
7+
Objects.requireNonNull(value, "QonId value cannot be null");
8+
}
9+
}
10+
11+
public record QonId_(String value) {
12+
public QonId_ { // compliant
13+
Objects.requireNonNull(value, "QonId value cannot be null");
14+
}
15+
}
416
enum Color {
517
RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, BLACK, WHITE,
618
ORANGE, BROWN, LIME, PURPLE, GREY, CRIMSON, NAVY, OLIVE,

java-checks/src/test/files/checks/IndentationCheck_custom.java renamed to java-checks-test-sources/src/main/java/checks/IndentationCheck_custom.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
package checks;
2+
3+
import java.util.List;
4+
import java.util.NoSuchElementException;
15
import java.util.stream.IntStream;
26

3-
class Foo {
7+
class FooIdentation {
48
int a; // Noncompliant {{Make this line start after 4 spaces instead of 2 in order to indent the code consistently. (Indentation level is at 4.)}}
59
int b; // Compliant - already reported
610
int c; // Compliant - already reported
@@ -39,7 +43,7 @@ class Foo { // Noncompliant {{Make this line start after 4 s
3943
}
4044
}
4145

42-
enum Bar {
46+
enum BarIdentation {
4347
A,
4448
B,
4549
C;
@@ -51,34 +55,34 @@ public void foo2() { // Compliant
5155
}
5256
}
5357

54-
interface Qix {
58+
interface QixIdentation {
5559

5660
void foo1(); // Noncompliant
5761

5862
void foo2(); // Compliant
5963

6064
}
6165

62-
class Baz {
66+
class BazIdentation {
6367

6468
void foo() { // Noncompliant {{Make this line start after 4 spaces instead of 2 in order to indent the code consistently. (Indentation level is at 4.)}}
65-
new MyInterface() { // Noncompliant {{Make this line start after 8 spaces instead of 4 in order to indent the code consistently. (Indentation level is at 4.)}}
66-
public void foo() { // Compliant
69+
new QixIdentation() { // Noncompliant {{Make this line start after 8 spaces instead of 4 in order to indent the code consistently. (Indentation level is at 4.)}}
70+
public void foo1() { // Compliant
6771
}
68-
public void bar() { // Noncompliant
72+
public void foo2() { // Noncompliant
6973
}
7074
};
7175
}
7276

73-
int[] foo = new int[] {
77+
Object[] foo = new Object[] {
7478
0,
75-
new Foo()
79+
new FooIdentation()
7680
};
7781

7882
}
7983

80-
class Qiz { // Noncompliant
81-
public void foo() { // Noncompliant
84+
class QizIndentation { // Noncompliant
85+
public void foo(int foo) { // Noncompliant
8286
switch (0) { // Noncompliant
8387
case 0:
8488
System.out.println(); System.out.println(); // Noncompliant
@@ -105,13 +109,12 @@ public void foo() { // Noncompliant
105109
: case 3: break; // Compliant
106110
}
107111
};
112+
static List<Integer> list = List.of(1,2,3);
108113
static {
109114
try{ // Noncompliant {{Make this line start after 8 spaces instead of 4 in order to indent the code consistently. (Indentation level is at 4.)}}
110-
while (keys.hasMoreElements()) { // Noncompliant {{Make this line start after 12 spaces instead of 7 in order to indent the code consistently. (Indentation level is at 4.)}}
111-
s = keys.nextElement(); // Noncompliant {{Make this line start after 16 spaces instead of 8 in order to indent the code consistently. (Indentation level is at 4.)}}
112-
rId = (String) s;
113-
cName = (String) exceptionClassNames.get(rId);
114-
exceptionRepositoryIds.put (cName, rId);
115+
while (list.isEmpty()) { // Noncompliant {{Make this line start after 12 spaces instead of 7 in order to indent the code consistently. (Indentation level is at 4.)}}
116+
int s = list.get(0); // Noncompliant {{Make this line start after 16 spaces instead of 8 in order to indent the code consistently. (Indentation level is at 4.)}}
117+
String k = "hello";
115118
}
116119
} catch (NoSuchElementException e) { }
117120
}
@@ -122,7 +125,7 @@ public static class Inner { // Noncompliant {{Make this line
122125
}
123126
}
124127

125-
class Lambda {
128+
class LambdaIndentation {
126129
void foo() {
127130
IntStream
128131
.range(1, 5)

java-checks/src/test/java/org/sonar/java/checks/IndentationCheckTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
import org.sonar.java.checks.verifier.CheckVerifier;
2424

2525
import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath;
26+
import static org.sonar.java.checks.verifier.TestUtils.nonCompilingTestSourcesPath;
2627

2728
class IndentationCheckTest {
2829

2930
@Test
3031
void detected_default_indentation_level() {
3132
CheckVerifier.newVerifier()
32-
.onFile("src/test/files/checks/IndentationCheck_default.java")
33+
.onFile(nonCompilingTestSourcesPath("checks/IndentationCheck_default.java"))
3334
.withCheck(new IndentationCheck())
3435
.verifyIssues();
3536
}
@@ -39,7 +40,7 @@ void detected_custom_level() {
3940
IndentationCheck check = new IndentationCheck();
4041
check.indentationLevel = 4;
4142
CheckVerifier.newVerifier()
42-
.onFile("src/test/files/checks/IndentationCheck_custom.java")
43+
.onFile(mainCodeSourcesPath("checks/IndentationCheck_custom.java"))
4344
.withCheck(check)
4445
.verifyIssues();
4546
}

java-frontend/src/main/java/org/sonar/java/model/declaration/MethodTreeImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.sonar.java.ast.parser.QualifiedIdentifierListTreeImpl;
3131
import org.sonar.java.ast.parser.TypeParameterListTreeImpl;
3232
import org.sonar.java.cfg.CFG;
33+
import org.sonar.java.model.InternalSyntaxToken;
3334
import org.sonar.java.model.JUtils;
3435
import org.sonar.java.model.JavaTree;
3536
import org.sonar.java.model.ModifiersUtils;
@@ -233,7 +234,16 @@ public void accept(TreeVisitor visitor) {
233234

234235
@Override
235236
public int getLine() {
236-
return parameters.openParenToken().getLine();
237+
InternalSyntaxToken token = parameters.openParenToken();
238+
if (token != null) {
239+
return token.getLine();
240+
} else {
241+
// type cast may fail, it is fine. We will just add a new case if it happens.
242+
// could first try with type cast and fallback parameters
243+
// but cannot reach full coverage
244+
InternalSyntaxToken name = (InternalSyntaxToken) simpleName().identifierToken();
245+
return name.getLine();
246+
}
237247
}
238248

239249
@Nullable

java-frontend/src/test/java/org/sonar/java/model/declaration/ClassTreeImplTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public void visitVariable(VariableTree tree) {
8787
void records_members_order() {
8888
ClassTree classTree = (ClassTree) JParserTestUtils.parse(
8989
"record Output(String title, String summary, String text) {\n"
90+
+ " public Output {}"
9091
+ " public static final String CONST_1 = \"abc\";\n"
9192
+ " boolean isTooLong() { return true; }\n"
9293
+ " public static final int CONST_2 = 42;\n"
@@ -100,6 +101,7 @@ void records_members_order() {
100101
assertThat(classTree).is(Tree.Kind.RECORD);
101102
List<String> membersKinds = classTree.members().stream().map(Tree::kind).map(Tree.Kind::name).collect(Collectors.toList());
102103
assertThat(membersKinds).containsExactly(
104+
"CONSTRUCTOR",
103105
"VARIABLE",
104106
"METHOD",
105107
"VARIABLE",

java-frontend/src/test/java/org/sonar/java/model/declaration/MethodTreeImplTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,21 @@ void has_all_syntax_token() {
219219
assertThat(method.closeParenToken()).isNotNull();
220220
assertThat(method.semicolonToken()).isNotNull();
221221
assertThat(method.throwsToken()).isNull();
222+
223+
method = getUniqueMethod("record Output(String title) { public Output {} }");
224+
assertThat(method.openParenToken()).isNull();
225+
assertThat(method.closeParenToken()).isNull();
226+
assertThat(method.semicolonToken()).isNull();
227+
assertThat(method.throwsToken()).isNull();
228+
}
229+
230+
@Test
231+
void getLine_return_line_of_method_declaration() {
232+
MethodTreeImpl method = getUniqueMethod("class A { public void foo(int arg) throws Exception {} }");
233+
assertThat(method.getLine()).isEqualTo(1);
234+
235+
method = getUniqueMethod("record Output(String title) { public Output {} }");
236+
assertThat(method.getLine()).isEqualTo(1);
222237
}
223238

224239
@Test

0 commit comments

Comments
 (0)