-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Applying suggestions and use ClassValue
- Loading branch information
Showing
4 changed files
with
43 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 4 additions & 15 deletions
19
...tation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsAdvisingHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,11 @@ | ||
package datadog.trace.instrumentation.mule4; | ||
|
||
import java.util.WeakHashMap; | ||
import datadog.trace.api.GenericClassValue; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class JpmsAdvisingHelper { | ||
private static final WeakHashMap<Class<?>, Boolean> ALREADY_PROCESSED_CACHE = new WeakHashMap<>(); | ||
|
||
public static void allowAccessOnModuleClass(final Class<?> cls) { | ||
if (Boolean.TRUE.equals(ALREADY_PROCESSED_CACHE.putIfAbsent(cls, Boolean.TRUE))) { | ||
return; | ||
} | ||
final Module module = cls.getModule(); | ||
if (module != null) { | ||
try { | ||
module.addExports(cls.getPackageName(), module.getClassLoader().getUnnamedModule()); | ||
} catch (Throwable ignored) { | ||
} | ||
} | ||
} | ||
public static final ClassValue<AtomicBoolean> ALREADY_PROCESSED_CACHE = | ||
GenericClassValue.constructing(AtomicBoolean.class); | ||
|
||
private JpmsAdvisingHelper() {} | ||
} |
32 changes: 32 additions & 0 deletions
32
...ation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsClearanceAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package datadog.trace.instrumentation.mule4; | ||
|
||
import static datadog.trace.instrumentation.mule4.JpmsAdvisingHelper.ALREADY_PROCESSED_CACHE; | ||
|
||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.implementation.bytecode.assign.Assigner; | ||
import org.mule.runtime.tracer.api.EventTracer; | ||
|
||
public class JpmsClearanceAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void openOnReturn(@Advice.This(typing = Assigner.Typing.DYNAMIC) Object self) { | ||
final Class<?> cls = self.getClass(); | ||
if (ALREADY_PROCESSED_CACHE.get(cls).compareAndSet(false, true)) { | ||
final Module module = cls.getModule(); | ||
if (module != null) { | ||
try { | ||
// This call needs imperatively to be done from the same module we're adding exports | ||
// because the jdk is checking that the caller belongs to the same module. | ||
// The code of this advice is getting inlined into the constructor of the class belonging | ||
// to that package so it will work. Moving the same to a helper won't. | ||
module.addExports(cls.getPackageName(), module.getClassLoader().getUnnamedModule()); | ||
} catch (Throwable ignored) { | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void muzzleCheck(final EventTracer<?> tracer) { | ||
// introduced in 4.5.0 | ||
tracer.endCurrentSpan(null); | ||
} | ||
} |