|
| 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 static io.opentelemetry.javaagent.tooling.instrumentation.indy.ForceDynamicallyTypedAssignReturnedFactory.replaceAnnotationValue; |
| 9 | + |
| 10 | +import javax.annotation.Nonnull; |
| 11 | +import net.bytebuddy.agent.builder.AgentBuilder; |
| 12 | +import net.bytebuddy.asm.Advice; |
| 13 | +import net.bytebuddy.description.annotation.AnnotationDescription; |
| 14 | +import net.bytebuddy.description.annotation.AnnotationList; |
| 15 | +import net.bytebuddy.description.annotation.AnnotationValue; |
| 16 | +import net.bytebuddy.description.method.MethodDescription; |
| 17 | +import net.bytebuddy.description.method.MethodList; |
| 18 | +import net.bytebuddy.description.method.ParameterDescription; |
| 19 | +import net.bytebuddy.description.method.ParameterList; |
| 20 | +import net.bytebuddy.description.type.TypeDescription; |
| 21 | +import net.bytebuddy.description.type.TypeList; |
| 22 | +import net.bytebuddy.dynamic.ClassFileLocator; |
| 23 | +import net.bytebuddy.pool.TypePool; |
| 24 | +import org.jetbrains.annotations.NotNull; |
| 25 | + |
| 26 | +/** |
| 27 | + * Pool strategy that sets "inline" attribute to false on {@link Advice.OnMethodEnter} and {@link |
| 28 | + * Advice.OnMethodExit} annotations. |
| 29 | + */ |
| 30 | +class AdviceUninliningPoolStrategy implements AgentBuilder.PoolStrategy { |
| 31 | + private final AgentBuilder.PoolStrategy poolStrategy; |
| 32 | + |
| 33 | + public AdviceUninliningPoolStrategy(AgentBuilder.PoolStrategy poolStrategy) { |
| 34 | + this.poolStrategy = poolStrategy; |
| 35 | + } |
| 36 | + |
| 37 | + @NotNull |
| 38 | + @Override |
| 39 | + public TypePool typePool(@NotNull ClassFileLocator classFileLocator, ClassLoader classLoader) { |
| 40 | + TypePool typePool = poolStrategy.typePool(classFileLocator, classLoader); |
| 41 | + return new TypePoolWrapper(typePool); |
| 42 | + } |
| 43 | + |
| 44 | + @NotNull |
| 45 | + @Override |
| 46 | + public TypePool typePool( |
| 47 | + @NotNull ClassFileLocator classFileLocator, ClassLoader classLoader, @NotNull String name) { |
| 48 | + TypePool typePool = poolStrategy.typePool(classFileLocator, classLoader, name); |
| 49 | + return new TypePoolWrapper(typePool); |
| 50 | + } |
| 51 | + |
| 52 | + private static class TypePoolWrapper implements TypePool { |
| 53 | + private final TypePool typePool; |
| 54 | + |
| 55 | + public TypePoolWrapper(TypePool typePool) { |
| 56 | + this.typePool = typePool; |
| 57 | + } |
| 58 | + |
| 59 | + @NotNull |
| 60 | + @Override |
| 61 | + public Resolution describe(@NotNull String name) { |
| 62 | + Resolution resolution = typePool.describe(name); |
| 63 | + |
| 64 | + return new Resolution() { |
| 65 | + @Override |
| 66 | + public boolean isResolved() { |
| 67 | + return resolution.isResolved(); |
| 68 | + } |
| 69 | + |
| 70 | + @NotNull |
| 71 | + @Override |
| 72 | + public TypeDescription resolve() { |
| 73 | + TypeDescription typeDescription = resolution.resolve(); |
| 74 | + |
| 75 | + return new TypeDescription.AbstractBase.OfSimpleType.WithDelegation() { |
| 76 | + |
| 77 | + @NotNull |
| 78 | + @Override |
| 79 | + public String getName() { |
| 80 | + return typeDescription.getName(); |
| 81 | + } |
| 82 | + |
| 83 | + @NotNull |
| 84 | + @Override |
| 85 | + protected TypeDescription delegate() { |
| 86 | + return typeDescription; |
| 87 | + } |
| 88 | + |
| 89 | + @NotNull |
| 90 | + @Override |
| 91 | + public MethodList<MethodDescription.InDefinedShape> getDeclaredMethods() { |
| 92 | + MethodList<MethodDescription.InDefinedShape> methods = super.getDeclaredMethods(); |
| 93 | + |
| 94 | + class MethodListWrapper |
| 95 | + extends MethodList.AbstractBase<MethodDescription.InDefinedShape> { |
| 96 | + |
| 97 | + @Override |
| 98 | + public MethodDescription.InDefinedShape get(int index) { |
| 99 | + return new MethodDescriptionWrapper(methods.get(index)); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public int size() { |
| 104 | + return methods.size(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return new MethodListWrapper(); |
| 109 | + } |
| 110 | + }; |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void clear() { |
| 117 | + typePool.clear(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private static class MethodDescriptionWrapper extends DelegatingMethodDescription { |
| 122 | + |
| 123 | + MethodDescriptionWrapper(MethodDescription.InDefinedShape method) { |
| 124 | + super(method); |
| 125 | + } |
| 126 | + |
| 127 | + @NotNull |
| 128 | + @Override |
| 129 | + public AnnotationList getDeclaredAnnotations() { |
| 130 | + AnnotationList annotations = method.getDeclaredAnnotations(); |
| 131 | + |
| 132 | + class AnnotationListWrapper extends AnnotationList.AbstractBase { |
| 133 | + |
| 134 | + @Override |
| 135 | + public AnnotationDescription get(int index) { |
| 136 | + AnnotationDescription annotation = annotations.get(index); |
| 137 | + String annotationTypeName = annotation.getAnnotationType().getActualName(); |
| 138 | + // we are only interested in OnMethodEnter and OnMethodExit annotations |
| 139 | + if (!Advice.OnMethodEnter.class.getName().equals(annotationTypeName) |
| 140 | + && !Advice.OnMethodExit.class.getName().equals(annotationTypeName)) { |
| 141 | + return annotation; |
| 142 | + } |
| 143 | + |
| 144 | + // replace value for "inline" attribute with false |
| 145 | + return replaceAnnotationValue( |
| 146 | + annotation, "inline", oldVal -> AnnotationValue.ForConstant.of(false)); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public int size() { |
| 151 | + return annotations.size(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return new AnnotationListWrapper(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static class DelegatingMethodDescription |
| 160 | + extends MethodDescription.InDefinedShape.AbstractBase { |
| 161 | + protected final MethodDescription.InDefinedShape method; |
| 162 | + |
| 163 | + DelegatingMethodDescription(MethodDescription.InDefinedShape method) { |
| 164 | + this.method = method; |
| 165 | + } |
| 166 | + |
| 167 | + @Nonnull |
| 168 | + @Override |
| 169 | + public TypeDescription getDeclaringType() { |
| 170 | + return method.getDeclaringType(); |
| 171 | + } |
| 172 | + |
| 173 | + @NotNull |
| 174 | + @Override |
| 175 | + public TypeDescription.Generic getReturnType() { |
| 176 | + return method.getReturnType(); |
| 177 | + } |
| 178 | + |
| 179 | + @NotNull |
| 180 | + @Override |
| 181 | + public ParameterList<ParameterDescription.InDefinedShape> getParameters() { |
| 182 | + return method.getParameters(); |
| 183 | + } |
| 184 | + |
| 185 | + @NotNull |
| 186 | + @Override |
| 187 | + public TypeList.Generic getExceptionTypes() { |
| 188 | + return method.getExceptionTypes(); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public AnnotationValue<?, ?> getDefaultValue() { |
| 193 | + return method.getDefaultValue(); |
| 194 | + } |
| 195 | + |
| 196 | + @NotNull |
| 197 | + @Override |
| 198 | + public String getInternalName() { |
| 199 | + return method.getInternalName(); |
| 200 | + } |
| 201 | + |
| 202 | + @NotNull |
| 203 | + @Override |
| 204 | + public TypeList.Generic getTypeVariables() { |
| 205 | + return method.getTypeVariables(); |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public int getModifiers() { |
| 210 | + return method.getModifiers(); |
| 211 | + } |
| 212 | + |
| 213 | + @NotNull |
| 214 | + @Override |
| 215 | + public AnnotationList getDeclaredAnnotations() { |
| 216 | + return method.getDeclaredAnnotations(); |
| 217 | + } |
| 218 | + } |
| 219 | +} |
0 commit comments