|
| 1 | +package datadog.opentelemetry.tooling; |
| 2 | + |
| 3 | +import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_INTERFACE; |
| 4 | +import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_NON_FINAL; |
| 5 | +import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_NON_INTERFACE; |
| 6 | +import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_NON_PRIVATE; |
| 7 | +import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_NON_STATIC; |
| 8 | +import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_PUBLIC; |
| 9 | +import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_PUBLIC_OR_PROTECTED; |
| 10 | +import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_STATIC; |
| 11 | + |
| 12 | +import datadog.trace.agent.tooling.muzzle.Reference; |
| 13 | +import java.util.Collection; |
| 14 | +import net.bytebuddy.jar.asm.Type; |
| 15 | + |
| 16 | +/** Maps OpenTelemetry muzzle references to the Datadog equivalent. */ |
| 17 | +public final class OtelMuzzleRefBuilder { |
| 18 | + private final Reference.Builder builder; |
| 19 | + |
| 20 | + OtelMuzzleRefBuilder(String className) { |
| 21 | + this.builder = new Reference.Builder(className); |
| 22 | + } |
| 23 | + |
| 24 | + public OtelMuzzleRefBuilder setSuperClassName(String superName) { |
| 25 | + builder.withSuperName(superName); |
| 26 | + return this; |
| 27 | + } |
| 28 | + |
| 29 | + public OtelMuzzleRefBuilder addInterfaceNames(Collection<String> interfaceNames) { |
| 30 | + for (String interfaceName : interfaceNames) { |
| 31 | + builder.withInterface(interfaceName); |
| 32 | + } |
| 33 | + return this; |
| 34 | + } |
| 35 | + |
| 36 | + public OtelMuzzleRefBuilder addInterfaceName(String interfaceName) { |
| 37 | + builder.withInterface(interfaceName); |
| 38 | + return this; |
| 39 | + } |
| 40 | + |
| 41 | + public OtelMuzzleRefBuilder addSource(String sourceName) { |
| 42 | + builder.withSource(sourceName, 0); |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + public OtelMuzzleRefBuilder addSource(String sourceName, int line) { |
| 47 | + builder.withSource(sourceName, line); |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + public OtelMuzzleRefBuilder addFlag(Flag flag) { |
| 52 | + builder.withFlag(flag.bit); |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + public OtelMuzzleRefBuilder addField( |
| 57 | + Source[] sources, Flag[] flags, String fieldName, Type fieldType, boolean isDeclared) { |
| 58 | + builder.withField(Source.flatten(sources), Flag.flatten(flags), fieldName, fieldType); |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + public OtelMuzzleRefBuilder addMethod( |
| 63 | + Source[] sources, Flag[] flags, String methodName, Type returnType, Type... argumentTypes) { |
| 64 | + builder.withMethod( |
| 65 | + Source.flatten(sources), Flag.flatten(flags), methodName, returnType, argumentTypes); |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + public ClassRef build() { |
| 70 | + return new ClassRef(builder.build()); |
| 71 | + } |
| 72 | + |
| 73 | + public static final class ClassRef extends Reference { |
| 74 | + public ClassRef(Reference ref) { |
| 75 | + super( |
| 76 | + ref.sources, |
| 77 | + ref.flags, |
| 78 | + ref.className, |
| 79 | + ref.superName, |
| 80 | + ref.interfaces, |
| 81 | + ref.fields, |
| 82 | + ref.methods); |
| 83 | + } |
| 84 | + |
| 85 | + public static OtelMuzzleRefBuilder builder(String className) { |
| 86 | + return new OtelMuzzleRefBuilder(className); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public enum Flag { |
| 91 | + PUBLIC(EXPECTS_PUBLIC), |
| 92 | + PROTECTED_OR_HIGHER(EXPECTS_PUBLIC_OR_PROTECTED), |
| 93 | + PROTECTED(EXPECTS_PUBLIC_OR_PROTECTED), |
| 94 | + PACKAGE_OR_HIGHER(EXPECTS_NON_PRIVATE), |
| 95 | + PACKAGE(EXPECTS_NON_PRIVATE), |
| 96 | + PRIVATE_OR_HIGHER(0), |
| 97 | + PRIVATE(0), |
| 98 | + ABSTRACT(0), |
| 99 | + FINAL(0), |
| 100 | + NON_FINAL(EXPECTS_NON_FINAL), |
| 101 | + INTERFACE(EXPECTS_INTERFACE), |
| 102 | + NON_INTERFACE(EXPECTS_NON_INTERFACE), |
| 103 | + STATIC(EXPECTS_STATIC), |
| 104 | + NON_STATIC(EXPECTS_NON_STATIC); |
| 105 | + |
| 106 | + final int bit; |
| 107 | + |
| 108 | + Flag(int bit) { |
| 109 | + this.bit = bit; |
| 110 | + } |
| 111 | + |
| 112 | + public static int flatten(Flag[] flags) { |
| 113 | + int bits = 0; |
| 114 | + for (Flag flag : flags) { |
| 115 | + bits |= flag.bit; |
| 116 | + } |
| 117 | + return bits; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static final class Source { |
| 122 | + final String name; |
| 123 | + final int line; |
| 124 | + |
| 125 | + public Source(String name, int line) { |
| 126 | + this.name = name; |
| 127 | + this.line = line; |
| 128 | + } |
| 129 | + |
| 130 | + public static String[] flatten(Source[] sources) { |
| 131 | + String[] locations = new String[sources.length]; |
| 132 | + for (int i = 0; i < sources.length; i++) { |
| 133 | + locations[i] = sources[i].name + ":" + sources[i].line; |
| 134 | + } |
| 135 | + return locations; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments