|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.tooling.instrumentation.indy; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.function.Function; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import net.bytebuddy.asm.Advice; |
| 15 | +import net.bytebuddy.description.annotation.AnnotationDescription; |
| 16 | +import net.bytebuddy.description.annotation.AnnotationValue; |
| 17 | +import net.bytebuddy.description.enumeration.EnumerationDescription; |
| 18 | +import net.bytebuddy.description.method.MethodDescription; |
| 19 | +import net.bytebuddy.description.type.TypeDescription; |
| 20 | +import net.bytebuddy.implementation.bytecode.assign.Assigner; |
| 21 | +import net.bytebuddy.matcher.ElementMatchers; |
| 22 | + |
| 23 | +/** |
| 24 | + * This factory is designed to wrap around {@link Advice.PostProcessor.Factory} and ensures that |
| 25 | + * {@link net.bytebuddy.implementation.bytecode.assign.Assigner.Typing#DYNAMIC} is used everywhere. |
| 26 | + * |
| 27 | + * <p>This helps by avoiding errors where the instrumented bytecode is suddenly unloadable due to |
| 28 | + * incompatible assignments and preventing cluttering advice code annotations with the explicit |
| 29 | + * typing. |
| 30 | + */ |
| 31 | +public class ForceDynamicallyTypedAssignReturnedFactory implements Advice.PostProcessor.Factory { |
| 32 | + |
| 33 | + private static final String TO_ARGUMENTS_TYPENAME = |
| 34 | + Advice.AssignReturned.ToArguments.class.getName(); |
| 35 | + private static final String TO_ARGUMENT_TYPENAME = |
| 36 | + Advice.AssignReturned.ToArguments.ToArgument.class.getName(); |
| 37 | + private static final String TO_ALL_ARGUMENTS_TYPENAME = |
| 38 | + Advice.AssignReturned.ToAllArguments.class.getName(); |
| 39 | + private static final String TO_THIS_TYPENAME = Advice.AssignReturned.ToThis.class.getName(); |
| 40 | + private static final String TO_FIELDS_TYPENAME = Advice.AssignReturned.ToFields.class.getName(); |
| 41 | + private static final String TO_FIELD_TYPENAME = |
| 42 | + Advice.AssignReturned.ToFields.ToField.class.getName(); |
| 43 | + private static final String TO_RETURNED_TYPENAME = |
| 44 | + Advice.AssignReturned.ToReturned.class.getName(); |
| 45 | + private static final String TO_THROWN_TYPENAME = Advice.AssignReturned.ToThrown.class.getName(); |
| 46 | + private static final EnumerationDescription DYNAMIC_TYPING = |
| 47 | + new EnumerationDescription.ForLoadedEnumeration(Assigner.Typing.DYNAMIC); |
| 48 | + |
| 49 | + private final Advice.PostProcessor.Factory delegate; |
| 50 | + |
| 51 | + public ForceDynamicallyTypedAssignReturnedFactory(Advice.PostProcessor.Factory delegate) { |
| 52 | + this.delegate = delegate; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Advice.PostProcessor make(MethodDescription.InDefinedShape adviceMethod, boolean exit) { |
| 57 | + return delegate.make(forceDynamicTyping(adviceMethod), exit); |
| 58 | + } |
| 59 | + |
| 60 | + // Visible for testing |
| 61 | + static MethodDescription.InDefinedShape forceDynamicTyping( |
| 62 | + MethodDescription.InDefinedShape adviceMethod) { |
| 63 | + return new MethodDescription.Latent( |
| 64 | + adviceMethod.getDeclaringType(), |
| 65 | + adviceMethod.getInternalName(), |
| 66 | + adviceMethod.getModifiers(), |
| 67 | + adviceMethod.getTypeVariables().asTokenList(ElementMatchers.none()), |
| 68 | + adviceMethod.getReturnType(), |
| 69 | + adviceMethod.getParameters().asTokenList(ElementMatchers.none()), |
| 70 | + adviceMethod.getExceptionTypes(), |
| 71 | + forceDynamicTyping(adviceMethod.getDeclaredAnnotations()), |
| 72 | + adviceMethod.getDefaultValue(), |
| 73 | + adviceMethod.getReceiverType()); |
| 74 | + } |
| 75 | + |
| 76 | + private static List<? extends AnnotationDescription> forceDynamicTyping( |
| 77 | + List<? extends AnnotationDescription> declaredAnnotations) { |
| 78 | + return declaredAnnotations.stream() |
| 79 | + .map(ForceDynamicallyTypedAssignReturnedFactory::forceDynamicTyping) |
| 80 | + .collect(Collectors.toList()); |
| 81 | + } |
| 82 | + |
| 83 | + private static AnnotationDescription forceDynamicTyping(AnnotationDescription anno) { |
| 84 | + |
| 85 | + String name = anno.getAnnotationType().getName(); |
| 86 | + if (name.equals(TO_FIELD_TYPENAME) |
| 87 | + || name.equals(TO_ARGUMENT_TYPENAME) |
| 88 | + || name.equals(TO_THIS_TYPENAME) |
| 89 | + || name.equals(TO_ALL_ARGUMENTS_TYPENAME) |
| 90 | + || name.equals(TO_RETURNED_TYPENAME) |
| 91 | + || name.equals(TO_THROWN_TYPENAME)) { |
| 92 | + return replaceAnnotationValue( |
| 93 | + anno, "typing", oldVal -> AnnotationValue.ForEnumerationDescription.of(DYNAMIC_TYPING)); |
| 94 | + } else if (name.equals(TO_FIELDS_TYPENAME) || name.equals(TO_ARGUMENTS_TYPENAME)) { |
| 95 | + return replaceAnnotationValue( |
| 96 | + anno, |
| 97 | + "value", |
| 98 | + oldVal -> { |
| 99 | + if (!oldVal.getState().isDefined()) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + AnnotationDescription[] resolve = (AnnotationDescription[]) oldVal.resolve(); |
| 103 | + if (resolve.length == 0) { |
| 104 | + return oldVal; |
| 105 | + } |
| 106 | + AnnotationDescription[] newValueList = |
| 107 | + Arrays.stream(resolve) |
| 108 | + .map(ForceDynamicallyTypedAssignReturnedFactory::forceDynamicTyping) |
| 109 | + .toArray(AnnotationDescription[]::new); |
| 110 | + TypeDescription subType = newValueList[0].getAnnotationType(); |
| 111 | + return AnnotationValue.ForDescriptionArray.of(subType, newValueList); |
| 112 | + }); |
| 113 | + } |
| 114 | + return anno; |
| 115 | + } |
| 116 | + |
| 117 | + private static AnnotationDescription replaceAnnotationValue( |
| 118 | + AnnotationDescription anno, |
| 119 | + String propertyName, |
| 120 | + Function<AnnotationValue<?, ?>, AnnotationValue<?, ?>> valueMapper) { |
| 121 | + AnnotationValue<?, ?> oldValue = anno.getValue(propertyName); |
| 122 | + AnnotationValue<?, ?> newValue = valueMapper.apply(oldValue); |
| 123 | + Map<String, AnnotationValue<?, ?>> updatedValues = new HashMap<>(); |
| 124 | + for (MethodDescription.InDefinedShape property : |
| 125 | + anno.getAnnotationType().getDeclaredMethods()) { |
| 126 | + AnnotationValue<?, ?> value = anno.getValue(property); |
| 127 | + if (!propertyName.equals(property.getName()) && value.getState().isDefined()) { |
| 128 | + updatedValues.put(property.getName(), value); |
| 129 | + } |
| 130 | + } |
| 131 | + if (newValue != null) { |
| 132 | + updatedValues.put(propertyName, newValue); |
| 133 | + } |
| 134 | + return new AnnotationDescription.Latent(anno.getAnnotationType(), updatedValues) {}; |
| 135 | + } |
| 136 | +} |
0 commit comments