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:
- there interfaces implemented. if @ least 1 interface exists, enhanced class might interface. (you might find cglib's
factory
interface must ignore.) - 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
Post a Comment