Search in sources :

Example 56 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitClassMembers.

/**
 * Given a list of class members for javac and JavaParser, visits each javac member with its
 * corresponding JavaParser member. Skips synthetic javac members.
 *
 * @param javacMembers a list of trees forming the members of a javac {@code ClassTree}
 * @param javaParserMembers a list of nodes forming the members of a JavaParser {@code
 *     ClassOrInterfaceDeclaration} or an {@code ObjectCreationExpr} with an anonymous class body
 *     that corresponds to {@code javacMembers}
 */
private void visitClassMembers(List<? extends Tree> javacMembers, List<BodyDeclaration<?>> javaParserMembers) {
    PeekingIterator<Tree> javacIter = Iterators.peekingIterator(javacMembers.iterator());
    PeekingIterator<BodyDeclaration<?>> javaParserIter = Iterators.peekingIterator(javaParserMembers.iterator());
    while (javacIter.hasNext() || javaParserIter.hasNext()) {
        // Skip javac's synthetic no-argument constructors.
        if (javacIter.hasNext() && isNoArgumentConstructor(javacIter.peek()) && (!javaParserIter.hasNext() || !isNoArgumentConstructor(javaParserIter.peek()))) {
            javacIter.next();
            continue;
        }
        // VariableDeclarators. Match the declarators with the VariableTrees.
        if (javaParserIter.hasNext() && javaParserIter.peek().isFieldDeclaration()) {
            for (VariableDeclarator decl : javaParserIter.next().asFieldDeclaration().getVariables()) {
                assert javacIter.hasNext();
                assert javacIter.peek().getKind() == Tree.Kind.VARIABLE;
                javacIter.next().accept(this, decl);
            }
            continue;
        }
        assert javacIter.hasNext();
        assert javaParserIter.hasNext();
        javacIter.next().accept(this, javaParserIter.next());
    }
    assert !javacIter.hasNext();
    assert !javaParserIter.hasNext();
}
Also used : CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) ExportsTree(com.sun.source.tree.ExportsTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ForLoopTree(com.sun.source.tree.ForLoopTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ModuleTree(com.sun.source.tree.ModuleTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) OpensTree(com.sun.source.tree.OpensTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ThrowTree(com.sun.source.tree.ThrowTree) BlockTree(com.sun.source.tree.BlockTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) BreakTree(com.sun.source.tree.BreakTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) IntersectionTypeTree(com.sun.source.tree.IntersectionTypeTree) WildcardTree(com.sun.source.tree.WildcardTree) RequiresTree(com.sun.source.tree.RequiresTree) UnionTypeTree(com.sun.source.tree.UnionTypeTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) PackageTree(com.sun.source.tree.PackageTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ContinueTree(com.sun.source.tree.ContinueTree) UsesTree(com.sun.source.tree.UsesTree) CaseTree(com.sun.source.tree.CaseTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) SwitchTree(com.sun.source.tree.SwitchTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AssertTree(com.sun.source.tree.AssertTree) StatementTree(com.sun.source.tree.StatementTree) ModifiersTree(com.sun.source.tree.ModifiersTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) EmptyStatementTree(com.sun.source.tree.EmptyStatementTree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) ProvidesTree(com.sun.source.tree.ProvidesTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) ErroneousTree(com.sun.source.tree.ErroneousTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) TryTree(com.sun.source.tree.TryTree) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 57 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitTry.

@Override
public Void visitTry(TryTree javacTree, Node javaParserNode) {
    TryStmt node = castNode(TryStmt.class, javaParserNode, javacTree);
    processTry(javacTree, node);
    Iterator<? extends Tree> javacResources = javacTree.getResources().iterator();
    for (Expression resource : node.getResources()) {
        if (resource.isVariableDeclarationExpr()) {
            for (VariableDeclarator declarator : resource.asVariableDeclarationExpr().getVariables()) {
                assert javacResources.hasNext();
                javacResources.next().accept(this, declarator);
            }
        } else {
            assert javacResources.hasNext();
            javacResources.next().accept(this, resource);
        }
    }
    javacTree.getBlock().accept(this, node.getTryBlock());
    visitLists(javacTree.getCatches(), node.getCatchClauses());
    visitOptional(javacTree.getFinallyBlock(), node.getFinallyBlock());
    return null;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TryStmt(com.github.javaparser.ast.stmt.TryStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 58 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitForLoop.

@Override
public Void visitForLoop(ForLoopTree javacTree, Node javaParserNode) {
    ForStmt node = castNode(ForStmt.class, javaParserNode, javacTree);
    processForLoop(javacTree, node);
    Iterator<? extends StatementTree> javacInitializers = javacTree.getInitializer().iterator();
    for (Expression initializer : node.getInitialization()) {
        if (initializer.isVariableDeclarationExpr()) {
            for (VariableDeclarator declarator : initializer.asVariableDeclarationExpr().getVariables()) {
                assert javacInitializers.hasNext();
                javacInitializers.next().accept(this, declarator);
            }
        } else if (initializer.isAssignExpr()) {
            ExpressionStatementTree statement = (ExpressionStatementTree) javacInitializers.next();
            statement.getExpression().accept(this, initializer);
        } else {
            assert javacInitializers.hasNext();
            javacInitializers.next().accept(this, initializer);
        }
    }
    assert !javacInitializers.hasNext();
    visitOptional(javacTree.getCondition(), node.getCompare());
    // the javac statements must be unwrapped.
    assert javacTree.getUpdate().size() == node.getUpdate().size();
    Iterator<Expression> javaParserUpdates = node.getUpdate().iterator();
    for (ExpressionStatementTree javacUpdate : javacTree.getUpdate()) {
        // Match the inner javac expression with the JavaParser expression.
        javacUpdate.getExpression().accept(this, javaParserUpdates.next());
    }
    javacTree.getStatement().accept(this, node.getBody());
    return null;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ForStmt(com.github.javaparser.ast.stmt.ForStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 59 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.

the class StubParser method processField.

private void processField(FieldDeclaration decl, VariableElement elt) {
    addDeclAnnotations(declAnnos, elt);
    annotateDecl(declAnnos, elt, decl.getAnnotations());
    // StubParser parses all annotations in type annotation position as type annotations
    annotateDecl(declAnnos, elt, decl.getElementType().getAnnotations());
    AnnotatedTypeMirror fieldType = atypeFactory.fromElement(elt);
    VariableDeclarator fieldVarDecl = null;
    String eltName = elt.getSimpleName().toString();
    for (VariableDeclarator var : decl.getVariables()) {
        if (var.getName().toString().equals(eltName)) {
            fieldVarDecl = var;
            break;
        }
    }
    assert fieldVarDecl != null;
    annotate(fieldType, fieldVarDecl.getType(), decl.getAnnotations());
    putNew(atypes, elt, fieldType);
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 60 with VariableDeclarator

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

the class NodeWithVariables method getElementType.

/**
 * Returns the element 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 also int. Note: no mention of b being an array.
 * <br/>For <code>int a,b;</code>, then doing setType(String) on b, then calling getElementType(). This is an assertion error. It is also a situation that you don't really want.
 */
default Type getElementType() {
    NodeList<VariableDeclarator> variables = getVariables();
    if (variables.isEmpty()) {
        throw new AssertionError("There is no element type since there are no variables.");
    }
    Type type = variables.get(0).getType().getElementType();
    for (int i = 1; i < variables.size(); i++) {
        if (!variables.get(i).getType().getElementType().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)

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