Skip to content

Commit

Permalink
SONARJAVA-4540 Support Record's Compact Constructors in MethodTreeIm…
Browse files Browse the repository at this point in the history
…pl (#4522)
  • Loading branch information
erwan-serandour authored Nov 6, 2023
1 parent 3618545 commit 5dda9f9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import java.util.stream.IntStream;

class TestSwitch {

public record QonId(String value) {
public QonId { // Noncompliant
Objects.requireNonNull(value, "QonId value cannot be null");
}
}

public record QonId_(String value) {
public QonId_ { // compliant
Objects.requireNonNull(value, "QonId value cannot be null");
}
}
enum Color {
RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, BLACK, WHITE,
ORANGE, BROWN, LIME, PURPLE, GREY, CRIMSON, NAVY, OLIVE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package checks;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.IntStream;

class Foo {
class FooIdentation {
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.)}}
int b; // Compliant - already reported
int c; // Compliant - already reported
Expand Down Expand Up @@ -39,7 +43,7 @@ class Foo { // Noncompliant {{Make this line start after 4 s
}
}

enum Bar {
enum BarIdentation {
A,
B,
C;
Expand All @@ -51,34 +55,34 @@ public void foo2() { // Compliant
}
}

interface Qix {
interface QixIdentation {

void foo1(); // Noncompliant

void foo2(); // Compliant

}

class Baz {
class BazIdentation {

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.)}}
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.)}}
public void foo() { // Compliant
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.)}}
public void foo1() { // Compliant
}
public void bar() { // Noncompliant
public void foo2() { // Noncompliant
}
};
}

int[] foo = new int[] {
Object[] foo = new Object[] {
0,
new Foo()
new FooIdentation()
};

}

class Qiz { // Noncompliant
public void foo() { // Noncompliant
class QizIndentation { // Noncompliant
public void foo(int foo) { // Noncompliant
switch (0) { // Noncompliant
case 0:
System.out.println(); System.out.println(); // Noncompliant
Expand All @@ -105,13 +109,12 @@ public void foo() { // Noncompliant
: case 3: break; // Compliant
}
};
static List<Integer> list = List.of(1,2,3);
static {
try{ // Noncompliant {{Make this line start after 8 spaces instead of 4 in order to indent the code consistently. (Indentation level is at 4.)}}
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.)}}
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.)}}
rId = (String) s;
cName = (String) exceptionClassNames.get(rId);
exceptionRepositoryIds.put (cName, rId);
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.)}}
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.)}}
String k = "hello";
}
} catch (NoSuchElementException e) { }
}
Expand All @@ -122,7 +125,7 @@ public static class Inner { // Noncompliant {{Make this line
}
}

class Lambda {
class LambdaIndentation {
void foo() {
IntStream
.range(1, 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
import org.sonar.java.checks.verifier.CheckVerifier;

import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath;
import static org.sonar.java.checks.verifier.TestUtils.nonCompilingTestSourcesPath;

class IndentationCheckTest {

@Test
void detected_default_indentation_level() {
CheckVerifier.newVerifier()
.onFile("src/test/files/checks/IndentationCheck_default.java")
.onFile(nonCompilingTestSourcesPath("checks/IndentationCheck_default.java"))
.withCheck(new IndentationCheck())
.verifyIssues();
}
Expand All @@ -39,7 +40,7 @@ void detected_custom_level() {
IndentationCheck check = new IndentationCheck();
check.indentationLevel = 4;
CheckVerifier.newVerifier()
.onFile("src/test/files/checks/IndentationCheck_custom.java")
.onFile(mainCodeSourcesPath("checks/IndentationCheck_custom.java"))
.withCheck(check)
.verifyIssues();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.sonar.java.ast.parser.QualifiedIdentifierListTreeImpl;
import org.sonar.java.ast.parser.TypeParameterListTreeImpl;
import org.sonar.java.cfg.CFG;
import org.sonar.java.model.InternalSyntaxToken;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.JavaTree;
import org.sonar.java.model.ModifiersUtils;
Expand Down Expand Up @@ -233,7 +234,16 @@ public void accept(TreeVisitor visitor) {

@Override
public int getLine() {
return parameters.openParenToken().getLine();
InternalSyntaxToken token = parameters.openParenToken();
if (token != null) {
return token.getLine();
} else {
// type cast may fail, it is fine. We will just add a new case if it happens.
// could first try with type cast and fallback parameters
// but cannot reach full coverage
InternalSyntaxToken name = (InternalSyntaxToken) simpleName().identifierToken();
return name.getLine();
}
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void visitVariable(VariableTree tree) {
void records_members_order() {
ClassTree classTree = (ClassTree) JParserTestUtils.parse(
"record Output(String title, String summary, String text) {\n"
+ " public Output {}"
+ " public static final String CONST_1 = \"abc\";\n"
+ " boolean isTooLong() { return true; }\n"
+ " public static final int CONST_2 = 42;\n"
Expand All @@ -100,6 +101,7 @@ void records_members_order() {
assertThat(classTree).is(Tree.Kind.RECORD);
List<String> membersKinds = classTree.members().stream().map(Tree::kind).map(Tree.Kind::name).collect(Collectors.toList());
assertThat(membersKinds).containsExactly(
"CONSTRUCTOR",
"VARIABLE",
"METHOD",
"VARIABLE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ void has_all_syntax_token() {
assertThat(method.closeParenToken()).isNotNull();
assertThat(method.semicolonToken()).isNotNull();
assertThat(method.throwsToken()).isNull();

method = getUniqueMethod("record Output(String title) { public Output {} }");
assertThat(method.openParenToken()).isNull();
assertThat(method.closeParenToken()).isNull();
assertThat(method.semicolonToken()).isNull();
assertThat(method.throwsToken()).isNull();
}

@Test
void getLine_return_line_of_method_declaration() {
MethodTreeImpl method = getUniqueMethod("class A { public void foo(int arg) throws Exception {} }");
assertThat(method.getLine()).isEqualTo(1);

method = getUniqueMethod("record Output(String title) { public Output {} }");
assertThat(method.getLine()).isEqualTo(1);
}

@Test
Expand Down

0 comments on commit 5dda9f9

Please sign in to comment.