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
Expand Up @@ -30,6 +30,7 @@
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import javax.lang.model.element.Modifier;

/**
* Warns that users should not have a {@link java.util.regex.Pattern} as a key to a Set or Map.
Expand Down Expand Up @@ -65,7 +66,8 @@ public Description matchClass(ClassTree classTree, VisitorState state) {
private static boolean hasArrayField(ClassTree classTree, VisitorState state) {
for (Tree member : classTree.getMembers()) {
if (member instanceof VariableTree variableTree) {
if (IS_ARRAY_VARIABLE.matches(variableTree, state)) {
if (IS_ARRAY_VARIABLE.matches(variableTree, state)
&& !variableTree.getModifiers().getFlags().contains(Modifier.STATIC)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,35 @@ public void testArrayInMethod() {
"}")
.doTest();
}

@Test
public void testStaticArrayFieldInRecord() {
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.*;",
"import java.util.regex.Pattern;",
"class Test {",
" private record MyRecord(String name) {",
" public static byte[] STATIC_PAYLOAD = new byte[0];",
" }",
"}")
.doTest();
}

@Test
public void testStaticAndNonStaticArrayFieldInRecord() {
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.*;",
"import java.util.regex.Pattern;",
"class Test {",
" // BUG: Diagnostic contains: Record type has an array field and",
" private record MyRecord(String name, byte[] payload) {",
" public static byte[] STATIC_PAYLOAD = new byte[0];",
" }",
"}")
.doTest();
}
}