Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

package checks.FileHeaderCheck;

public class Class4 {
}
// Compliant

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package checks.FileHeaderCheck;

public class Class5 {
}
// Compliant
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

package checks.FileHeaderCheck;

public class Regex5 {
}
// Compliant
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package checks.FileHeaderCheck;

public class Regex6 {
}
// Compliant
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,21 @@ public List<Tree.Kind> nodesToVisit() {
@Override
public void setContext(JavaFileScannerContext context) {
super.context = context;
if (isRegularExpression) {
if (searchPattern == null) {
try {
searchPattern = Pattern.compile(getHeaderFormat(), Pattern.DOTALL);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("[" + getClass().getSimpleName() + "] Unable to compile the regular expression: " + headerFormat, e);
if (headerFormat.isEmpty()) {
expectedLines = new String[]{};
isRegularExpression = false;
Comment on lines +61 to +63
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isRegularExpression is a @RuleProperty-annotated public field set by the SonarQube framework to represent user configuration. Mutating it inside setContext() means the field no longer reflects the user's intent after the first file is scanned.

In practice this is harmless — the isEmpty() guard is re-evaluated on every call so the mutation is always re-applied for empty headers. But if the framework ever inspects or logs the field value after scanning (for diagnostics or rule-parameter serialization), it will see false even when the user explicitly set true.

A cleaner approach: replace the mutation with a local boolean that shadows the field for the duration of the call:

boolean effectiveRegex = isRegularExpression;
if (headerFormat.isEmpty()) {
  expectedLines = new String[]{};
  effectiveRegex = false;
} else if (isRegularExpression) {
  ...
}

This keeps rule-property state immutable and makes the intent explicit.

Suggested change
if (headerFormat.isEmpty()) {
expectedLines = new String[]{};
isRegularExpression = false;
if (headerFormat.isEmpty()) {
expectedLines = new String[]{};
} else {
if (isRegularExpression) {
if (searchPattern == null) {
try {
searchPattern = Pattern.compile(getHeaderFormat(), Pattern.DOTALL);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("[" + getClass().getSimpleName() + "] Unable to compile the regular expression: " + headerFormat, e);
}
}
} else {
expectedLines = headerFormat.split("(?:\r)?\n|\r");
}
}
  • Mark as noise

} else {
if (isRegularExpression) {
if (searchPattern == null) {
try {
searchPattern = Pattern.compile(getHeaderFormat(), Pattern.DOTALL);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("[" + getClass().getSimpleName() + "] Unable to compile the regular expression: " + headerFormat, e);
}
}
} else {
expectedLines = headerFormat.split("(?:\r)?\n|\r");
}
} else {
expectedLines = headerFormat.split("(?:\r)?\n|\r");
}
visitFile();
}
Expand Down Expand Up @@ -114,7 +119,6 @@ private static boolean matches(String[] expectedLines, List<String> lines) {
} else {
result = false;
}

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ void test() {
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Class3.java"))
.withCheck(check)
.verifyNoIssues();

check = new FileHeaderCheck();
CheckVerifier.newVerifier()
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Class4.java"))
.withCheck(check)
.verifyNoIssues();

check = new FileHeaderCheck();
CheckVerifier.newVerifier()
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Class5.java"))
.withCheck(check)
.verifyNoIssues();
}

@Test
Expand Down Expand Up @@ -139,6 +151,20 @@ void regex() {
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Regex4.java"))
.withCheck(check)
.verifyIssues();

check = new FileHeaderCheck();
check.isRegularExpression = true;
CheckVerifier.newVerifier()
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Regex5.java"))
.withCheck(check)
.verifyNoIssues();

check = new FileHeaderCheck();
check.isRegularExpression = true;
CheckVerifier.newVerifier()
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Regex6.java"))
.withCheck(check)
.verifyNoIssues();
}

@Test
Expand Down
Loading