use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class ClassDefinitionFactory method populateDefinitionFromClass.
public static void populateDefinitionFromClass(ClassDefinition def, Resource resource, Class<?> concrete, boolean asTrait) {
try {
def.setClassName(concrete.getName());
if (concrete.getSuperclass() != null) {
def.setSuperClass(concrete.getSuperclass().getName());
}
ClassFieldInspector inspector = new ClassFieldInspector(concrete);
Map<String, Method> methods = inspector.getGetterMethods();
Map<String, Method> setters = inspector.getSetterMethods();
int j = 0;
Map<String, TypeFieldDescr> fields = new HashMap<String, TypeFieldDescr>();
for (String fieldName : methods.keySet()) {
if (asTrait && ("core".equals(fieldName) || "fields".equals(fieldName))) {
continue;
}
if (!inspector.isNonGetter(fieldName) && setters.keySet().contains(fieldName)) {
Position position = null;
if (!concrete.isInterface()) {
try {
Field fld = concrete.getDeclaredField(fieldName);
position = fld.getAnnotation(Position.class);
} catch (NoSuchFieldException nsfe) {
// @Position can only annotate fields. This x means that a getter/setter pair was found with no field
}
}
Class ret = methods.get(fieldName).getReturnType();
TypeFieldDescr field = new TypeFieldDescr();
field.setResource(resource);
field.setFieldName(fieldName);
field.setPattern(new PatternDescr(ret.getName()));
field.setIndex(position != null ? position.value() : -1);
fields.put(fieldName, field);
}
}
if (!fields.isEmpty()) {
List<FieldDefinition> fieldDefs = sortFields(fields, null, null);
int i = 0;
for (FieldDefinition fieldDef : fieldDefs) {
fieldDef.setIndex(i++);
def.addField(fieldDef);
}
}
Set<String> interfaces = new HashSet<String>();
Collections.addAll(interfaces, def.getInterfaces());
for (Class iKlass : ClassUtils.getAllImplementedInterfaceNames(concrete)) {
interfaces.add(iKlass.getName());
}
def.setInterfaces(interfaces.toArray(new String[interfaces.size()]));
def.setDefinedClass(concrete);
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class ClassHierarchyManager method buildDescrsFromFields.
private static void buildDescrsFromFields(Class klass, TypeDeclarationDescr typeDescr, PackageRegistry pkgRegistry, Map<String, TypeFieldDescr> fieldMap) {
ClassFieldInspector inspector = null;
try {
inspector = new ClassFieldInspector(klass);
} catch (IOException e) {
throw new RuntimeException(e);
}
for (String name : inspector.getGetterMethods().keySet()) {
// classFieldAccessor requires both getter and setter
if (inspector.getSetterMethods().containsKey(name)) {
if (!inspector.isNonGetter(name) && !"class".equals(name)) {
Resource resource = typeDescr.getResource();
PatternDescr patternDescr = new PatternDescr(inspector.getFieldType(name).getName());
patternDescr.setResource(resource);
TypeFieldDescr inheritedFlDescr = new TypeFieldDescr(name, patternDescr);
inheritedFlDescr.setResource(resource);
inheritedFlDescr.setInherited(!Modifier.isAbstract(inspector.getGetterMethods().get(name).getModifiers()));
if (!fieldMap.containsKey(inheritedFlDescr.getFieldName()))
fieldMap.put(inheritedFlDescr.getFieldName(), inheritedFlDescr);
}
}
}
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class ClassHierarchyManager method buildInheritedFieldDescrFromDefinition.
protected TypeFieldDescr buildInheritedFieldDescrFromDefinition(org.kie.api.definition.type.FactField fld, TypeDeclarationDescr typeDescr) {
TypeFieldDescr inheritedFldDescr = new TypeFieldDescr();
inheritedFldDescr.setFieldName(fld.getName());
inheritedFldDescr.setResource(typeDescr.getResource());
PatternDescr fldType = new PatternDescr();
fldType.setObjectType(((FieldDefinition) fld).getTypeName());
// also sets resource for PatternDescr fldType
inheritedFldDescr.setPattern(fldType);
if (fld.isKey()) {
AnnotationDescr keyAnnotation = new AnnotationDescr(Key.class.getCanonicalName());
keyAnnotation.setFullyQualifiedName(Key.class.getCanonicalName());
keyAnnotation.setResource(typeDescr.getResource());
inheritedFldDescr.addAnnotation(keyAnnotation);
}
inheritedFldDescr.setIndex(((FieldDefinition) fld).getDeclIndex());
inheritedFldDescr.setInherited(true);
String initExprOverride = ((FieldDefinition) fld).getInitExpr();
int overrideCount = 0;
// only @aliasing local fields may override defaults.
for (TypeFieldDescr localField : typeDescr.getFields().values()) {
Alias alias = localField.getTypedAnnotation(Alias.class);
if (alias != null && fld.getName().equals(alias.value().replaceAll("\"", "")) && localField.getInitExpr() != null) {
overrideCount++;
initExprOverride = localField.getInitExpr();
}
}
if (overrideCount > 1) {
// however, only one is allowed
initExprOverride = null;
}
inheritedFldDescr.setInitExpr(initExprOverride);
return inheritedFldDescr;
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class PatternHandler method start.
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
parser.startElementBuilder(localName, attrs);
final String objectType = attrs.getValue("object-type");
if (objectType == null || objectType.trim().equals("")) {
throw new SAXParseException("<pattern> requires an 'object-type' attribute", parser.getLocator());
}
PatternDescr patternDescr = null;
final String identifier = attrs.getValue("identifier");
if (identifier == null || identifier.trim().equals("")) {
patternDescr = new PatternDescr(objectType);
} else {
patternDescr = new PatternDescr(objectType, identifier);
}
return patternDescr;
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class PatternHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
final PatternDescr patternDescr = (PatternDescr) parser.getCurrent();
final Object parent = parser.getParent();
if (parent instanceof PatternDestinationDescr) {
final PatternDestinationDescr parentDescr = (PatternDestinationDescr) parent;
parentDescr.setInputPattern(patternDescr);
} else {
final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parent;
parentDescr.addDescr(patternDescr);
}
return patternDescr;
}
Aggregations