Skip to content

Commit

Permalink
Exclude static fields from records's methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Jun 7, 2021
1 parent e501c48 commit 16f76a3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
3 changes: 3 additions & 0 deletions example/src/main/java/com/example/RecordExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

@Desugar
public record RecordExample(int i, String s, long l, float f, double d, String[] arr) {

public static RecordExample DUMMY = new RecordExample(0, null, 0, 0, 0, null);

public RecordExample {
if (i > 1_000_000) {
throw new IllegalArgumentException("'i' is too big");
Expand Down
5 changes: 2 additions & 3 deletions example/src/test/java/com/example/RecordExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ public void testNotEquals() {

@Test
public void testHashCodeWithDefaults() {
RecordExample r = new RecordExample(0, null, 0, 0f, 5d, null);
assertEquals(
intellijStyleHashCode(r),
Objects.hashCode(r)
intellijStyleHashCode(RecordExample.DUMMY),
Objects.hashCode(RecordExample.DUMMY)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.*;

import javax.lang.model.element.Modifier;
import javax.tools.JavaFileObject;
import java.util.Iterator;
import java.util.stream.Stream;
Expand Down Expand Up @@ -176,6 +177,13 @@ public Void visitClass(ClassTree node, Void aVoid) {
public void finished(TaskEvent e) {
}

private Stream<JCTree.JCVariableDecl> getRecordComponents(JCTree.JCClassDecl classDecl) {
return classDecl.getMembers().stream()
.filter(JCTree.JCVariableDecl.class::isInstance)
.map(JCTree.JCVariableDecl.class::cast)
.filter(it -> !it.getModifiers().getFlags().contains(Modifier.STATIC));
}

private List<JCTree.JCStatement> generateToString(JCTree.JCClassDecl classDecl) {
JCTree.JCExpression stringBuilder = make.NewClass(
null,
Expand All @@ -185,11 +193,8 @@ private List<JCTree.JCStatement> generateToString(JCTree.JCClassDecl classDecl)
null
);

Stream<JCTree.JCVariableDecl> fields = classDecl.getMembers().stream()
.filter(JCTree.JCVariableDecl.class::isInstance)
.map(JCTree.JCVariableDecl.class::cast);
for (
Iterator<JCTree.JCVariableDecl> iterator = fields.iterator();
Iterator<JCTree.JCVariableDecl> iterator = getRecordComponents(classDecl).iterator();
iterator.hasNext();
) {
JCTree.JCVariableDecl fieldDecl = iterator.next();
Expand Down Expand Up @@ -274,11 +279,12 @@ private List<JCTree.JCStatement> generateEquals(JCTree.JCClassDecl classDecl, Na

// fields
{
for (JCTree member : classDecl.getMembers()) {
if (!(member instanceof JCTree.JCVariableDecl)) {
continue;
}
JCTree.JCVariableDecl fieldDecl = (JCTree.JCVariableDecl) member;
for (
Iterator<JCTree.JCVariableDecl> iterator = getRecordComponents(classDecl).iterator();
iterator.hasNext();
) {
JCTree.JCVariableDecl fieldDecl = iterator.next();

JCTree.JCExpression myFieldAccess = make.Select(make.This(Type.noType), fieldDecl.name);
JCTree.JCExpression otherFieldAccess = make.Select(
make.TypeCast(make.Ident(classDecl.name), make.Ident(otherName)),
Expand Down Expand Up @@ -313,11 +319,11 @@ private List<JCTree.JCStatement> generateEquals(JCTree.JCClassDecl classDecl, Na
private List<JCTree.JCStatement> generateHashCode(JCTree.JCClassDecl classDecl) {
ListBuffer<JCTree.JCExpression> expressions = new ListBuffer<>();

for (JCTree member : classDecl.getMembers()) {
if (!(member instanceof JCTree.JCVariableDecl)) {
continue;
}
JCTree.JCVariableDecl fieldDecl = (JCTree.JCVariableDecl) member;
for (
Iterator<JCTree.JCVariableDecl> iterator = getRecordComponents(classDecl).iterator();
iterator.hasNext();
) {
JCTree.JCVariableDecl fieldDecl = iterator.next();

JCTree fType = fieldDecl.getType();

Expand Down

0 comments on commit 16f76a3

Please sign in to comment.