Search in sources :

Example 1 with NullLiteralExpr

use of com.github.javaparser.ast.expr.NullLiteralExpr in project checker-framework by typetools.

the class StubParser method getValueOfExpressionInAnnotation.

/**
 * Returns the value of {@code expr}, or null if some problem occurred getting the value.
 */
private Object getValueOfExpressionInAnnotation(String name, Expression expr, TypeKind valueKind) {
    if (expr instanceof FieldAccessExpr || expr instanceof NameExpr) {
        VariableElement elem;
        if (expr instanceof NameExpr) {
            elem = findVariableElement((NameExpr) expr);
        } else {
            elem = findVariableElement((FieldAccessExpr) expr);
        }
        if (elem == null) {
            stubWarn("Field not found: " + expr);
            return null;
        }
        Object value = elem.getConstantValue() != null ? elem.getConstantValue() : elem;
        if (value instanceof Number) {
            return convert((Number) value, valueKind);
        } else {
            return value;
        }
    } else if (expr instanceof StringLiteralExpr) {
        return ((StringLiteralExpr) expr).asString();
    } else if (expr instanceof BooleanLiteralExpr) {
        return ((BooleanLiteralExpr) expr).getValue();
    } else if (expr instanceof CharLiteralExpr) {
        return convert((int) ((CharLiteralExpr) expr).asChar(), valueKind);
    } else if (expr instanceof DoubleLiteralExpr) {
        // double, too.
        return ((DoubleLiteralExpr) expr).asDouble();
    } else if (expr instanceof IntegerLiteralExpr) {
        return convert(((IntegerLiteralExpr) expr).asInt(), valueKind);
    } else if (expr instanceof LongLiteralExpr) {
        return convert(((LongLiteralExpr) expr).asLong(), valueKind);
    } else if (expr instanceof ClassExpr) {
        ClassExpr classExpr = (ClassExpr) expr;
        String className = classExpr.getType().toString();
        if (importedTypes.containsKey(className)) {
            return importedTypes.get(className).asType();
        }
        TypeElement typeElement = findTypeOfName(className);
        if (typeElement == null) {
            stubWarn("StubParser: unknown class name " + className);
            return null;
        }
        return typeElement.asType();
    } else if (expr instanceof NullLiteralExpr) {
        stubWarn("Null found as value for %s. Null isn't allowed as an annotation value", name);
        return null;
    } else {
        stubWarn("Unexpected annotation expression: " + expr);
        return null;
    }
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) TypeElement(javax.lang.model.element.TypeElement) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) VariableElement(javax.lang.model.element.VariableElement) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr)

Example 2 with NullLiteralExpr

use of com.github.javaparser.ast.expr.NullLiteralExpr 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 3 with NullLiteralExpr

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

the class TransformationsTest method example5.

@Test
public void example5() throws IOException {
    considerExample("Example5_original");
    cu.getClassByName("A").get().getFieldByName("a").get().getVariable(0).setInitializer(new NullLiteralExpr());
    assertTransformed("Example5", cu);
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Test(org.junit.Test)

Aggregations

NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)3 Node (com.github.javaparser.ast.Node)1 Parameter (com.github.javaparser.ast.body.Parameter)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 ArrayCreationExpr (com.github.javaparser.ast.expr.ArrayCreationExpr)1 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 CharLiteralExpr (com.github.javaparser.ast.expr.CharLiteralExpr)1 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)1 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)1 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)1 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)1 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)1 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)1 ForStmt (com.github.javaparser.ast.stmt.ForStmt)1 ForeachStmt (com.github.javaparser.ast.stmt.ForeachStmt)1 TypeElement (javax.lang.model.element.TypeElement)1