use of org.drools.javaparser.ast.type.Type in project drools by kiegroup.
the class ExpressionTyper method nameExpr.
private Optional<TypedExpressionCursor> nameExpr(Expression drlxExpr, NameExpr firstNode, boolean isInLineCast, Class<?> originalTypeCursor) {
Optional<TypedExpressionCursor> teCursor;
String firstName = firstNode.getName().getIdentifier();
Optional<DeclarationSpec> declarationById = ruleContext.getDeclarationById(firstName);
if (declarationById.isPresent()) {
// do NOT append any reactOnProperties.
// because reactOnProperties is referring only to the properties of the type of the pattern, not other declarations properites.
context.addUsedDeclarations(firstName);
final Class<?> typeCursor;
if (!isInLineCast) {
typeCursor = declarationById.get().getDeclarationClass();
} else {
typeCursor = originalTypeCursor;
}
teCursor = of(new TypedExpressionCursor(new NameExpr(firstName), typeCursor));
} else if (packageModel.getGlobals().containsKey(firstName)) {
context.addUsedDeclarations(firstName);
return of(new TypedExpressionCursor(new NameExpr(firstName), packageModel.getGlobals().get(firstName)));
} else {
final Class<?> typeCursor;
// In OOPath a declaration is based on a position rather than a name.
// Only an OOPath chunk can have a backreference expression
Optional<DeclarationSpec> backReference = Optional.empty();
if (firstNode.getBackReferencesCount() > 0) {
List<DeclarationSpec> ooPathDeclarations = ruleContext.getOOPathDeclarations();
DeclarationSpec backReferenceDeclaration = ooPathDeclarations.get(ooPathDeclarations.size() - 1 - firstNode.getBackReferencesCount());
typeCursor = backReferenceDeclaration.getDeclarationClass();
backReference = of(backReferenceDeclaration);
context.addUsedDeclarations(backReferenceDeclaration.getBindingId());
} else {
typeCursor = originalTypeCursor;
}
Method firstAccessor = ClassUtils.getAccessor((!isInLineCast) ? typeCursor : patternType, firstName);
if (firstAccessor != null) {
// Hack to review - if a property is upper case it's probably not a react on property
if (!"".equals(firstName) && Character.isLowerCase(firstName.charAt(0))) {
context.addReactOnProperties(firstName);
}
final Class<?> typeOfFirstAccessor;
if (!isInLineCast) {
typeOfFirstAccessor = firstAccessor.getReturnType();
} else {
typeOfFirstAccessor = typeCursor;
}
NameExpr thisAccessor = new NameExpr("_this");
final NameExpr scope = backReference.map(d -> new NameExpr(d.getBindingId())).orElse(thisAccessor);
teCursor = of(new TypedExpressionCursor(new MethodCallExpr(scope, firstAccessor.getName()), typeOfFirstAccessor));
} else {
try {
Class<?> resolvedType = ruleContext.getTypeResolver().resolveType(firstName);
return of(new TypedExpressionCursor(null, resolvedType));
} catch (ClassNotFoundException e) {
// ignore
}
final Optional<Node> rootNode = findRootNodeViaParent(drlxExpr);
rootNode.ifPresent(n -> {
// In the error messages HalfBinary are transformed to Binary
Node withHalfBinaryReplaced = replaceAllHalfBinaryChildren(n);
ruleContext.addCompilationError(new ParseExpressionErrorResult((Expression) withHalfBinaryReplaced));
});
teCursor = Optional.empty();
}
}
return teCursor;
}
Aggregations