From 1eb34e2bfab3fb549c2b46e87023b8525f36b701 Mon Sep 17 00:00:00 2001 From: Martin Wittlinger Date: Wed, 17 Jan 2024 15:13:12 +0100 Subject: [PATCH] fix: Add thread-safety to TypeAdaptor method adaptation Synchronization logic has been added to the method adaptation process in the TypeAdaptor class to prevent multi-threading issues. This change was necessary because without synchronization, simultaneous modifications by multiple threads could lead to inconsistent or unexpected results. The relevant issue can be found at https://github.com/INRIA/spoon/issues/5619. --- .../java/spoon/support/adaption/TypeAdaptor.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/spoon/support/adaption/TypeAdaptor.java b/src/main/java/spoon/support/adaption/TypeAdaptor.java index 34bac74c489..a214df4a334 100644 --- a/src/main/java/spoon/support/adaption/TypeAdaptor.java +++ b/src/main/java/spoon/support/adaption/TypeAdaptor.java @@ -450,13 +450,17 @@ public boolean isOverriding(CtMethod subMethod, CtMethod superMethod) { if (!isSubtype(subDeclaringType, superDeclaringType.getReference())) { return false; } - + // we lock here because of multi threading issues, see https://github.com/INRIA/spoon/issues/5619 + CtMethod adapted; // 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); + synchronized (superMethod) { + CtBlock superBody = superMethod.getBody(); + superBody.clone(); + superMethod.setBody(null); + 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);