use of org.drools.drl.ast.descr.AnnotationDescr 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.drl.ast.descr.AnnotationDescr 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.drl.ast.descr.AnnotationDescr in project drools by kiegroup.
the class DescrTypeDefinition method processClassAnnotations.
private void processClassAnnotations() {
for (AnnotationDescr ann : typeDeclarationDescr.getAnnotations()) {
if (ann.getName().equals(SERIAL_VERSION_UID)) {
DescrFieldDefinition serialVersionField = new DescrFieldDefinition(SERIAL_VERSION_UID, "long", ann.getValue("value").toString()).setFinal(true).setStatic(true);
fieldDefinition.add(serialVersionField);
}
try {
annotations.add(DescrAnnotationDefinition.fromDescr(typeResolver, ann));
} catch (UnkownAnnotationClassException | UnknownKeysInAnnotation e) {
// Do not do anything
}
}
}
use of org.drools.drl.ast.descr.AnnotationDescr in project drools by kiegroup.
the class DescrTypeDefinition method typeFieldsSortedByPosition.
private List<TypeFieldDescr> typeFieldsSortedByPosition(List<FieldDefinition> inheritedFields) {
Collection<TypeFieldDescr> typeFields = typeDeclarationDescr.getFields().values().stream().filter(f -> inheritedFields.stream().map(FieldDefinition::getFieldName).noneMatch(name -> name.equals(f.getFieldName()))).collect(Collectors.toList());
TypeFieldDescr[] sortedTypes = new TypeFieldDescr[typeFields.size()];
List<TypeFieldDescr> nonPositionalFields = new ArrayList<>();
for (TypeFieldDescr descr : typeFields) {
AnnotationDescr ann = descr.getAnnotation("Position");
if (ann == null) {
nonPositionalFields.add(descr);
} else {
int pos = Integer.parseInt(ann.getValue().toString());
if (pos >= sortedTypes.length) {
errors.add(new TypeDeclarationError(typeDeclarationDescr, "Out of range position " + pos + " for field '" + descr.getFieldName() + "' on class " + typeDeclarationDescr.getTypeName()));
} else if (sortedTypes[pos] != null) {
errors.add(new TypeDeclarationError(typeDeclarationDescr, "Duplicated position " + pos + " for field '" + descr.getFieldName() + "' on class " + typeDeclarationDescr.getTypeName()));
} else {
sortedTypes[pos] = descr;
}
}
}
if (!errors.isEmpty()) {
return Collections.emptyList();
}
int counter = 0;
for (TypeFieldDescr descr : nonPositionalFields) {
for (; sortedTypes[counter] != null; counter++) ;
sortedTypes[counter++] = descr;
}
return Arrays.asList(sortedTypes);
}
use of org.drools.drl.ast.descr.AnnotationDescr in project drools by kiegroup.
the class ClassDefinitionFactory method sortFields.
private static List<FieldDefinition> sortFields(Map<String, TypeFieldDescr> fields, TypeResolver typeResolver, KnowledgeBuilderImpl kbuilder) {
List<FieldDefinition> fieldDefs = new ArrayList<>(fields.size());
int maxDeclaredPos = 0;
BitSet occupiedPositions = new BitSet(fields.size());
for (TypeFieldDescr field : fields.values()) {
GenericTypeDefinition genericType = field.getPattern().getGenericType().map(type -> TypeDeclarationUtils.toBuildableType(type, kbuilder != null ? kbuilder.getRootClassLoader() : null));
FieldDefinition fieldDef = new FieldDefinition(field.getFieldName(), genericType);
fieldDefs.add(fieldDef);
if (field.hasOverride()) {
fieldDef.setOverriding(field.getOverriding().getPattern().getObjectType());
}
fieldDef.setInherited(field.isInherited());
fieldDef.setRecursive(field.isRecursive());
fieldDef.setInitExpr(TypeDeclarationUtils.rewriteInitExprWithImports(field.getInitExpr(), typeResolver));
if (field.getIndex() >= 0) {
int pos = field.getIndex();
occupiedPositions.set(pos);
maxDeclaredPos = Math.max(maxDeclaredPos, pos);
fieldDef.addMetaData("position", pos);
} else {
Position position = getTypedAnnotation(field, Position.class);
if (position != null) {
int pos = position.value();
field.setIndex(pos);
occupiedPositions.set(pos);
maxDeclaredPos = Math.max(maxDeclaredPos, pos);
fieldDef.addMetaData("position", pos);
}
}
if (field.hasAnnotation(Key.class)) {
fieldDef.setKey(true);
fieldDef.addMetaData("key", null);
}
for (AnnotationDescr annotationDescr : field.getAnnotations()) {
if (annotationDescr.getFullyQualifiedName() == null) {
if (annotationDescr.isStrict()) {
kbuilder.addBuilderResult(new TypeDeclarationError(field, "Unknown annotation @" + annotationDescr.getName() + " on field " + field.getFieldName()));
} else {
// Annotation is custom metadata
fieldDef.addMetaData(annotationDescr.getName(), annotationDescr.getSingleValue());
continue;
}
}
Annotation annotation = AnnotationFactory.buildAnnotation(typeResolver, annotationDescr);
if (annotation != null) {
try {
AnnotationDefinition annotationDefinition = AnnotationDefinition.build(annotation.annotationType(), field.getAnnotation(annotationDescr.getFullyQualifiedName()).getValueMap(), typeResolver);
fieldDef.addAnnotation(annotationDefinition);
} catch (Exception e) {
kbuilder.addBuilderResult(new TypeDeclarationError(field, "Annotated field " + field.getFieldName() + " - undefined property in @annotation " + annotationDescr.getName() + ": " + e.getMessage() + ";"));
}
} else {
if (annotationDescr.isStrict()) {
kbuilder.addBuilderResult(new TypeDeclarationError(field, "Unknown annotation @" + annotationDescr.getName() + " on field " + field.getFieldName()));
}
}
}
fieldDef.setDeclIndex(field.getIndex());
}
int curr = 0;
for (FieldDefinition fieldDef : fieldDefs) {
if (fieldDef.getDeclIndex() < 0) {
int freePos = occupiedPositions.nextClearBit(0);
if (freePos < maxDeclaredPos) {
occupiedPositions.set(freePos);
} else {
freePos = maxDeclaredPos + 1;
}
fieldDef.setPriority(freePos * 256 + curr++);
} else {
fieldDef.setPriority(fieldDef.getDeclIndex() * 256 + curr++);
}
}
Collections.sort(fieldDefs);
return fieldDefs;
}
Aggregations