class ProxyFactory {
  private static final Objenesis OBJENESIS = new ObjenesisStd();
  private static final ByteBuddy BYTEBUDDY = new ByteBuddy()
      .with(new NamingStrategy.SuffixingRandom("ByteBuddy", NO_PREFIX));
  private static final ElementMatcher<? super MethodDescription> METHOD_FILTER = not(
      named("hashCode").or(named("equals")));
  private static final Method PRIVATE_LOOKUP_IN;
  private static final Object LOOKUP;

  static {
    Method privateLookupIn;
    Object lookup;
    try {
      Class<?> methodHandles = Class.forName("java.lang.invoke.MethodHandles");
      lookup = methodHandles.getMethod("lookup").invoke(null);
      privateLookupIn = methodHandles.getMethod(
          "privateLookupIn",
          Class.class,
          Class.forName("java.lang.invoke.MethodHandles$Lookup")
      );
    } catch (Exception e) {
      privateLookupIn = null;
      lookup = null;
    }
    PRIVATE_LOOKUP_IN = privateLookupIn;
    LOOKUP = lookup;
  }

  /**
   * @throws ErrorsException if the proxy for {@code type} cannot be generated or instantiated
   */
  static <T> T proxyFor(Class<T> type, InvocationHandler interceptor, Errors errors)
      throws ErrorsException {
    return proxyFor(type, interceptor, errors, Boolean.FALSE);
  }

  /**
   * @throws ErrorsException if the proxy for {@code type} cannot be generated or instantiated
   */
  static <T> T proxyFor(Class<T> type, InvocationHandler interceptor, Errors errors, boolean useOSGiClassLoaderBridging)
      throws ErrorsException {
    if (Primitives.isPrimitive(type))
      return Primitives.defaultValueForWrapper(type);
    if (isProxyUnsupported(type))
      return null;
    if (Modifier.isFinal(type.getModifiers()))
      throw errors.invocationAgainstFinalClass(type).toException();

    try {
      final DynamicType.Unloaded<T> unloaded = BYTEBUDDY
          .subclass(type)
          .method(METHOD_FILTER)
          .intercept(InvocationHandlerAdapter.of(interceptor))
          .make();
      final ClassLoadingStrategy<ClassLoader> classLoadingStrategy = chooseClassLoadingStrategy(type);
      final ClassLoader classLoader = useOSGiClassLoaderBridging
          ? BridgeClassLoaderFactory.getClassLoader(type)
          : type.getClassLoader();
      if (classLoadingStrategy != null) {
        return OBJENESIS.newInstance(unloaded
            .load(classLoader, classLoadingStrategy)
            .getLoaded());
      } else {
        return OBJENESIS.newInstance(unloaded
            .load(classLoader)
            .getLoaded());
      }
    } catch (Throwable t) {
      throw errors.errorInstantiatingProxy(type, t).toException();
    }
  }

  private static boolean isProxyUnsupported(Class<?> type) {
    return type.equals(String.class)
        || type.equals(Object.class)
        || Collection.class.isAssignableFrom(type)
        || Number.class.isAssignableFrom(type);
  }

  private static <T> ClassLoadingStrategy<ClassLoader> chooseClassLoadingStrategy(Class<T> type) {<FILL_FUNCTION_BODY>}
}

    try {
      final ClassLoadingStrategy<ClassLoader> strategy;
      if (ClassInjector.UsingLookup.isAvailable() && PRIVATE_LOOKUP_IN != null && LOOKUP != null) {
        Object privateLookup = PRIVATE_LOOKUP_IN.invoke(null, type, LOOKUP);
        strategy = ClassLoadingStrategy.UsingLookup.of(privateLookup);
      } else if (ClassInjector.UsingReflection.isAvailable()) {
        strategy = ClassLoadingStrategy.Default.INJECTION;
      } else {
        throw new IllegalStateException("No code generation strategy available");
      }
      return strategy;
    } catch (InvocationTargetException e) {
      throw new IllegalStateException("Failed to invoke 'privateLookupIn' method from java.lang.invoke.MethodHandles$Lookup.", e);
    } catch (IllegalAccessException e) {
      throw new IllegalStateException("Failed to invoke 'privateLookupIn' method from java.lang.invoke.MethodHandles$Lookup.", e);
    }
  
<no_super_class>