Skip to content

Instantly share code, notes, and snippets.

@scottmarlow
Created June 10, 2025 12:22
Show Gist options
  • Select an option

  • Save scottmarlow/27d346f66847c6e83cbfa980bd691c2e to your computer and use it in GitHub Desktop.

Select an option

Save scottmarlow/27d346f66847c6e83cbfa980bd691c2e to your computer and use it in GitHub Desktop.
public Class<?> load(Class<?> referenceClass, String className, BiFunction<ByteBuddy, NamingStrategy, DynamicType.Builder<?>> makeClassFunction) {
if (result.getClassLoader() == referenceClass.getClassLoader()) {
try {
return referenceClass.getClassLoader().loadClass(className);
}
catch (ClassNotFoundException e) {
// Ignore
}
}
try {
return make( makeClassFunction.apply( byteBuddy, new FixedNamingStrategy( className ) ) )
.load(
referenceClass.getClassLoader(),
resolveClassLoadingStrategy( referenceClass )
)
.getLoaded();
}
catch (LinkageError e) {
try {
return referenceClass.getClassLoader().loadClass( className );
}
catch (ClassNotFoundException ex) {
throw new RuntimeException( "Couldn't load or define class [" + className + "]", e );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment