Skip to content

Commit

Permalink
Allow param reassignment in compact constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
lunagl committed Apr 8, 2024
1 parent 36ee212 commit 6e21a35
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import spoon.processing.AbstractProcessor;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtParameter;

import java.util.Map;
Expand All @@ -19,7 +20,10 @@ protected void check(StaticAnalysis staticAnalysis, DynamicAnalysis dynamicAnaly
staticAnalysis.processWith(new AbstractProcessor<CtParameter<?>>() {
@Override
public void process(CtParameter<?> ctParameter) {
if (ctParameter.isImplicit() || !ctParameter.getPosition().isValidPosition()) {
if (ctParameter.isImplicit()
|| !ctParameter.getPosition().isValidPosition()
// reassignment in compact constructor is used to modify the future record field value
|| (ctParameter.getParent() instanceof CtConstructor<?> ctor && ctor.isCompactConstructor())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ class Test<T> {
problems.assertExhausted();
}

@Test
void testRecordConstructor() throws IOException, LinterException {
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString(
JavaVersion.JAVA_17,
"Test",
"""
record Test(int a) {
Test {
a = 1; //# ok
}
Test(int a, int b) {
this(a);
b = 3; //# not ok
}
}
"""
), PROBLEM_TYPES);

assertEqualsReassigned(problems.next(), "b");
problems.assertExhausted();
}

@Test
void testLambda() throws IOException, LinterException {
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString(
Expand Down

0 comments on commit 6e21a35

Please sign in to comment.