Skip to content

Commit

Permalink
perf: Don't clone method body when adapting it for isOverriding (#4862)
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Al-Istannen authored Sep 11, 2022
1 parent f3ae9b5 commit e466b82
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/spoon/support/adaption/TypeAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import spoon.SpoonException;
import spoon.processing.FactoryAccessor;
import spoon.reflect.code.CtBlock;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtExecutable;
Expand Down Expand Up @@ -403,8 +404,12 @@ public boolean isOverriding(CtMethod<?> subMethod, CtMethod<?> superMethod) {
return false;
}

// We don't need to clone the body here, so leave it out
CtBlock<?> superBody = superMethod.getBody();
superMethod.setBody(null);
CtMethod<?> adapted = new TypeAdaptor(subMethod.getDeclaringType())
.adaptMethod(superMethod);
superMethod.setBody(superBody);

for (int i = 0; i < subMethod.getParameters().size(); i++) {
CtParameter<?> subParam = subMethod.getParameters().get(i);
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import spoon.reflect.reference.CtReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.adaption.TypeAdaptor;
import spoon.support.modelobs.ChangeCollector;
import spoon.support.modelobs.SourceFragmentCreator;
import spoon.support.sniper.SniperJavaPrettyPrinter;
Expand Down Expand Up @@ -837,6 +838,25 @@ void testSniperAddsSpaceAfterFinal() {
testSniper("sniperPrinter.SpaceAfterFinal", modifyField, assertContainsSpaceAfterFinal);
}

@Test
void typeAdaptionBodyResetDoesNotBreakSniper() {
// contract: Resetting the body in the type adaption does not impact the sniper printer.
testSniper(
"sniperPrinter.Overriding",
type -> {
CtType<?> top = type.getNestedType("Super");
CtType<?> bottom = type.getNestedType("Sub");

CtMethod<?> topFoo = top.getMethodsByName("foo").get(0);
CtMethod<?> bottomFoo = bottom.getMethodsByName("foo").get(0);

assertTrue(new TypeAdaptor(bottom).isOverriding(bottomFoo, topFoo));
},
// Did not reformat body
(type, result) -> assertThat(result, containsString("System. out. println(1+2\n"))
);
}

@Nested
class ResourcePrintingInTryWithResourceStatement{
private CtTryWithResource getTryWithResource(CtType<?> type) {
Expand Down
16 changes: 16 additions & 0 deletions src/test/resources/sniperPrinter/Overriding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package sniperPrinter;

public class Overriding {
public static class Super {
void foo() {
System. out. println(1+2
);
}
}

public static class Sub extends Super {
@Override
void foo() {
}
}
}

0 comments on commit e466b82

Please sign in to comment.