use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class KnowledgeBaseImpl method processTypeDeclaration.
protected void processTypeDeclaration(TypeDeclaration newDecl, InternalKnowledgePackage newPkg) throws ClassNotFoundException {
JavaDialectRuntimeData runtime = ((JavaDialectRuntimeData) newPkg.getDialectRuntimeRegistry().getDialectData("java"));
TypeDeclaration typeDeclaration = this.classTypeDeclaration.get(newDecl.getTypeClassName());
if (typeDeclaration == null) {
String className = newDecl.getTypeClassName();
byte[] def = runtime != null ? runtime.getClassDefinition(convertClassToResourcePath(className)) : null;
Class<?> definedKlass = registerAndLoadTypeDefinition(className, def);
if (definedKlass == null && newDecl.isNovel()) {
throw new RuntimeException("Registering null bytes for class " + className);
}
if (newDecl.getTypeClassDef() == null) {
newDecl.setTypeClassDef(new ClassDefinition());
}
newDecl.setTypeClass(definedKlass);
this.classTypeDeclaration.put(className, newDecl);
typeDeclaration = newDecl;
} else {
Class<?> definedKlass = typeDeclaration.getTypeClass();
newDecl.getTypeClassDef().setDefinedClass(definedKlass);
newDecl.setTypeClass(definedKlass);
mergeTypeDeclarations(typeDeclaration, newDecl);
}
// update existing OTNs
updateDependentTypes(typeDeclaration);
}
use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class AbstractTraitFactory method buildProxyClass.
protected Class<T> buildProxyClass(String key, K core, Class<?> trait) {
Class coreKlass = core.getClass();
// get the trait classDef
ClassDefinition tdef = getTraitRegistry().getTrait(trait.getName());
ClassDefinition cdef = getTraitRegistry().getTraitable(coreKlass.getName());
if (tdef == null) {
if (trait.getAnnotation(Trait.class) != null) {
try {
if (Thing.class.isAssignableFrom(trait)) {
tdef = buildClassDefinition(trait, null);
} else {
throw new RuntimeException("Unable to create definition for class " + trait + " : trait interfaces should extend " + Thing.class.getName() + " or be DECLARED as traits explicitly");
}
} catch (IOException e) {
throw new RuntimeException("Unable to create definition for class " + trait + " : " + e.getMessage());
}
getTraitRegistry().addTrait(tdef);
} else {
throw new RuntimeException("Unable to find Trait definition for class " + trait.getName() + ". It should have been DECLARED as a trait");
}
}
if (cdef == null) {
if (core.getClass().getAnnotation(Traitable.class) != null) {
try {
cdef = buildClassDefinition(core.getClass(), core.getClass());
} catch (IOException e) {
throw new RuntimeException("Unable to create definition for class " + coreKlass.getName() + " : " + e.getMessage());
}
getTraitRegistry().addTraitable(cdef);
} else {
throw new RuntimeException("Unable to find Core class definition for class " + coreKlass.getName() + ". It should have been DECLARED as a trait");
}
}
String proxyName = getProxyName(tdef, cdef);
String wrapperName = getPropertyWrapperName(tdef, cdef);
KieComponentFactory rcf = getComponentFactory();
TraitPropertyWrapperClassBuilder propWrapperBuilder = (TraitPropertyWrapperClassBuilder) rcf.getClassBuilderFactory().getPropertyWrapperBuilder();
propWrapperBuilder.init(tdef, getTraitRegistry());
try {
byte[] propWrapper = propWrapperBuilder.buildClass(cdef, getRootClassLoader());
registerAndLoadTypeDefinition(wrapperName, propWrapper);
} catch (Exception e) {
e.printStackTrace();
}
TraitProxyClassBuilder proxyBuilder = (TraitProxyClassBuilder) rcf.getClassBuilderFactory().getTraitProxyBuilder();
proxyBuilder.init(tdef, rcf.getBaseTraitProxyClass(), getTraitRegistry());
try {
byte[] proxy = proxyBuilder.buildClass(cdef, getRootClassLoader());
registerAndLoadTypeDefinition(proxyName, proxy);
} catch (Exception e) {
e.printStackTrace();
}
try {
BitSet mask = getTraitRegistry().getFieldMask(trait.getName(), cdef.getDefinedClass().getName());
Class<T> wrapperClass = (Class<T>) getRootClassLoader().loadClass(wrapperName);
Class<T> proxyClass = (Class<T>) getRootClassLoader().loadClass(proxyName);
return proxyClass;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class TraitRegistry method init.
private void init() {
TypeDeclaration thingType = new TypeDeclaration(Thing.class.getName());
thingType.setKind(TypeDeclaration.Kind.TRAIT);
thingType.setTypeClass(Thing.class);
ClassDefinition def = new ClassDefinition();
def.setClassName(thingType.getTypeClass().getName());
def.setDefinedClass(Thing.class);
addTrait(def);
ClassDefinition individualDef = new ClassDefinition();
individualDef.setClassName(Entity.class.getName());
individualDef.setDefinedClass(Entity.class);
individualDef.setInterfaces(new String[] { Serializable.class.getName(), TraitableBean.class.getName() });
individualDef.setTraitable(true);
addTraitable(individualDef);
ClassDefinition mapcoreDef = new ClassDefinition();
mapcoreDef.setClassName(MapCore.class.getName());
mapcoreDef.setDefinedClass(MapCore.class);
mapcoreDef.setInterfaces(new String[] { Serializable.class.getName(), TraitableBean.class.getName(), CoreWrapper.class.getName() });
mapcoreDef.setTraitable(true);
addTraitable(mapcoreDef);
ClassDefinition logicalMapcoreDef = new ClassDefinition();
logicalMapcoreDef.setClassName(LogicalMapCore.class.getName());
logicalMapcoreDef.setDefinedClass(LogicalMapCore.class);
logicalMapcoreDef.setInterfaces(new String[] { Serializable.class.getName(), TraitableBean.class.getName(), CoreWrapper.class.getName() });
logicalMapcoreDef.setTraitable(true, true);
addTraitable(logicalMapcoreDef);
}
use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class ClassDefinitionFactory method generateDeclaredBean.
/**
* Generates a bean, and adds it to the composite class loader that
* everything is using.
*/
public ClassDefinition generateDeclaredBean(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, PackageRegistry pkgRegistry, List<TypeDefinition> unresolvedTypeDefinitions, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
ClassDefinition def = createClassDefinition(typeDescr, type);
boolean success = true;
success &= wireAnnotationDefs(typeDescr, type, def, pkgRegistry.getTypeResolver());
success &= wireEnumLiteralDefs(typeDescr, type, def);
success &= wireFields(typeDescr, type, def, pkgRegistry, unresolvedTypeDefinitions);
if (!success) {
unprocesseableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
}
// attach the class definition, it will be completed later
type.setTypeClassDef(def);
return def;
}
use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class ClassDefinitionFactory method createClassDefinition.
protected ClassDefinition createClassDefinition(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
// extracts type, supertype and interfaces
String fullName = typeDescr.getType().getFullName();
if (type.getKind().equals(TypeDeclaration.Kind.CLASS)) {
TypeDeclarationDescr tdescr = (TypeDeclarationDescr) typeDescr;
if (tdescr.getSuperTypes().size() > 1) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Declared class " + fullName + " - has more than one supertype;"));
return null;
} else if (tdescr.getSuperTypes().isEmpty()) {
tdescr.addSuperType("java.lang.Object");
}
}
Traitable traitableAnn = typeDescr.getTypedAnnotation(Traitable.class);
boolean traitable = traitableAnn != null;
String[] fullSuperTypes = new String[typeDescr.getSuperTypes().size() + 1];
int j = 0;
for (QualifiedName qname : typeDescr.getSuperTypes()) {
fullSuperTypes[j++] = qname.getFullName();
}
fullSuperTypes[j] = Thing.class.getName();
List<String> interfaceList = new ArrayList<String>();
interfaceList.add(traitable ? Externalizable.class.getName() : Serializable.class.getName());
if (traitable) {
interfaceList.add(TraitableBean.class.getName());
}
String[] interfaces = interfaceList.toArray(new String[interfaceList.size()]);
// prepares a class definition
ClassDefinition def;
switch(type.getKind()) {
case TRAIT:
def = new ClassDefinition(fullName, Object.class.getName(), fullSuperTypes);
break;
case ENUM:
def = new EnumClassDefinition(fullName, fullSuperTypes[0], null);
break;
case CLASS:
default:
def = new ClassDefinition(fullName, fullSuperTypes[0], interfaces);
def.setTraitable(traitable, traitableAnn != null && traitableAnn.logical());
}
return def;
}
Aggregations