Pluggable naming for `proxyName` in `AbstractGlueGenerator`

21 views
Skip to first unread message

Behrooz Nobakht

unread,
May 27, 2025, 12:29:57 AMMay 27
to google-guice

When using Interception API with Guice, the enhanced classes by Guice follow a specific pattern which is controlled/generated by Enhancer or AbstractGlueGenerator. The naming pattern includes the hashCode(). The design choice is understandable.

However, there're use cases where the Class.getName() is distorted by the naming pattern. For example, consider a continuous profiling that runs on an application. In every instance of JVM, the class names are generated with a different hashCode(). Maybe there's a way to always ensure a consistent hashCode() but that also depends on the JVM.

In this use case, since the name of the classes are different per JVM instance, the collected profiling data appear as if they're collected from different object types; though the underlying object is all the same.

For example, if MyService is intercepted, then it appears in the profiling data as both MyService$$EnhancedByGuice$$111 and MyService$$EnhancedByGuice$$222. They are both the same type but intercepted in different JVM instances or the same JVM at different times.

I am looking for a way to extend/configure/plug a way into how Guice generates the proxyName -- link


===
protected AbstractGlueGenerator(Class<?> hostClass, String marker) {
    this.hostClass = hostClass;
    this.hostName = Type.getInternalName(hostClass);
    this.proxyName = proxyName(hostName, marker, hashCode());
  }

  /** Generates a unique name based on the original class name and marker. */
  private static String proxyName(String hostName, String marker, int hash) {
    long id = ((hash & 0x000FFFFF) | (COUNTER.getAndIncrement() << 20));
    String proxyName = hostName + marker + Long.toHexString(id);
    if (proxyName.startsWith("java/") && !ClassDefining.hasPackageAccess()) {
      proxyName = '$' + proxyName; // can't define java.* glue in same package
    }
    return proxyName;
  }
===

Thanks,
Behrooz

Behrooz Nobakht

unread,
May 27, 2025, 12:31:18 AMMay 27
to google-guice
I was not sure so I also raised the same question here
Reply all
Reply to author
Forward
0 new messages