Search in sources :

Example 21 with AnnotationDefinition

use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.

the class AnnotationsOnPatternTest method testNestedAnnotationsWithMultiplicity.

@Test
public void testNestedAnnotationsWithMultiplicity() {
    final 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 ";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("annotations-test", kieBaseTestConfiguration, drl);
    final Pattern p = ((Pattern) ((RuleImpl) kbase.getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
    final Map<String, AnnotationDefinition> defs = p.getAnnotations();
    assertEquals(1, defs.size());
    final AnnotationDefinition outer = defs.get(Outer.class.getName().replace("$", "."));
    assertNotNull(outer);
    final 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) KieBase(org.kie.api.KieBase) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 22 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);
    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);
    }
    ClassWriter 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();
    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) Trait(org.drools.core.factmodel.traits.Trait) GeneratedFact(org.drools.core.factmodel.GeneratedFact) ClassGenerator.createClassWriter(org.drools.mvel.asm.ClassGenerator.createClassWriter) ClassWriter(org.mvel2.asm.ClassWriter)

Example 23 with AnnotationDefinition

use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.

the class DefaultBeanClassBuilder method buildFieldAnnotations.

protected void buildFieldAnnotations(FieldDefinition fieldDef, FieldVisitor fv) {
    if (fieldDef.getAnnotations() != null) {
        for (AnnotationDefinition ad : fieldDef.getAnnotations()) {
            AnnotationVisitor av = fv.visitAnnotation("L" + BuildUtils.getInternalType(ad.getName()) + ";", true);
            addAnnotationAttribute(ad, av);
            av.visitEnd();
        }
    }
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) AnnotationVisitor(org.mvel2.asm.AnnotationVisitor)

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