Search in sources :

Example 1 with IntegerLiteralExpr

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

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

the class ArrayCreationLevelTransformationsTest method replacingDimension.

@Test
public void replacingDimension() throws IOException {
    ArrayCreationLevel it = consider("[10]");
    it.setDimension(new IntegerLiteralExpr("12"));
    assertTransformedToString("[12]", it);
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) ArrayCreationLevel(com.github.javaparser.ast.ArrayCreationLevel) Test(org.junit.Test) AbstractLexicalPreservingTest(com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)

Example 3 with IntegerLiteralExpr

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

the class AnnotationMemberDeclarationTransformationsTest method replacingDefaultValue.

@Test
public void replacingDefaultValue() {
    AnnotationMemberDeclaration md = consider("int foo() default 10;");
    md.setDefaultValue(new IntegerLiteralExpr("11"));
    assertTransformedToString("int foo() default 11;", md);
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) AnnotationMemberDeclaration(com.github.javaparser.ast.body.AnnotationMemberDeclaration) Test(org.junit.Test) AbstractLexicalPreservingTest(com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)

Example 4 with IntegerLiteralExpr

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

the class NodeTest method stream.

@Test
public void stream() {
    Expression e = parseExpression("1+2+3");
    List<IntegerLiteralExpr> ints = e.stream().filter(n -> n instanceof IntegerLiteralExpr).map(IntegerLiteralExpr.class::cast).filter(i -> i.asInt() > 1).collect(Collectors.toList());
    assertEquals("[2, 3]", ints.toString());
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) SimpleName(com.github.javaparser.ast.expr.SimpleName) java.util(java.util) AstObserver(com.github.javaparser.ast.observer.AstObserver) EOL(com.github.javaparser.utils.Utils.EOL) Test(org.junit.Test) JavaParser.parse(com.github.javaparser.JavaParser.parse) Collectors(java.util.stream.Collectors) ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) LineComment(com.github.javaparser.ast.comments.LineComment) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) JavaParser.parseExpression(com.github.javaparser.JavaParser.parseExpression) AstObserverAdapter(com.github.javaparser.ast.observer.AstObserverAdapter) Expression(com.github.javaparser.ast.expr.Expression) Comment(com.github.javaparser.ast.comments.Comment) Assert(org.junit.Assert) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BlockComment(com.github.javaparser.ast.comments.BlockComment) 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 5 with IntegerLiteralExpr

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

the class TokenKindGenerator method generate.

@Override
public void generate() {
    Log.info("Running %s", getClass().getSimpleName());
    final CompilationUnit javaTokenCu = sourceRoot.parse("com.github.javaparser", "JavaToken.java");
    final ClassOrInterfaceDeclaration javaToken = javaTokenCu.getClassByName("JavaToken").orElseThrow(() -> new AssertionError("Can't find class in java file."));
    final EnumDeclaration kindEnum = javaToken.findFirst(EnumDeclaration.class, e -> e.getNameAsString().equals("Kind")).orElseThrow(() -> new AssertionError("Can't find class in java file."));
    kindEnum.getEntries().clear();
    annotateGenerated(kindEnum);
    final SwitchStmt valueOfSwitch = kindEnum.findFirst(SwitchStmt.class).orElseThrow(() -> new AssertionError("Can't find valueOf switch."));
    valueOfSwitch.findAll(SwitchEntryStmt.class).stream().filter(e -> e.getLabel().isPresent()).forEach(Node::remove);
    final CompilationUnit constantsCu = generatedJavaCcSourceRoot.parse("com.github.javaparser", "GeneratedJavaParserConstants.java");
    final ClassOrInterfaceDeclaration constants = constantsCu.getInterfaceByName("GeneratedJavaParserConstants").orElseThrow(() -> new AssertionError("Can't find class in java file."));
    for (BodyDeclaration<?> member : constants.getMembers()) {
        member.toFieldDeclaration().filter(field -> {
            String javadoc = field.getJavadocComment().get().getContent();
            return javadoc.contains("RegularExpression Id") || javadoc.contains("End of File");
        }).map(field -> field.getVariable(0)).ifPresent(var -> {
            final String name = var.getNameAsString();
            final IntegerLiteralExpr kind = var.getInitializer().get().asIntegerLiteralExpr();
            generateEnumEntry(kindEnum, name, kind);
            generateValueOfEntry(valueOfSwitch, name, kind);
        });
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) Generator(com.github.javaparser.generator.Generator) SourceRoot(com.github.javaparser.utils.SourceRoot) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) Log(com.github.javaparser.utils.Log) SwitchEntryStmt(com.github.javaparser.ast.stmt.SwitchEntryStmt) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) SwitchEntryStmt(com.github.javaparser.ast.stmt.SwitchEntryStmt) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration)

Aggregations

IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)16 Test (org.junit.Test)14 Expression (com.github.javaparser.ast.expr.Expression)9 JavaParser.parseExpression (com.github.javaparser.JavaParser.parseExpression)7 AbstractLexicalPreservingTest (com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)4 ArrayCreationLevel (com.github.javaparser.ast.ArrayCreationLevel)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)2 Node (com.github.javaparser.ast.Node)2 AnnotationMemberDeclaration (com.github.javaparser.ast.body.AnnotationMemberDeclaration)2 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)2 SimpleName (com.github.javaparser.ast.expr.SimpleName)2 JavaParser.parse (com.github.javaparser.JavaParser.parse)1 JavaParser.parseClassOrInterfaceType (com.github.javaparser.JavaParser.parseClassOrInterfaceType)1 JavaParser.parseName (com.github.javaparser.JavaParser.parseName)1 NodeList (com.github.javaparser.ast.NodeList)1 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)1 EnumConstantDeclaration (com.github.javaparser.ast.body.EnumConstantDeclaration)1 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1