Skip to content

Commit

Permalink
Issue checkstyle#11807: fixes exception on records and fields in Requ…
Browse files Browse the repository at this point in the history
…ireThis
  • Loading branch information
rnveach committed Dec 2, 2022
1 parent 488ed2d commit b41af5c
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
Expand Up @@ -1562,10 +1562,18 @@ public boolean hasStaticMethod(final DetailAST ident) {
public boolean hasFinalField(final DetailAST instanceMember) {
boolean result = false;
for (DetailAST member : instanceMembers) {
final DetailAST mods = member.getParent().findFirstToken(TokenTypes.MODIFIERS);
final boolean finalMod = mods.findFirstToken(TokenTypes.FINAL) != null;
if (finalMod && isAstSimilar(member, instanceMember)) {
final DetailAST parent = member.getParent();
if (parent.getType() == TokenTypes.RECORD_COMPONENT_DEF) {
result = true;
}
else {
final DetailAST mods = parent.findFirstToken(TokenTypes.MODIFIERS);
final boolean finalMod = mods.findFirstToken(TokenTypes.FINAL) != null;
if (finalMod && isAstSimilar(member, instanceMember)) {
result = true;
}
}
if (result) {
break;
}
}
Expand Down
Expand Up @@ -471,6 +471,26 @@ public void testRecordsDefault() throws Exception {
expected);
}

@Test
public void testRecordsWithCheckFields() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getNonCompilablePath("InputRequireThisRecordsWithCheckFields.java"),
expected);
}

@Test
public void testRecordsWithCheckFieldsOverlap() throws Exception {
final String[] expected = {
"20:20: " + getCheckMessage(MSG_VARIABLE, "a", ""),
"39:20: " + getCheckMessage(MSG_VARIABLE, "a", ""),
"46:16: " + getCheckMessage(MSG_VARIABLE, "a", ""),
};
verifyWithInlineConfigParser(
getNonCompilablePath("InputRequireThisRecordsWithCheckFieldsOverlap.java"),
expected);
}

@Test
public void testLocalClassesInsideLambdas() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
Expand Down
@@ -0,0 +1,48 @@
/*
RequireThis
checkFields = true
checkMethods = (default)true
validateOnlyOverlapping = (default)true
*/

//non-compiled with javac: Compilable with Java14
package com.puppycrawl.tools.checkstyle.checks.coding.requirethis;

public record InputRequireThisRecordsWithCheckFields(String a) {
void method() {
String a = "BUG";
a = a.substring(0, 1); // ok, field 'a' is final and cannot be changed
}

void method2() {
String x = a; // ok, not overlapping
String y = this.a; // ok, uses 'this'
String a = this.a; // ok, local variable assigned from field
a += a; // ok, local variable
}
}
class InputRequireThisRecordsWithCheckFieldsClass {
public final String a;

public InputRequireThisRecordsWithCheckFieldsClass(String a) {
this.a = a;
}

void method() {
String a = "BUG";
a = a.substring(0, 1); // ok, field 'a' is final and cannot be changed
}

void method2() {
String x = a; // ok, not overlapping
String y = this.a; // ok, uses 'this'
String a = this.a; // ok, local variable assigned from field
a += a; // ok, local variable
}

public String a() {
return a; // ok
}
}
@@ -0,0 +1,48 @@
/*
RequireThis
checkFields = true
checkMethods = (default)true
validateOnlyOverlapping = false
*/

//non-compiled with javac: Compilable with Java14
package com.puppycrawl.tools.checkstyle.checks.coding.requirethis;

public record InputRequireThisRecordsWithCheckFieldsOverlap(String a) {
void method() {
String a = "BUG";
a = a.substring(0, 1); // ok, field 'a' is final and cannot be changed
}

void method2() {
String x = a; // violation 'Reference to instance variable 'a' needs "this.".'
String y = this.a; // ok, uses 'this'
String a = this.a; // ok, local variable assigned from field
a += a; // ok, local variable
}
}
class InputRequireThisRecordsWithCheckFieldsClass {
public final String a;

public InputRequireThisRecordsWithCheckFieldsClass(String a) {
this.a = a;
}

void method() {
String a = "BUG";
a = a.substring(0, 1); // ok, field 'a' is final and cannot be changed
}

void method2() {
String x = a; // violation 'Reference to instance variable 'a' needs "this.".'
String y = this.a; // ok, uses 'this'
String a = this.a; // ok, local variable assigned from field
a += a; // ok, local variable
}

public String a() {
return a; // violation 'Reference to instance variable 'a' needs "this.".'
}
}

0 comments on commit b41af5c

Please sign in to comment.