use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.
the class DefaultBeanClassBuilder method buildClassAnnotations.
protected void buildClassAnnotations(ClassDefinition classDef, ClassVisitor cw) {
for (AnnotationDefinition ad : classDef.getAnnotations()) {
AnnotationVisitor av = cw.visitAnnotation("L" + BuildUtils.getInternalType(ad.getName()) + ";", true);
addAnnotationAttribute(ad, av);
av.visitEnd();
}
}
use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.
the class DefaultBeanClassBuilder method addAnnotationAttribute.
public static void addAnnotationAttribute(AnnotationDefinition ad, AnnotationVisitor av) {
for (String key : ad.getValues().keySet()) {
AnnotationDefinition.AnnotationPropertyVal apv = ad.getValues().get(key);
switch(apv.getValType()) {
case STRINGARRAY:
AnnotationVisitor subAv = av.visitArray(apv.getProperty());
Object[] array = (Object[]) apv.getValue();
for (Object o : array) {
subAv.visit(null, o);
}
subAv.visitEnd();
break;
case PRIMARRAY:
av.visit(apv.getProperty(), apv.getValue());
break;
case ENUMARRAY:
AnnotationVisitor subEnav = av.visitArray(apv.getProperty());
Enum[] enArray = (Enum[]) apv.getValue();
String aenumType = "L" + BuildUtils.getInternalType(enArray[0].getClass().getName()) + ";";
for (Enum enumer : enArray) {
subEnav.visitEnum(null, aenumType, enumer.name());
}
subEnav.visitEnd();
break;
case CLASSARRAY:
AnnotationVisitor subKlav = av.visitArray(apv.getProperty());
Class[] klarray = (Class[]) apv.getValue();
for (Class klass : klarray) {
subKlav.visit(null, Type.getType("L" + BuildUtils.getInternalType(klass.getName()) + ";"));
}
subKlav.visitEnd();
break;
case ENUMERATION:
String enumType = "L" + BuildUtils.getInternalType(apv.getType().getName()) + ";";
av.visitEnum(apv.getProperty(), enumType, ((Enum) apv.getValue()).name());
break;
case KLASS:
String klassName = BuildUtils.getInternalType(((Class) apv.getValue()).getName());
av.visit(apv.getProperty(), Type.getType("L" + klassName + ";"));
break;
case PRIMITIVE:
av.visit(apv.getProperty(), apv.getValue());
break;
case STRING:
av.visit(apv.getProperty(), apv.getValue());
break;
}
}
}
use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.
the class AnnotationsOnPatternTest method testCollectAnnotationsParsingAndBuilding.
@Test
public void testCollectAnnotationsParsingAndBuilding() {
final String packageName = "org.drools.compiler.integrationtests";
final String drl = "package " + packageName + "; " + " " + "dialect 'mvel' " + " " + "import java.util.Collection; " + "import " + Inner.class.getCanonicalName() + "; " + " " + "rule \"test collect with annotation\" " + " when " + " Collection() from collect ( " + " String() @Inner " + " ) " + " then " + "end " + "";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("annotations-test", kieBaseTestConfiguration, drl);
final RuleImpl rule = (RuleImpl) kbase.getRule(packageName, "test collect with annotation");
final List<? extends RuleConditionElement> nested = ((Pattern) rule.getLhs().getChildren().get(0)).getSource().getNestedElements();
assertEquals(1, nested.size());
final Map<String, AnnotationDefinition> annotations = ((Pattern) nested.get(0)).getAnnotations();
assertEquals(1, annotations.size());
assertNotNull(annotations.keySet().iterator().next());
}
use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.
the class ModelGenerator method ruleMetaAttributes.
/**
* Build a list of method calls, representing each needed {@link org.drools.model.impl.RuleBuilder#metadata(String, Object)}
* starting from a drools-compiler {@link RuleDescr}.<br/>
* Based on {@link org.drools.modelcompiler.KiePackagesBuilder#setRuleMetaAttributes(Rule, RuleImpl)} the reserved annotation keywords are:
* Propagation, All, Direct.
*/
private static List<MethodCallExpr> ruleMetaAttributes(RuleContext context, RuleDescr ruleDescr) {
List<MethodCallExpr> ruleMetaAttributes = new ArrayList<>();
for (String metaAttr : ruleDescr.getAnnotationNames()) {
MethodCallExpr metaAttributeCall = new MethodCallExpr(METADATA_CALL);
metaAttributeCall.addArgument(toStringLiteral(metaAttr));
AnnotationDescr ad = ruleDescr.getAnnotation(metaAttr);
String adFqn = ad.getFullyQualifiedName();
if ("Propagation".equals(metaAttr)) {
// legacy case, as explained in the javadoc annotation above, ref. DROOLS-5685
metaAttributeCall.addArgument(parseExpression(org.kie.api.definition.rule.Propagation.Type.class.getCanonicalName() + "." + ad.getSingleValueAsString()));
} else if (adFqn != null) {
AnnotationDefinition annotationDefinition;
try {
annotationDefinition = AnnotationDefinition.build(context.getTypeResolver().resolveType(adFqn), ad.getValueMap(), context.getTypeResolver());
} catch (NoSuchMethodException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
if (annotationDefinition.getValues().size() == 1 && annotationDefinition.getValues().containsKey(AnnotationDescr.VALUE)) {
Object annValue = annotationDefinition.getPropertyValue(AnnotationDescr.VALUE);
metaAttributeCall.addArgument(toStringLiteral(annValue.toString()));
} else {
Map<String, Object> map = new HashMap<>(annotationDefinition.getValues().size());
for (String key : annotationDefinition.getValues().keySet()) {
map.put(key, annotationDefinition.getPropertyValue(key));
}
metaAttributeCall.addArgument(objectAsJPExpression(map));
}
} else {
if (ad.hasValue()) {
if (ad.getValueMap().size() == 1) {
metaAttributeCall.addArgument(annotationSingleValueExpression(ad));
} else {
metaAttributeCall.addArgument(objectAsJPExpression(ad.getValueMap()));
}
} else {
metaAttributeCall.addArgument(new NullLiteralExpr());
}
}
ruleMetaAttributes.add(metaAttributeCall);
}
return ruleMetaAttributes;
}
use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.
the class PatternBuilder method processMetadataAnnotations.
protected void processMetadataAnnotations(PatternDescr patternDescr, Pattern pattern, TypeResolver typeResolver) {
for (AnnotationDescr ann : patternDescr.getAnnotations()) {
String annFQN = ann.getFullyQualifiedName();
if (!Watch.class.getCanonicalName().equals(annFQN)) {
AnnotationDefinition def = buildAnnotationDef(ann, typeResolver);
pattern.getAnnotations().put(annFQN, def);
}
}
}
Aggregations