Java detect if class is a proxy

Johan Sjöberg picture Johan Sjöberg · Sep 21, 2011 · Viewed 8.3k times · Source

Is it possible to detect if a class is a proxy (dynamic, cglib or otherwise)?

Let classes Aand B implement a common interface I. Then I need to define a routine classEquals of signature

public boolean classEquals(Class<? extends I> a, Class<? extends I> b);

such that it evaluates to true only if a.equals(b) or Proxy(a).equals(b), where Proxy(a) denotes a dynamic proxy of type A (dynamic, cglib or otherwise).


With the assistance of @Jigar Joshi, this is what it looks like so far:

public boolean classEquals(Class a, Class b) {
    if (Proxy.isProxyClass(a)) {
        return classEquals(a.getSuperclass(), b);
    }
    return a.equals(b);
}

The problem is that it doesn't detect e.g., a CGLIB proxy.

Answer