Skip to content

Commit b7dc70d

Browse files
authored
[8.x] Simplify instrumenter and tests (#118493) (#118714)
This commit simplifies the entitlements instrumentation service and instrumenter a bit. It especially removes some repetition in the instrumenter tests.
1 parent eff77c3 commit b7dc70d

File tree

8 files changed

+221
-327
lines changed

8 files changed

+221
-327
lines changed

libs/entitlement/asm-provider/src/main/java/org/elasticsearch/entitlement/instrumentation/impl/InstrumentationServiceImpl.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@
2929
public class InstrumentationServiceImpl implements InstrumentationService {
3030

3131
@Override
32-
public Instrumenter newInstrumenter(Map<MethodKey, CheckMethod> checkMethods) {
33-
return InstrumenterImpl.create(checkMethods);
32+
public Instrumenter newInstrumenter(Class<?> clazz, Map<MethodKey, CheckMethod> methods) {
33+
return InstrumenterImpl.create(clazz, methods);
3434
}
3535

3636
@Override
37-
public Map<MethodKey, CheckMethod> lookupMethodsToInstrument(String entitlementCheckerClassName) throws ClassNotFoundException,
38-
IOException {
37+
public Map<MethodKey, CheckMethod> lookupMethods(Class<?> checkerClass) throws IOException {
3938
var methodsToInstrument = new HashMap<MethodKey, CheckMethod>();
40-
var checkerClass = Class.forName(entitlementCheckerClassName);
4139
var classFileInfo = InstrumenterImpl.getClassFileInfo(checkerClass);
4240
ClassReader reader = new ClassReader(classFileInfo.bytecodes());
4341
ClassVisitor visitor = new ClassVisitor(Opcodes.ASM9) {

libs/entitlement/asm-provider/src/main/java/org/elasticsearch/entitlement/instrumentation/impl/InstrumenterImpl.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,14 @@ public class InstrumenterImpl implements Instrumenter {
5858
this.checkMethods = checkMethods;
5959
}
6060

61-
static String getCheckerClassName() {
62-
int javaVersion = Runtime.version().feature();
63-
final String classNamePrefix;
64-
if (javaVersion >= 23) {
65-
classNamePrefix = "Java23";
66-
} else {
67-
classNamePrefix = "";
68-
}
69-
return "org/elasticsearch/entitlement/bridge/" + classNamePrefix + "EntitlementChecker";
70-
}
71-
72-
public static InstrumenterImpl create(Map<MethodKey, CheckMethod> checkMethods) {
73-
String checkerClass = getCheckerClassName();
74-
String handleClass = checkerClass + "Handle";
75-
String getCheckerClassMethodDescriptor = Type.getMethodDescriptor(Type.getObjectType(checkerClass));
61+
public static InstrumenterImpl create(Class<?> checkerClass, Map<MethodKey, CheckMethod> checkMethods) {
62+
Type checkerClassType = Type.getType(checkerClass);
63+
String handleClass = checkerClassType.getInternalName() + "Handle";
64+
String getCheckerClassMethodDescriptor = Type.getMethodDescriptor(checkerClassType);
7665
return new InstrumenterImpl(handleClass, getCheckerClassMethodDescriptor, "", checkMethods);
7766
}
7867

79-
public ClassFileInfo instrumentClassFile(Class<?> clazz) throws IOException {
80-
ClassFileInfo initial = getClassFileInfo(clazz);
81-
return new ClassFileInfo(initial.fileName(), instrumentClass(Type.getInternalName(clazz), initial.bytecodes()));
82-
}
83-
84-
public static ClassFileInfo getClassFileInfo(Class<?> clazz) throws IOException {
68+
static ClassFileInfo getClassFileInfo(Class<?> clazz) throws IOException {
8569
String internalName = Type.getInternalName(clazz);
8670
String fileName = "/" + internalName + ".class";
8771
byte[] originalBytecodes;
@@ -306,5 +290,5 @@ protected void pushEntitlementChecker(MethodVisitor mv) {
306290
mv.visitMethodInsn(INVOKESTATIC, handleClass, "instance", getCheckerClassMethodDescriptor, false);
307291
}
308292

309-
public record ClassFileInfo(String fileName, byte[] bytecodes) {}
293+
record ClassFileInfo(String fileName, byte[] bytecodes) {}
310294
}

libs/entitlement/asm-provider/src/test/java/org/elasticsearch/entitlement/instrumentation/impl/InstrumentationServiceImplTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ interface TestCheckerCtors {
5151
void check$org_example_TestTargetClass$(Class<?> clazz, int x, String y);
5252
}
5353

54-
public void testInstrumentationTargetLookup() throws IOException, ClassNotFoundException {
55-
Map<MethodKey, CheckMethod> checkMethods = instrumentationService.lookupMethodsToInstrument(TestChecker.class.getName());
54+
public void testInstrumentationTargetLookup() throws IOException {
55+
Map<MethodKey, CheckMethod> checkMethods = instrumentationService.lookupMethods(TestChecker.class);
5656

5757
assertThat(checkMethods, aMapWithSize(3));
5858
assertThat(
@@ -116,8 +116,8 @@ public void testInstrumentationTargetLookup() throws IOException, ClassNotFoundE
116116
);
117117
}
118118

119-
public void testInstrumentationTargetLookupWithOverloads() throws IOException, ClassNotFoundException {
120-
Map<MethodKey, CheckMethod> checkMethods = instrumentationService.lookupMethodsToInstrument(TestCheckerOverloads.class.getName());
119+
public void testInstrumentationTargetLookupWithOverloads() throws IOException {
120+
Map<MethodKey, CheckMethod> checkMethods = instrumentationService.lookupMethods(TestCheckerOverloads.class);
121121

122122
assertThat(checkMethods, aMapWithSize(2));
123123
assertThat(
@@ -148,8 +148,8 @@ public void testInstrumentationTargetLookupWithOverloads() throws IOException, C
148148
);
149149
}
150150

151-
public void testInstrumentationTargetLookupWithCtors() throws IOException, ClassNotFoundException {
152-
Map<MethodKey, CheckMethod> checkMethods = instrumentationService.lookupMethodsToInstrument(TestCheckerCtors.class.getName());
151+
public void testInstrumentationTargetLookupWithCtors() throws IOException {
152+
Map<MethodKey, CheckMethod> checkMethods = instrumentationService.lookupMethods(TestCheckerCtors.class);
153153

154154
assertThat(checkMethods, aMapWithSize(2));
155155
assertThat(

0 commit comments

Comments
 (0)