ByteArrayClassLoader byteArrayClassLoader = cache.getByteArrayClassLoader();
Map<Class< ? >, ClassFieldInspector> inspectors = cache.getInspectors();
try {
// otherwise, bytecode generate a specific extractor
ClassFieldInspector inspector = inspectors.get( clazz );
if ( inspector == null ) {
inspector = new ClassFieldInspector( clazz );
inspectors.put( clazz,
inspector );
}
final Method setterMethod = (Method) inspector.getSetterMethods().get( fieldName );
if ( setterMethod != null ) {
final Class< ? > fieldType = setterMethod.getParameterTypes()[0];
final String className = ClassFieldAccessorFactory.BASE_PACKAGE + "/" + Type.getInternalName( clazz ) + Math.abs( System.identityHashCode( clazz ) ) + "$" + setterMethod.getName();
// generating byte array to create target class
final byte[] bytes = dumpWriter( clazz,
className,
setterMethod,
fieldType,
clazz.isInterface() );
// use bytes to get a class
final Class< ? > newClass = byteArrayClassLoader.defineClass( className.replace( '/',
'.' ),
bytes,
PROTECTION_DOMAIN );
// instantiating target class
final Integer index = (Integer) inspector.getFieldNames().get( fieldName );
final ValueType valueType = ValueType.determineValueType( fieldType );
final Object[] params = {index, fieldType, valueType};
return (BaseClassFieldWriter) newClass.getConstructors()[0].newInstance( params );
} else {
throw new RuntimeDroolsException( "Field/method '" + fieldName + "' not found for class '" + clazz.getName() + "'" );