20

I am trying to obtain some classes name by getClass().getSimpleName() under Spring and it returns something like

MyClass$$EnhancerBySpringCGLIB$$SOMEHEX

This is probably because Spring wraps the class into proxy.

Is there any portable way to obtain original class name?

Dims
  • 42,427
  • 94
  • 291
  • 543

1 Answers1

37

Spring provides a utility for this.

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/ClassUtils.html#getUserClass-java.lang.Class-

public static Class<?> getUserClass(Class<?> clazz)

"Return the user-defined class for the given class: usually simply the given class, but the original class in case of a CGLIB-generated subclass."

James Watkins
  • 4,556
  • 5
  • 28
  • 40
  • I forgot to mention: this only works for objects proxied by spring. There is no general solution for detecting the target type of any given proxy. The way I solve this is by explicitly defining the target type of my proxies external to the proxy itself. – James Watkins Dec 21 '17 at 15:50
  • Thank you! This worked while other suggestions such as `TargetAwareClass` failed on class cast exception. – Don Rhummy Mar 14 '19 at 00:51
  • 5
    In spring boot: `log.debug(ClassUtils.getUserClass(constraintDescriptor.getAnnotation()).getName());` is `com.sun.proxy.$Proxy153` :-/ – e-info128 Aug 11 '20 at 07:23
  • How it works when Spring creates the proxy class using JDK dynamic proxies mechanism instead of cglib? – amseager Oct 01 '20 at 07:49
  • @e-info128 : did you find a solution ? – serv-inc Dec 22 '21 at 14:18