Search in sources :

Example 21 with Expression

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

the class MethodCallExprContext method solveMethodAsUsage.

@Override
public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver) {
    if (wrappedNode.getScope().isPresent()) {
        Expression scope = wrappedNode.getScope().get();
        // Consider static method calls
        if (scope instanceof NameExpr) {
            String className = ((NameExpr) scope).getName().getId();
            SymbolReference<ResolvedTypeDeclaration> ref = solveType(className, typeSolver);
            if (ref.isSolved()) {
                SymbolReference<ResolvedMethodDeclaration> m = MethodResolutionLogic.solveMethodInType(ref.getCorrespondingDeclaration(), name, argumentsTypes, typeSolver);
                if (m.isSolved()) {
                    MethodUsage methodUsage = new MethodUsage(m.getCorrespondingDeclaration());
                    methodUsage = resolveMethodTypeParametersFromExplicitList(typeSolver, methodUsage);
                    methodUsage = resolveMethodTypeParameters(methodUsage, argumentsTypes);
                    return Optional.of(methodUsage);
                } else {
                    throw new UnsolvedSymbolException(ref.getCorrespondingDeclaration().toString(), "Method '" + name + "' with parameterTypes " + argumentsTypes);
                }
            }
        }
        ResolvedType typeOfScope = JavaParserFacade.get(typeSolver).getType(scope);
        // we can replace the parameter types from the scope into the typeParametersValues
        Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>();
        for (int i = 0; i < argumentsTypes.size(); i++) {
            // by replacing types I can also find new equivalences
            // for example if I replace T=U with String because I know that T=String I can derive that also U equal String
            ResolvedType originalArgumentType = argumentsTypes.get(i);
            ResolvedType updatedArgumentType = usingParameterTypesFromScope(typeOfScope, originalArgumentType, inferredTypes);
            argumentsTypes.set(i, updatedArgumentType);
        }
        for (int i = 0; i < argumentsTypes.size(); i++) {
            ResolvedType updatedArgumentType = applyInferredTypes(argumentsTypes.get(i), inferredTypes);
            argumentsTypes.set(i, updatedArgumentType);
        }
        return solveMethodAsUsage(typeOfScope, name, argumentsTypes, typeSolver, this);
    } else {
        Context parentContext = getParent();
        while (parentContext instanceof MethodCallExprContext) {
            parentContext = parentContext.getParent();
        }
        return parentContext.solveMethodAsUsage(name, argumentsTypes, typeSolver);
    }
}
Also used : UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) Context(com.github.javaparser.symbolsolver.core.resolution.Context) HashMap(java.util.HashMap) NameExpr(com.github.javaparser.ast.expr.NameExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodUsage(com.github.javaparser.resolution.MethodUsage)

Example 22 with Expression

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

the class JsonPrinterTest method testWithoutType.

@Test
public void testWithoutType() {
    JsonPrinter jsonPrinter = new JsonPrinter(false);
    Expression expression = parseExpression("1+1");
    String output = jsonPrinter.output(expression);
    assertEquals("{\"operator\":\"PLUS\",\"left\":{\"value\":\"1\"},\"right\":{\"value\":\"1\"}}", output);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) Test(org.junit.Test)

Example 23 with Expression

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

the class BlockStmtTest method issue748AddingIdenticalStatementsDoesParentingRight.

@Test
public void issue748AddingIdenticalStatementsDoesParentingRight() {
    BlockStmt blockStmt = new BlockStmt();
    Expression exp = new NameExpr("x");
    MethodCallExpr expression = new MethodCallExpr(exp, "y");
    blockStmt.addStatement(expression);
    blockStmt.addStatement(expression.clone());
    // This fails when the issue exists:
    String s = blockStmt.toString();
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 24 with Expression

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

the class ModifierVisitorTest method binaryExprReturnsRightExpressionWhenLeftSideIsRemoved.

@Test
public void binaryExprReturnsRightExpressionWhenLeftSideIsRemoved() {
    final Expression expression = parseExpression("1+2");
    final Visitable result = expression.accept(new ModifierVisitor<Void>() {

        public Visitable visit(IntegerLiteralExpr integerLiteralExpr, Void arg) {
            if (integerLiteralExpr.getValue().equals("2")) {
                return null;
            }
            return integerLiteralExpr;
        }
    }, null);
    assertEquals("1", result.toString());
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) JavaParser.parseExpression(com.github.javaparser.JavaParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) Test(org.junit.Test)

Example 25 with Expression

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

the class AnnotationMemberDeclarationTest method removeDefaultValueWhenDefaultValueIsPresent.

@Test
public void removeDefaultValueWhenDefaultValueIsPresent() {
    AnnotationMemberDeclaration decl = new AnnotationMemberDeclaration();
    SimpleName name = new SimpleName("foo");
    decl.setName(name);
    Expression defaultValue = new IntegerLiteralExpr("2");
    decl.setDefaultValue(defaultValue);
    decl.removeDefaultValue();
    assertFalse(defaultValue.getParentNode().isPresent());
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) SimpleName(com.github.javaparser.ast.expr.SimpleName) Test(org.junit.Test)

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