DrlxParseUtil { public static java.lang.reflect.Type getExpressionType(RuleContext context, TypeResolver typeResolver, Expression expr, Collection<String> usedDeclarations) { if (expr instanceof LiteralExpr) { return getLiteralExpressionType( ( LiteralExpr ) expr ); } if (expr instanceof UnaryExpr) { return getExpressionType(context, typeResolver, expr.asUnaryExpr().getExpression(), usedDeclarations); } if (expr instanceof ArrayAccessExpr) { return getClassFromContext(typeResolver, ((ArrayCreationExpr)((ArrayAccessExpr) expr).getName()).getElementType().asString()); } if (expr instanceof ArrayCreationExpr) { return getClassFromContext(typeResolver, ((ArrayCreationExpr) expr).getElementType().asString()); } if (expr instanceof MapCreationLiteralExpression) { return Map.class; } if (expr instanceof NameExpr) { return expressionTypeNameExpr(context, usedDeclarations, ((NameExpr) expr).getNameAsString()); } if (expr instanceof DrlNameExpr) { return expressionTypeNameExpr(context, usedDeclarations, ((DrlNameExpr) expr).getNameAsString()); } if (expr instanceof BinaryExpr) { return boolean.class; } if (expr instanceof MethodCallExpr) { MethodCallExpr methodCallExpr = ( MethodCallExpr ) expr; Optional<Expression> scopeExpression = methodCallExpr.getScope(); if (scopeExpression.isPresent()) { java.lang.reflect.Type scopeType = getExpressionType(context, typeResolver, scopeExpression.get(), usedDeclarations); return returnTypeOfMethodCallExpr(context, typeResolver, methodCallExpr, scopeType, usedDeclarations); } else { throw new IllegalStateException("Scope expression is not present for " + ((MethodCallExpr) expr).getNameAsString() + "!"); } } if (expr instanceof ObjectCreationExpr) { final ClassOrInterfaceType type = ((ObjectCreationExpr) expr).getType(); return getClassFromContext (typeResolver, type.asString()); } if (expr.isCastExpr()) { String typeName = expr.asCastExpr().getType().toString(); try { return typeResolver.resolveType( expr.asCastExpr().getType().toString() ); } catch (ClassNotFoundException e) { context.addCompilationError( new InvalidExpressionErrorResult( "Unknown type in cast expression: " + typeName ) ); throw new RuntimeException( "Unknown type in cast expression: " + typeName ); } } if (expr instanceof ConditionalExpr) { ConditionalExpr ternaryExpr = (( ConditionalExpr ) expr); java.lang.reflect.Type conditionType = getExpressionType( context, typeResolver, ternaryExpr.getCondition(), usedDeclarations ); if (conditionType != Boolean.class && conditionType != boolean.class) { context.addCompilationError( new InvalidExpressionErrorResult( "Condtion used in ternary expression '" + expr + "' isn't boolean" ) ); return Object.class; } java.lang.reflect.Type leftType = getExpressionType( context, typeResolver, ternaryExpr.getThenExpr(), usedDeclarations ); java.lang.reflect.Type rightType = getExpressionType( context, typeResolver, ternaryExpr.getElseExpr(), usedDeclarations ); Class<?> leftClass = toRawClass( leftType ); Class<?> rightClass = toRawClass( rightType ); if (leftClass.isAssignableFrom( rightClass )) { return leftType; } if (rightClass.isAssignableFrom( leftClass )) { return rightType; } return Object.class; } throw new RuntimeException("Unknown expression type: " + PrintUtil.printConstraint(expr)); } private  DrlxParseUtil(); static boolean isThisExpression( Node expr ); static Index.ConstraintType toConstraintType( Operator operator); static TypedExpression nameExprToMethodCallExprWithCast(String name, java.lang.reflect.Type type, Expression scope, Type castType); static TypedExpression nameExprToMethodCallExpr(String name, java.lang.reflect.Type type, Expression scope); static java.lang.reflect.Type returnTypeOfMethodCallExpr(RuleContext context, TypeResolver typeResolver, MethodCallExpr methodCallExpr, java.lang.reflect.Type clazz, Collection<String> usedDeclarations); static java.lang.reflect.Type getExpressionType(RuleContext context, TypeResolver typeResolver, Expression expr, Collection<String> usedDeclarations); static boolean canCoerceLiteralNumberExpr(Class<?> type); static Class<?> getLiteralExpressionType( LiteralExpr expr ); static Expression prepend(Expression scope, Expression expr); static Optional<Node> findRootNodeViaParent(Node expr); static Node replaceAllHalfBinaryChildren(Node parent); static Expression trasformHalfBinaryToBinary(Expression drlxExpr); static MethodCallExpr findLastMethodInChain(MethodCallExpr expr); static RemoveRootNodeResult findRemoveRootNodeViaScope(Expression expr); static Optional<Expression> findRootNodeViaScope(Expression expr); static RemoveRootNodeResult removeRootNode(Expression expr); static String fromVar(String key); static BlockStmt parseBlock(String ruleConsequenceAsBlock); static Expression generateLambdaWithoutParameters(Collection<String> usedDeclarations, Expression expr); static Expression generateLambdaWithoutParameters(Expression expr); static Expression generateLambdaWithoutParameters(Collection<String> usedDeclarations,
                                                             Expression expr,
                                                             boolean skipFirstParamAsThis,
                                                             Optional<Class<?>> patternClass); static Expression generateLambdaWithoutParameters(Collection<String> usedDeclarations,
                                                             Expression expr,
                                                             boolean skipFirstParamAsThis,
                                                             Optional<Class<?>> patternClass,
                                                             RuleContext ruleContext); static Type classToReferenceType(Class<?> declClass); static Type classNameToReferenceType(String className); static Type toType(Class<?> declClass); static ClassOrInterfaceType toClassOrInterfaceType( Class<?> declClass ); static ClassOrInterfaceType toClassOrInterfaceType( String className ); static Optional<String> findBindingIdFromDotExpression(String expression); static Optional<Expression> findViaScopeWithPredicate(Expression expr, Predicate<Expression> predicate); static DrlxExpression parseExpression(String expression); static Class<?> getClassFromType(TypeResolver typeResolver, Type type); static Class<?> getClassFromContext(TypeResolver typeResolver, String className); static boolean isPrimitiveExpression(Expression expr); static void forceCastForName(String nameRef, Type type, Expression expression); static void rescopeNamesToNewScope(Expression newScope, List<String> names, Expression e); static List<String> getPatternListenedProperties( PatternDescr pattern); static Optional<MethodCallExpr> findLastPattern(List<Expression> expressions); static boolean isNameExprWithName(Node expression, String name); static List<Node> findAllChildrenRecursive(Expression e); static String toVar(String key); static Optional<InvalidExpressionErrorResult> validateDuplicateBindings(String ruleName, List<String> allBindings); static Method getAccessor( Class<?> clazz, String name ); static void clearAccessorCache(); static Field getField( Class<?> clazz, String name ); static T transformDrlNameExprToNameExpr(T e); static String addCurlyBracesToBlock(String blockString); static String addSemicolon(String block); static Expression uncastExpr(Expression e); static Collection<String> collectUsedDeclarationsInExpression(Expression expr); static Optional<java.lang.reflect.Type> safeResolveType(TypeResolver typeResolver, String typeName); static final String THIS_PLACEHOLDER; }
@Test public void getExpressionTypeTest() { assertEquals(Double.class, getExpressionType(null, typeResolver, StaticJavaParser.parseExpression("new Double[]{2.0d, 3.0d}[1]"), null)); assertEquals(Float.class, getExpressionType(null, typeResolver, StaticJavaParser.parseExpression("new Float[]{2.0d, 3.0d}"), null)); assertEquals(boolean.class, getExpressionType(null, typeResolver, new BooleanLiteralExpr(true), null)); assertEquals(char.class, getExpressionType(null, typeResolver, new CharLiteralExpr('a'), null)); assertEquals(double.class, getExpressionType(null, typeResolver, new DoubleLiteralExpr(2.0d), null)); assertEquals(int.class, getExpressionType(null, typeResolver, new IntegerLiteralExpr(2), null)); assertEquals(long.class, getExpressionType(null, typeResolver, new LongLiteralExpr(2l), null)); assertEquals(ClassUtil.NullType.class, getExpressionType(null, typeResolver, new NullLiteralExpr(), null)); assertEquals(String.class, getExpressionType(null, typeResolver, new StringLiteralExpr(""), null)); }