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

fix: Add thread-safety to TypeAdaptor method adaptation #5621

Merged
merged 1 commit into from
Jan 22, 2024
Merged
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
26 changes: 18 additions & 8 deletions src/main/java/spoon/support/adaption/TypeAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

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 @@ -249,12 +248,27 @@ private static boolean supertypeReachableInInheritanceTree(CtType<?> base, Strin
* @return the input method but with the return type, parameter types and thrown types adapted to
* the context of this type adapter
*/
@SuppressWarnings("unchecked")
public CtMethod<?> adaptMethod(CtMethod<?> inputMethod) {
return adaptMethod(inputMethod, true);
}

@SuppressWarnings("unchecked")
private CtMethod<?> adaptMethod(CtMethod<?> inputMethod, boolean cloneBody) {
if (useLegacyTypeAdaption(inputMethod)) {
return legacyAdaptMethod(inputMethod);
}
CtMethod<?> clonedMethod = inputMethod.clone();
CtMethod<?> clonedMethod;
if (cloneBody) {
clonedMethod = inputMethod.clone();
} else {
clonedMethod = inputMethod.getFactory().createMethod().setSimpleName(inputMethod.getSimpleName());
for (CtParameter<?> parameter : inputMethod.getParameters()) {
clonedMethod.addParameter(parameter.clone());
}
for (CtTypeParameter parameter : inputMethod.getFormalCtTypeParameters()) {
clonedMethod.addFormalCtTypeParameter(parameter.clone());
}
}

for (int i = 0; i < clonedMethod.getFormalCtTypeParameters().size(); i++) {
CtTypeParameter clonedParameter = clonedMethod.getFormalCtTypeParameters().get(i);
Expand Down Expand Up @@ -450,13 +464,9 @@ public boolean isOverriding(CtMethod<?> subMethod, CtMethod<?> superMethod) {
if (!isSubtype(subDeclaringType, superDeclaringType.getReference())) {
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);
.adaptMethod(superMethod, false);

for (int i = 0; i < subMethod.getParameters().size(); i++) {
CtParameter<?> subParam = subMethod.getParameters().get(i);
Expand Down