Search in sources :

Example 11 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class ASTHelper method createVariableDeclarationExpr.

/**
 * Creates a {@link VariableDeclarationExpr}.
 *
 * @param type
 *            type
 * @param name
 *            name
 * @return instance of {@link VariableDeclarationExpr}
 */
public static VariableDeclarationExpr createVariableDeclarationExpr(Type type, String name) {
    List<VariableDeclarator> vars = new ArrayList<VariableDeclarator>();
    vars.add(new VariableDeclarator(new VariableDeclaratorId(name)));
    return new VariableDeclarationExpr(type, vars);
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) VariableDeclaratorId(com.github.javaparser.ast.body.VariableDeclaratorId) ArrayList(java.util.ArrayList) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 12 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class VarValidator method accept.

@Override
public void accept(VarType node, ProblemReporter reporter) {
    // All allowed locations are within a VariableDeclaration inside a VariableDeclarationExpr inside something else.
    Optional<VariableDeclarator> variableDeclarator = node.findParent(VariableDeclarator.class);
    if (!variableDeclarator.isPresent()) {
        // Java 11's var in lambda's
        if (varAllowedInLambdaParameters) {
            boolean valid = node.findParent(Parameter.class).flatMap(Node::getParentNode).map((Node p) -> p instanceof LambdaExpr).orElse(false);
            if (valid) {
                return;
            }
        }
        reportIllegalPosition(node, reporter);
        return;
    }
    variableDeclarator.ifPresent(vd -> {
        Optional<Node> variableDeclarationExpr = vd.getParentNode();
        if (!variableDeclarationExpr.isPresent()) {
            reportIllegalPosition(node, reporter);
            return;
        }
        variableDeclarationExpr.ifPresent(vdeNode -> {
            if (!(vdeNode instanceof VariableDeclarationExpr)) {
                reportIllegalPosition(node, reporter);
                return;
            }
            VariableDeclarationExpr vde = (VariableDeclarationExpr) vdeNode;
            if (vde.getVariables().size() > 1) {
                reporter.report(vde, "\"var\" only takes a single variable.");
            }
            Optional<Node> container = vdeNode.getParentNode();
            if (!container.isPresent()) {
                reportIllegalPosition(node, reporter);
                return;
            }
            container.ifPresent(c -> {
                boolean positionIsFine = c instanceof ForStmt || c instanceof ForeachStmt || c instanceof ExpressionStmt;
                if (!positionIsFine) {
                    reportIllegalPosition(node, reporter);
                }
                // A local variable declaration ends up inside an ExpressionStmt.
                if (c instanceof ExpressionStmt) {
                    if (!vd.getInitializer().isPresent()) {
                        reporter.report(node, "\"var\" needs an initializer.");
                    }
                    vd.getInitializer().ifPresent(initializer -> {
                        if (initializer instanceof NullLiteralExpr) {
                            reporter.report(node, "\"var\" cannot infer type from just null.");
                        }
                        if (initializer instanceof ArrayCreationExpr) {
                            reporter.report(node, "\"var\" cannot infer array types.");
                        }
                    });
                }
            });
        });
    });
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Node(com.github.javaparser.ast.Node) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ForeachStmt(com.github.javaparser.ast.stmt.ForeachStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) ForStmt(com.github.javaparser.ast.stmt.ForStmt) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr)

Example 13 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class NodeWithVariables method getCommonType.

/**
 * Returns the type that is shared between all variables.
 * This is a shortcut for when you are certain that all variables share one type.
 * What makes this difficult is arrays, and being able to set the type.
 * <br/>For <code>int a;</code> this is int.
 * <br/>For <code>int a,b,c,d;</code> this is also int.
 * <br/>For <code>int a,b[],c;</code> this is an assertion error since b is an int[], not an int.
 * <br/>For <code>int a,b;</code>, then doing setType(String) on b, this is an assertion error. It is also a situation that you don't really want.
 */
default Type getCommonType() {
    NodeList<VariableDeclarator> variables = getVariables();
    if (variables.isEmpty()) {
        throw new AssertionError("There is no common type since there are no variables.");
    }
    Type type = variables.get(0).getType();
    for (int i = 1; i < variables.size(); i++) {
        if (!variables.get(i).getType().equals(type)) {
            throw new AssertionError("The variables do not have a common type.");
        }
    }
    return type;
}
Also used : ArrayType(com.github.javaparser.ast.type.ArrayType) Type(com.github.javaparser.ast.type.Type) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 14 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class GenericsResolutionTest method resolveFieldWithGenericTypeToString.

@Test
public void resolveFieldWithGenericTypeToString() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
    VariableDeclarator fieldS = Navigator.demandField(clazz, "s");
    SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
    Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("s", fieldS);
    assertEquals(true, symbolReference.isPresent());
    assertEquals("s", symbolReference.get().getName());
    ResolvedType type = symbolReference.get().getType();
    assertEquals(1, type.asReferenceType().typeParametersValues().size());
    assertEquals("java.lang.String", type.asReferenceType().typeParametersValues().get(0).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Value(com.github.javaparser.symbolsolver.model.resolution.Value) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 15 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class GenericsResolutionTest method resolveUsageOfGenericFieldIntermediateCase.

// PRIMA UN TEST CHE DICA CHE IL TIPO DEL CAMPO AS e' LIST<A> NON LIST<E>
@Test
public void resolveUsageOfGenericFieldIntermediateCase() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SomeCollection");
    VariableDeclarator field = Navigator.demandField(clazz, "as");
    ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(field);
    assertEquals(false, type.isTypeVariable());
    assertEquals("java.util.List<A>", type.describe());
    assertEquals(1, type.asReferenceType().typeParametersValues().size());
    assertEquals(true, type.asReferenceType().typeParametersValues().get(0).isTypeVariable());
    assertEquals("A", type.asReferenceType().typeParametersValues().get(0).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Aggregations

VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)110 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)50 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)44 Expression (com.github.javaparser.ast.expr.Expression)43 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)41 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)39 NameExpr (com.github.javaparser.ast.expr.NameExpr)33 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)32 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 Test (org.junit.Test)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)18 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)17 CompilationUnit (com.github.javaparser.ast.CompilationUnit)16 NodeList (com.github.javaparser.ast.NodeList)16 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)14 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)13 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)13 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)11 Parameter (com.github.javaparser.ast.body.Parameter)9