use of org.drools.core.base.ClassFieldAccessor in project drools by kiegroup.
the class TypeDeclarationConfigurator method buildFieldAccessors.
protected void buildFieldAccessors(final TypeDeclaration type, final PackageRegistry pkgRegistry) throws SecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
ClassDefinition cd = type.getTypeClassDef();
ClassFieldAccessorStore store = pkgRegistry.getPackage().getClassFieldAccessorStore();
for (FieldDefinition attrDef : cd.getFieldsDefinitions()) {
ClassFieldAccessor accessor = store.getAccessor(cd.getDefinedClass().getName(), attrDef.getName());
attrDef.setReadWriteAccessor(accessor);
}
}
use of org.drools.core.base.ClassFieldAccessor in project drools by kiegroup.
the class AbstractTraitFactory method buildClassDefinition.
public ClassDefinition buildClassDefinition(Class<?> klazz, Class<?> wrapperClass) throws IOException {
ClassFieldInspector inspector = new ClassFieldInspector(klazz);
ClassFieldAccessorStore store = getClassFieldAccessorStore();
ClassDefinition def;
if (!klazz.isInterface()) {
String className = wrapperClass.getName();
String superClass = wrapperClass != klazz ? klazz.getName() : klazz.getSuperclass().getName();
String[] interfaces = new String[klazz.getInterfaces().length + 1];
for (int j = 0; j < klazz.getInterfaces().length; j++) {
interfaces[j] = klazz.getInterfaces()[j].getName();
}
interfaces[interfaces.length - 1] = CoreWrapper.class.getName();
def = new ClassDefinition(className, superClass, interfaces);
def.setDefinedClass(wrapperClass);
Traitable tbl = wrapperClass.getAnnotation(Traitable.class);
def.setTraitable(true, tbl != null && tbl.logical());
Map<String, Field> fields = inspector.getFieldTypesField();
for (Field f : fields.values()) {
if (f != null) {
FieldDefinition fld = new FieldDefinition();
fld.setName(f.getName());
fld.setTypeName(f.getType().getName());
fld.setInherited(true);
ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
fld.setReadWriteAccessor(accessor);
if (inspector.getGetterMethods().containsKey(f.getName())) {
fld.setGetterName(inspector.getGetterMethods().get(f.getName()).getName());
}
if (inspector.getSetterMethods().containsKey(f.getName())) {
fld.setSetterName(inspector.getSetterMethods().get(f.getName()).getName());
}
def.addField(fld);
}
}
} else {
String className = klazz.getName();
String superClass = Object.class.getName();
String[] interfaces = new String[klazz.getInterfaces().length];
for (int j = 0; j < klazz.getInterfaces().length; j++) {
interfaces[j] = klazz.getInterfaces()[j].getName();
}
def = new ClassDefinition(className, superClass, interfaces);
def.setDefinedClass(klazz);
Map<String, Method> properties = inspector.getGetterMethods();
for (String key : properties.keySet()) {
Method m = properties.get(key);
if (m != null && m.getDeclaringClass() != TraitType.class && m.getDeclaringClass() != Thing.class && inspector.getSetterMethods().containsKey(key)) {
FieldDefinition fld = new FieldDefinition();
fld.setName(getterToFieldName(m.getName()));
fld.setTypeName(m.getReturnType().getName());
fld.setInherited(true);
ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
fld.setReadWriteAccessor(accessor);
fld.setGetterName(m.getName());
fld.setSetterName(inspector.getSetterMethods().get(key).getName());
def.addField(fld);
}
}
}
return def;
}
Aggregations