Search in sources :

Example 11 with AnnotationDefinition

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;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 12 with AnnotationDefinition

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;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 13 with AnnotationDefinition

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"));
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 14 with AnnotationDefinition

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[]);
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 15 with 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();
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Serializable(java.io.Serializable) FieldDefinition(org.drools.core.factmodel.FieldDefinition) AnnotationVisitor(org.mvel2.asm.AnnotationVisitor) ClassGenerator.createClassWriter(org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter) ClassWriter(org.mvel2.asm.ClassWriter) GeneratedFact(org.drools.core.factmodel.GeneratedFact)

Aggregations

AnnotationDefinition (org.drools.core.factmodel.AnnotationDefinition)23 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 Pattern (org.drools.core.rule.Pattern)10 Test (org.junit.Test)10 AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)5 KieBase (org.kie.api.KieBase)5 AnnotationVisitor (org.mvel2.asm.AnnotationVisitor)5 KieHelper (org.kie.internal.utils.KieHelper)4 IOException (java.io.IOException)3 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)3 FieldDefinition (org.drools.core.factmodel.FieldDefinition)3 Trait (org.drools.core.factmodel.traits.Trait)3 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ClassDefinition (org.drools.core.factmodel.ClassDefinition)2 GeneratedFact (org.drools.core.factmodel.GeneratedFact)2 ClassWriter (org.mvel2.asm.ClassWriter)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1