java - Comparing classes up to CGLib enhancement -


i want test if 2 values have same class:

x.getclass.equals(y.getclass) 

however, comparison should succeed if 1 (or both) values belongs class constructed cglib enhancer.

the obvious solution search $$enhancerbycglib$$... in class name, remove it, , compare remaining parts of class names (and classloaders). there better alternative?

cglib allows user set namingstrategy makes replacing enhancerbycglib tag unreliable class can named arbitrarily.

the reliable way of identifying cglib class check existance of field cglib$bound hard-coded library such name cannot change. if such field exists in class, need check if:

  1. there interfaces implemented. if @ least 1 interface exists, enhanced class might interface. (you might find cglib's factory interface must ignore.)
  2. there super class not java.lang.object. if interface enhanced, there super class defined.

as approximation detection algorithm, therefore use:

static class<?> original(class<?> type) {   try {     type.getdeclaredfield("cglib$bound");     if (type.getsuperclass() != object.class) {       return type.getsuperclass();     }     (class<?> iface : type.getinterfaces()) {       if (iface != factory.class) {         return iface;       }     }     return object.class;   } catch (nosuchfieldexception ignored) {     return type;   } } 

note cglib allows generate proxies classes implement several interfaces above unrolling return super class.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -