Skip to content

Commit

Permalink
fix: records can have static initializers (#4962)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell authored Oct 17, 2022
1 parent 4d3e52f commit 3f8c1b4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public <C extends CtType<Object>> C addTypeMemberAt(int position, CtTypeMember m
JLSViolation.throwIfSyntaxErrorsAreNotIgnored(this, String.format("%s method is native or abstract, both is not allowed",
member.getSimpleName()));
}
if (member instanceof CtAnonymousExecutable) {
JLSViolation.throwIfSyntaxErrorsAreNotIgnored(this, "Anonymous executable is not allowed in a record");
if (member instanceof CtAnonymousExecutable && !member.isStatic()) {
JLSViolation.throwIfSyntaxErrorsAreNotIgnored(this, "Instance initializer is not allowed in a record (JLS 17 $8.10.2)");
}
return super.addTypeMemberAt(position, member);
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/spoon/test/record/CtRecordTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package spoon.test.record;

import static java.lang.System.lineSeparator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.constraints.NotNull;

import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.declaration.CtAnonymousExecutable;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
Expand Down Expand Up @@ -202,6 +207,15 @@ void testGenericTypeParametersArePrintedBeforeTheFunctionParameters() {
assertEquals("public record GenericRecord<T>(T a, T b) {}", record.toString());
}

@Test
void testBuildRecordModelWithStaticInitializer() {
// contract: a record can have static initializers
String code = "src/test/resources/records/WithStaticInitializer.java";
CtModel model = assertDoesNotThrow(() -> createModelFromPath(code));
List<CtAnonymousExecutable> execs = model.getElements(new TypeFilter<>(CtAnonymousExecutable.class));
assertThat(execs.size(), equalTo(2));
}

private <T> T head(Collection<T> collection) {
return collection.iterator().next();
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/resources/records/WithStaticInitializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package records;

public record WithStaticInitializer(int i, String s) {
static {
System.out.println("Hello World");
}
static {
if (Math.random() < 0.5) {
System.out.println("A");
} else {
System.out.println("B");
}
}
}

0 comments on commit 3f8c1b4

Please sign in to comment.