use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.
the class ClassDefinitionFactory method wireAnnotationDefs.
protected boolean wireAnnotationDefs(AbstractClassTypeDeclarationDescr typeDescr, ClassDefinition def, TypeResolver resolver) {
for (AnnotationDescr annotationDescr : typeDescr.getAnnotations()) {
Class annotation;
try {
annotation = annotationDescr.getFullyQualifiedName() != null ? resolver.resolveType(annotationDescr.getFullyQualifiedName()) : null;
} catch (ClassNotFoundException e) {
continue;
}
if (annotation != null && annotation.isAnnotation()) {
try {
AnnotationDefinition annotationDefinition = AnnotationDefinition.build(annotation, annotationDescr.getValueMap(), resolver);
def.addAnnotation(annotationDefinition);
} catch (NoSuchMethodException nsme) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Annotated type " + typeDescr.getType().getFullName() + " - undefined property in @annotation " + annotationDescr.getName() + ": " + nsme.getMessage() + ";"));
}
}
if (annotation == null || annotation.getCanonicalName().startsWith("org.kie.api.definition.type")) {
def.addMetaData(annotationDescr.getName(), annotationDescr.getSingleValue());
}
}
return true;
}
use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.
the class ClassDefinitionFactory method wireAnnotationDefs.
protected boolean wireAnnotationDefs(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, ClassDefinition def, TypeResolver resolver) {
for (AnnotationDescr annotationDescr : typeDescr.getAnnotations()) {
Class annotation = null;
try {
annotation = annotationDescr.getFullyQualifiedName() != null ? resolver.resolveType(annotationDescr.getFullyQualifiedName()) : null;
} catch (ClassNotFoundException e) {
continue;
}
if (annotation != null && annotation.isAnnotation()) {
try {
AnnotationDefinition annotationDefinition = AnnotationDefinition.build(annotation, annotationDescr.getValueMap(), resolver);
def.addAnnotation(annotationDefinition);
} catch (NoSuchMethodException nsme) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Annotated type " + typeDescr.getType().getFullName() + " - undefined property in @annotation " + annotationDescr.getName() + ": " + nsme.getMessage() + ";"));
}
}
if (annotation == null || annotation.getCanonicalName().startsWith("org.kie.api.definition.type")) {
def.addMetaData(annotationDescr.getName(), annotationDescr.getSingleValue());
}
}
return true;
}
use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.
the class AnnotationsTest method testNestedAnnotations.
@Test
public void testNestedAnnotations() {
String drl = "package org.drools.test; " + "import " + Outer.class.getName().replace("$", ".") + "; " + "import " + Inner.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + " String() @Outer( value = @Inner( text = \"world\" ) ) " + "then " + "end ";
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
Map<String, AnnotationDefinition> defs = p.getAnnotations();
assertEquals(1, defs.size());
AnnotationDefinition outer = defs.get(Outer.class.getName().replace("$", "."));
assertNotNull(outer);
Object val = outer.getPropertyValue("value");
assertNotNull(val);
assertTrue(val instanceof AnnotationDefinition);
AnnotationDefinition inner = (AnnotationDefinition) val;
assertEquals("world", inner.getPropertyValue("text"));
}
use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.
the class AnnotationsTest method testNestedAnnotationsWithMultiplicity.
@Test
public void testNestedAnnotationsWithMultiplicity() {
String drl = "package org.drools.test; " + "import " + Outer.class.getName().replace("$", ".") + "; " + "import " + Inner.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + " String() @Outer( values = { @Inner( text = \"hello\" ), @Inner( text = \"world\" ) } ) " + "then " + "end ";
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
assertEquals(0, helper.verify().getMessages().size());
Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
Map<String, AnnotationDefinition> defs = p.getAnnotations();
assertEquals(1, defs.size());
AnnotationDefinition outer = defs.get(Outer.class.getName().replace("$", "."));
assertNotNull(outer);
Object val = outer.getPropertyValue("values");
assertNotNull(val);
assertTrue(val instanceof AnnotationDefinition[]);
}
use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.
the class TraitClassBuilderImpl method buildClass.
public byte[] buildClass(ClassDefinition classDef, ClassLoader classLoader) {
init(classDef);
ClassWriter cw = null;
try {
String cName = BuildUtils.getInternalType(classDef.getClassName());
String genericTypes = BuildUtils.getGenericTypes(classDef.getInterfaces());
String superType = Type.getInternalName(Object.class);
String[] intfaces = null;
if (Object.class.getName().equals(classDef.getSuperClass())) {
String[] tmp = BuildUtils.getInternalTypes(classDef.getInterfaces());
intfaces = new String[tmp.length + 2];
System.arraycopy(tmp, 0, intfaces, 0, tmp.length);
intfaces[tmp.length] = Type.getInternalName(Serializable.class);
intfaces[tmp.length + 1] = Type.getInternalName(GeneratedFact.class);
} else {
String[] tmp = BuildUtils.getInternalTypes(classDef.getInterfaces());
intfaces = new String[tmp.length + 3];
System.arraycopy(tmp, 0, intfaces, 0, tmp.length);
intfaces[tmp.length] = BuildUtils.getInternalType(classDef.getSuperClass());
intfaces[tmp.length + 1] = Type.getInternalName(Serializable.class);
intfaces[tmp.length + 2] = Type.getInternalName(GeneratedFact.class);
}
cw = createClassWriter(classLoader, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, cName, genericTypes, superType, intfaces);
{
if (classDef.getDefinedClass() == null || classDef.getDefinedClass().getAnnotation(Trait.class) == null) {
AnnotationVisitor av0 = cw.visitAnnotation(Type.getDescriptor(Trait.class), true);
for (AnnotationDefinition adef : classDef.getAnnotations()) {
if (Trait.class.getName().equals(adef.getName())) {
addAnnotationAttribute(adef, av0);
break;
}
}
av0.visitEnd();
}
}
for (FieldDefinition field : classDef.getFieldsDefinitions()) {
buildField(cw, field);
}
finalizeCreation(classDef);
cw.visitEnd();
} catch (Exception e) {
e.printStackTrace();
}
return cw.toByteArray();
}
Aggregations