use of org.kie.api.definition.type.FactField in project drools by kiegroup.
the class TraitRegistry method bind.
private BitSet bind(String trait, String traitable) throws UnsupportedOperationException {
ClassDefinition traitDef = getTrait(trait);
if (traitDef == null) {
throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " : not a trait ");
}
ClassDefinition traitableDef = getTraitable(traitable);
if (traitableDef == null) {
throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " : not a traitable ");
}
int j = 0;
BitSet bitmask = new BitSet(traitDef.getFields().size());
for (FactField field : traitDef.getFields()) {
String alias = ((FieldDefinition) field).resolveAlias();
FieldDefinition concreteField = traitableDef.getFieldByAlias(alias);
if (concreteField != null) {
if (!traitableDef.isFullTraiting() && !concreteField.getType().isAssignableFrom(field.getType())) {
throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " :" + " trait field " + field.getName() + ":" + ((FieldDefinition) field).getTypeName() + " is incompatible with" + " concrete hard field " + concreteField.getName() + ":" + concreteField.getTypeName() + ". Consider enabling logical traiting" + " mode using @Traitable( logical = true )");
}
bitmask.set(j);
}
j++;
}
return bitmask;
}
use of org.kie.api.definition.type.FactField in project drools by kiegroup.
the class DefaultBeanClassBuilder method initializeDynamicTypeStructures.
/**
* Initializes the trait map and dynamic property map to empty values
* @param mv
* @param classDef
*/
protected void initializeDynamicTypeStructures(MethodVisitor mv, ClassDefinition classDef) {
if (classDef.isFullTraiting()) {
mv.visitVarInsn(ALOAD, 0);
mv.visitTypeInsn(NEW, Type.getInternalName(TraitFieldTMSImpl.class));
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TraitFieldTMSImpl.class), "<init>", "()V");
mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(classDef.getClassName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
for (FactField hardField : classDef.getFields()) {
FieldDefinition fld = (FieldDefinition) hardField;
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getClassName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(classDef.getClassName())));
mv.visitLdcInsn(fld.resolveAlias());
if (BuildUtils.isPrimitive(fld.getTypeName())) {
// mv.visitFieldInsn( GETSTATIC, BuildUtils.getInternalType( BuildUtils.box( fld.getTypeName() ) ), "TYPE", Type.getDescriptor( Class.class ) );
mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(BuildUtils.box(fld.getTypeName()))));
} else {
mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(fld.getTypeName())));
}
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getClassName()), BuildUtils.getterName(fld.getName(), fld.getTypeName()), "()" + BuildUtils.getTypeDescriptor(fld.getTypeName()));
if (BuildUtils.isPrimitive(fld.getTypeName())) {
mv.visitMethodInsn(INVOKESTATIC, BuildUtils.getInternalType(BuildUtils.box(fld.getTypeName())), "valueOf", "(" + BuildUtils.getTypeDescriptor(fld.getTypeName()) + ")" + BuildUtils.getTypeDescriptor(BuildUtils.box(fld.getTypeName())));
}
if (fld.getInitExpr() != null) {
mv.visitLdcInsn(fld.getInitExpr());
} else {
mv.visitInsn(ACONST_NULL);
}
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(TraitFieldTMS.class), "registerField", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(Class.class), Type.getType(String.class), Type.getType(Class.class), Type.getType(Object.class), Type.getType(String.class) }));
}
}
}
use of org.kie.api.definition.type.FactField in project drools by kiegroup.
the class TypeDeclarationFactory method compareTypeDeclarations.
protected int compareTypeDeclarations(TypeDeclaration oldDeclaration, TypeDeclaration newDeclaration) throws IncompatibleClassChangeError {
// different formats -> incompatible
if (!oldDeclaration.getFormat().equals(newDeclaration.getFormat())) {
throw new IncompatibleClassChangeError("Type Declaration " + newDeclaration.getTypeName() + " has a different" + " format that its previous definition: " + newDeclaration.getFormat() + "!=" + oldDeclaration.getFormat());
}
// different superclasses -> Incompatible (TODO: check for hierarchy)
if (!oldDeclaration.getTypeClassDef().getSuperClass().equals(newDeclaration.getTypeClassDef().getSuperClass())) {
if (oldDeclaration.getNature() == TypeDeclaration.Nature.DEFINITION && newDeclaration.getNature() == TypeDeclaration.Nature.DECLARATION && Object.class.getName().equals(newDeclaration.getTypeClassDef().getSuperClass())) {
// actually do nothing. The new declaration just recalls the previous definition, probably to extend it.
} else {
throw new IncompatibleClassChangeError("Type Declaration " + newDeclaration.getTypeName() + " has a different" + " superclass that its previous definition: " + newDeclaration.getTypeClassDef().getSuperClass() + " != " + oldDeclaration.getTypeClassDef().getSuperClass());
}
}
// different duration -> Incompatible
if (!nullSafeEqualityComparison(oldDeclaration.getDurationAttribute(), newDeclaration.getDurationAttribute())) {
throw new IncompatibleClassChangeError("Type Declaration " + newDeclaration.getTypeName() + " has a different" + " duration: " + newDeclaration.getDurationAttribute() + " != " + oldDeclaration.getDurationAttribute());
}
// //different masks -> incompatible
if (newDeclaration.getNature().equals(TypeDeclaration.Nature.DEFINITION)) {
if (oldDeclaration.getSetMask() != newDeclaration.getSetMask()) {
throw new IncompatibleClassChangeError("Type Declaration " + newDeclaration.getTypeName() + " is incompatible with" + " the previous definition: " + newDeclaration + " != " + oldDeclaration);
}
}
// TODO: further comparison?
// Field comparison
List<FactField> oldFields = oldDeclaration.getTypeClassDef().getFields();
Map<String, FactField> newFieldsMap = new HashMap<String, FactField>();
for (FactField factField : newDeclaration.getTypeClassDef().getFields()) {
newFieldsMap.put(factField.getName(), factField);
}
// each of the fields in the old definition that are also present in the
// new definition must have the same type. If not -> Incompatible
boolean allFieldsInOldDeclarationAreStillPresent = true;
for (FactField oldFactField : oldFields) {
FactField newFactField = newFieldsMap.get(oldFactField.getName());
if (newFactField != null) {
// we can't use newFactField.getType() since it throws a NPE at this point.
String newFactType = ((FieldDefinition) newFactField).getTypeName();
if (!newFactType.equals(((FieldDefinition) oldFactField).getTypeName())) {
throw new IncompatibleClassChangeError("Type Declaration " + newDeclaration.getTypeName() + "." + newFactField.getName() + " has a different" + " type that its previous definition: " + newFactType + " != " + oldFactField.getType().getCanonicalName());
}
} else {
allFieldsInOldDeclarationAreStillPresent = false;
}
}
// If the old declaration has less fields than the new declaration, oldDefinition < newDefinition
if (oldFields.size() < newFieldsMap.size()) {
return -1;
}
// If the old declaration has more fields than the new declaration, oldDefinition > newDefinition
if (oldFields.size() > newFieldsMap.size()) {
return 1;
}
// they are incompatible
if (allFieldsInOldDeclarationAreStillPresent) {
return 0;
}
// fields in the old declaration are present in the new declaration.
throw new IncompatibleClassChangeError(newDeclaration.getTypeName() + " introduces" + " fields that are not present in its previous version.");
}
use of org.kie.api.definition.type.FactField in project drools by kiegroup.
the class DynamicFact method entrySet.
public Set entrySet() {
Set set = new HashSet();
List<FactField> list = this.typeDef.getFields();
for (FactField factField : list) {
final FactField ff = factField;
Map.Entry<String, Object> ent = new Map.Entry<String, Object>() {
public String getKey() {
return ff.getName();
}
public Object getValue() {
return typeDef.get(bean, ff.getName());
}
public Object setValue(Object o) {
typeDef.set(bean, ff.getName(), o);
return o;
}
};
}
return set;
}
Aggregations