Search in sources :

Example 41 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class LambdaResolutionTest method typeOfVoidLambda.

@Test
public void typeOfVoidLambda() {
    CompilationUnit cu = parseSample("LambdaVoid");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    MethodDeclaration method = Navigator.demandMethod(clazz, "lambdaEmpty");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    Expression expression = returnStmt.getExpression().get();
    LambdaExpr lambdaExpr = Navigator.findNodeOfGivenClass(expression, LambdaExpr.class);
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
    ResolvedType type = javaParserFacade.getType(lambdaExpr);
    assertEquals("void", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 42 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class MethodsResolutionTest method solveMethodAccessThroughSuper.

@Test
public void solveMethodAccessThroughSuper() {
    CompilationUnit cu = parseSample("AccessThroughSuper");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "AccessThroughSuper.SubClass");
    MethodDeclaration method = Navigator.demandMethod(clazz, "methodTest");
    ReturnStmt returnStmt = (ReturnStmt) method.getBody().get().getStatements().get(0);
    Expression expression = returnStmt.getExpression().get();
    ResolvedType ref = JavaParserFacade.get(new ReflectionTypeSolver()).getType(expression);
    assertEquals("java.lang.String", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Expression(com.github.javaparser.ast.expr.Expression) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 43 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class ConstraintFormulaTest method testExpressionCompatibleWithTypeReduce1.

/**
 * From JLS 18.1.2
 *
 * From Collections.singleton("hi"), we have the constraint formula ‹"hi" → α›.
 * Through reduction, this will become the constraint formula: ‹String <: α›.
 */
@Test
public void testExpressionCompatibleWithTypeReduce1() {
    ResolvedTypeParameterDeclaration tp = mock(ResolvedTypeParameterDeclaration.class);
    Expression e = new StringLiteralExpr("hi");
    InferenceVariable inferenceVariable = new InferenceVariable("α", tp);
    ExpressionCompatibleWithType formula = new ExpressionCompatibleWithType(typeSolver, e, inferenceVariable);
    ConstraintFormula.ReductionResult res1 = formula.reduce(BoundSet.empty());
    assertEquals(ConstraintFormula.ReductionResult.empty().withConstraint(new TypeCompatibleWithType(typeSolver, stringType, inferenceVariable)), res1);
    assertEquals(ConstraintFormula.ReductionResult.empty().withConstraint(new TypeSubtypeOfType(typeSolver, stringType, inferenceVariable)), res1.getConstraint(0).reduce(BoundSet.empty()));
}
Also used : InferenceVariable(com.github.javaparser.symbolsolver.resolution.typeinference.InferenceVariable) Expression(com.github.javaparser.ast.expr.Expression) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ConstraintFormula(com.github.javaparser.symbolsolver.resolution.typeinference.ConstraintFormula) Test(org.junit.Test)

Example 44 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class SymbolResolutionResolutionTest method conditionalExpressionExample.

@Test
public void conditionalExpressionExample() {
    CompilationUnit cu = parseSample("JreConditionalExpression");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo1");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    Expression expression = returnStmt.getExpression().get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(expression);
    assertEquals("java.lang.String", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 45 with Expression

use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.

the class ASTCompilerVisitor method visit.

@Override
public DirectCompilerResult visit(UnaryTestNode n) {
    DirectCompilerResult value = n.getValue().accept(this);
    Expression expr = Expressions.unary(n.getOperator(), value.getExpression());
    Expressions.NamedLambda namedLambda = Expressions.namedUnaryLambda(expr, n.getText());
    DirectCompilerResult r = DirectCompilerResult.of(namedLambda.name(), BuiltInType.UNARY_TEST).withFD(value);
    r.addFieldDeclaration(namedLambda.field());
    return r;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)429 Test (org.junit.Test)166 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)149 NameExpr (com.github.javaparser.ast.expr.NameExpr)135 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)94 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)90 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)84 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)73 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)71 NodeList (com.github.javaparser.ast.NodeList)66 DrlxParser.parseExpression (org.drools.mvel.parser.DrlxParser.parseExpression)61 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)53 CompilationUnit (com.github.javaparser.ast.CompilationUnit)51 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)50 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)49 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)49 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)46 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)44 List (java.util.List)44 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)43