Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Don't clone method body when adapting it for isOverriding #4862

Merged
merged 1 commit into from
Sep 11, 2022
Merged
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
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() {
}
}
}