|
| 1 | +package datadog.trace.agent.tooling.bytebuddy.advice.transformation; |
| 2 | + |
| 3 | +import static net.bytebuddy.matcher.ElementMatchers.any; |
| 4 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 5 | + |
| 6 | +import java.lang.reflect.Method; |
| 7 | +import java.util.concurrent.AbstractExecutorService; |
| 8 | +import lombok.SneakyThrows; |
| 9 | +import net.bytebuddy.asm.MemberSubstitution; |
| 10 | +import net.bytebuddy.build.Plugin; |
| 11 | +import net.bytebuddy.description.ByteCodeElement; |
| 12 | +import net.bytebuddy.description.method.MethodDescription; |
| 13 | +import net.bytebuddy.description.type.TypeDescription; |
| 14 | +import net.bytebuddy.description.type.TypeList; |
| 15 | +import net.bytebuddy.dynamic.ClassFileLocator; |
| 16 | +import net.bytebuddy.dynamic.DynamicType; |
| 17 | +import net.bytebuddy.implementation.bytecode.StackManipulation; |
| 18 | +import net.bytebuddy.implementation.bytecode.member.MethodInvocation; |
| 19 | +import net.bytebuddy.pool.TypePool; |
| 20 | +import net.bytebuddy.utility.CompoundList; |
| 21 | + |
| 22 | +/** |
| 23 | + * This Plugin is responsible for modifying WrapRunnableAsNewTaskInstrumentation's advice so it is |
| 24 | + * able to make a call to a protected method. |
| 25 | + */ |
| 26 | +public final class NewTaskForAdvicePlugin implements Plugin { |
| 27 | + Method newTaskForMethod; |
| 28 | + |
| 29 | + @SneakyThrows |
| 30 | + public NewTaskForAdvicePlugin() { |
| 31 | + newTaskForMethod = |
| 32 | + AbstractExecutorService.class.getDeclaredMethod("newTaskFor", Runnable.class, Object.class); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean matches(TypeDescription target) { |
| 37 | + return named( |
| 38 | + "datadog.trace.instrumentation.java.concurrent.WrapRunnableAsNewTaskInstrumentation$Wrap") |
| 39 | + .matches(target); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public DynamicType.Builder<?> apply( |
| 44 | + DynamicType.Builder<?> builder, |
| 45 | + TypeDescription typeDescription, |
| 46 | + ClassFileLocator classFileLocator) { |
| 47 | + return builder.visit( |
| 48 | + MemberSubstitution.strict() |
| 49 | + .method(named("superNewTaskFor")) |
| 50 | + // https://github.com/raphw/byte-buddy/issues/976 |
| 51 | + // .replaceWith(newTaskForMethod) |
| 52 | + .replaceWith(new VisibilityIgnoringSubstitution(newTaskForMethod)) |
| 53 | + .on(any())); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void close() {} |
| 58 | + |
| 59 | + // This is a hack until https://github.com/raphw/byte-buddy/issues/976 is resolved. |
| 60 | + private static final class VisibilityIgnoringSubstitution |
| 61 | + implements MemberSubstitution.Substitution.Factory { |
| 62 | + private final MethodDescription methodDescription; |
| 63 | + |
| 64 | + public VisibilityIgnoringSubstitution(Method method) { |
| 65 | + this.methodDescription = new MethodDescription.ForLoadedMethod(method); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public MemberSubstitution.Substitution make( |
| 70 | + TypeDescription instrumentedType, MethodDescription instrumentedMethod, TypePool typePool) { |
| 71 | + return new VisibilityIgnoringSubstitutionInvocation( |
| 72 | + instrumentedType, |
| 73 | + new MemberSubstitution.Substitution.ForMethodInvocation.MethodResolver.Simple( |
| 74 | + methodDescription)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static final class VisibilityIgnoringSubstitutionInvocation |
| 79 | + extends MemberSubstitution.Substitution.ForMethodInvocation { |
| 80 | + |
| 81 | + /** The index of the this reference within a non-static method. */ |
| 82 | + private static final int THIS_REFERENCE = 0; |
| 83 | + |
| 84 | + private final MethodResolver methodResolver; |
| 85 | + |
| 86 | + public VisibilityIgnoringSubstitutionInvocation( |
| 87 | + TypeDescription instrumentedType, MethodResolver methodResolver) { |
| 88 | + super(instrumentedType, methodResolver); |
| 89 | + this.methodResolver = methodResolver; |
| 90 | + } |
| 91 | + |
| 92 | + // Copied from net.bytebuddy.asm.MemberSubstitution.Substitution.ForMethodInvocation so the |
| 93 | + // visibility restrictions could be removed. |
| 94 | + @Override |
| 95 | + public StackManipulation resolve( |
| 96 | + TypeDescription targetType, |
| 97 | + ByteCodeElement target, |
| 98 | + TypeList.Generic parameters, |
| 99 | + TypeDescription.Generic result, |
| 100 | + int freeOffset) { |
| 101 | + MethodDescription methodDescription = |
| 102 | + methodResolver.resolve(targetType, target, parameters, result); |
| 103 | + |
| 104 | + // if (!methodDescription.isAccessibleTo(instrumentedType)) { |
| 105 | + // throw new IllegalStateException(instrumentedType + " cannot access " + |
| 106 | + // methodDescription); |
| 107 | + // } |
| 108 | + |
| 109 | + TypeList.Generic mapped = |
| 110 | + methodDescription.isStatic() |
| 111 | + ? methodDescription.getParameters().asTypeList() |
| 112 | + : new TypeList.Generic.Explicit( |
| 113 | + CompoundList.of( |
| 114 | + methodDescription.getDeclaringType(), |
| 115 | + methodDescription.getParameters().asTypeList())); |
| 116 | + |
| 117 | + if (!methodDescription.getReturnType().asErasure().isAssignableTo(result.asErasure())) { |
| 118 | + throw new IllegalStateException( |
| 119 | + "Cannot assign return value of " + methodDescription + " to " + result); |
| 120 | + } else if (mapped.size() != parameters.size()) { |
| 121 | + throw new IllegalStateException( |
| 122 | + "Cannot invoke " + methodDescription + " on " + parameters.size() + " parameters"); |
| 123 | + } |
| 124 | + for (int index = 0; index < mapped.size(); index++) { |
| 125 | + if (!parameters.get(index).asErasure().isAssignableTo(mapped.get(index).asErasure())) { |
| 126 | + throw new IllegalStateException( |
| 127 | + "Cannot invoke " |
| 128 | + + methodDescription |
| 129 | + + " on parameter " |
| 130 | + + index |
| 131 | + + " of type " |
| 132 | + + parameters.get(index)); |
| 133 | + } |
| 134 | + } |
| 135 | + return methodDescription.isVirtual() |
| 136 | + ? MethodInvocation.invoke(methodDescription) |
| 137 | + .virtual(mapped.get(THIS_REFERENCE).asErasure()) |
| 138 | + : MethodInvocation.invoke(methodDescription); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments