Search in sources :

Example 51 with VariableDeclarator

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

the class AnnotationFileParser method processField.

/**
 * Process the field declaration in decl: copy its annotations to {@code #annotationFileAnnos}.
 *
 * @param decl the declaration in the annotation file
 * @param elt the element representing that same declaration
 */
private void processField(FieldDeclaration decl, VariableElement elt) {
    if (skipNode(decl)) {
        // and might refer to types that are not accessible.
        return;
    }
    markAsFromStubFile(elt);
    recordDeclAnnotation(elt, decl.getAnnotations(), decl);
    // AnnotationFileParser parses all annotations in type annotation position as type annotations
    recordDeclAnnotation(elt, decl.getElementType().getAnnotations(), decl);
    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(), fieldVarDecl);
    putMerge(annotationFileAnnos.atypes, elt, fieldType);
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 52 with VariableDeclarator

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

the class ToIndexFileConverter method visit.

@Override
public Void visit(VariableDeclarationExpr expr, AElement elem) {
    List<AnnotationExpr> annos = expr.getAnnotations();
    AMethod method = (AMethod) elem;
    List<VariableDeclarator> varDecls = expr.getVariables();
    for (int i = 0; i < varDecls.size(); i++) {
        VariableDeclarator decl = varDecls.get(i);
        LocalLocation loc = new LocalLocation(i, decl.getNameAsString());
        AField field = method.body.locals.getVivify(loc);
        visitType(expr.getCommonType(), field.type);
        if (annos != null) {
            for (AnnotationExpr annoExpr : annos) {
                Annotation anno = extractAnnotation(annoExpr);
                field.tlAnnotationsHere.add(anno);
            }
        }
    }
    return null;
}
Also used : AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) LocalLocation(scenelib.annotations.el.LocalLocation) AMethod(scenelib.annotations.el.AMethod) Annotation(scenelib.annotations.Annotation) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AField(scenelib.annotations.el.AField)

Example 53 with VariableDeclarator

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

the class ToIndexFileConverter method visit.

@Override
public Void visit(FieldDeclaration decl, AElement elem) {
    for (VariableDeclarator v : decl.getVariables()) {
        AClass clazz = (AClass) elem;
        AField field = clazz.fields.getVivify(v.getNameAsString());
        visitDecl(decl, field);
        visitType(decl.getCommonType(), field.type);
    }
    return null;
}
Also used : AClass(scenelib.annotations.el.AClass) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AField(scenelib.annotations.el.AField)

Example 54 with VariableDeclarator

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

the class JointJavacJavaParserVisitor method visitVariable.

@Override
public Void visitVariable(VariableTree javacTree, Node javaParserNode) {
    // variable declarations, parameters, and fields.
    if (javaParserNode instanceof VariableDeclarator) {
        // JavaParser uses VariableDeclarator as parts of other declaration types like
        // VariableDeclarationExpr when multiple variables may be declared.
        VariableDeclarator node = (VariableDeclarator) javaParserNode;
        processVariable(javacTree, node);
        // Don't process the variable type when it's the Java keyword "var".
        if (!node.getType().isVarType() && (!node.getType().isClassOrInterfaceType() || !node.getType().asClassOrInterfaceType().getName().asString().equals("var"))) {
            javacTree.getType().accept(this, node.getType());
        }
        // The name expression can be null, even when a name exists.
        if (javacTree.getNameExpression() != null) {
            javacTree.getNameExpression().accept(this, node.getName());
        }
        visitOptional(javacTree.getInitializer(), node.getInitializer());
    } else if (javaParserNode instanceof Parameter) {
        Parameter node = (Parameter) javaParserNode;
        processVariable(javacTree, node);
        if (node.isVarArgs()) {
            ArrayTypeTree arrayType;
            // AnnotatedType depending on whether it has an annotation.
            if (javacTree.getType().getKind() == Tree.Kind.ARRAY_TYPE) {
                arrayType = (ArrayTypeTree) javacTree.getType();
            } else {
                AnnotatedTypeTree annotatedType = (AnnotatedTypeTree) javacTree.getType();
                arrayType = (ArrayTypeTree) annotatedType.getUnderlyingType();
            }
            arrayType.getType().accept(this, node.getType());
        } else {
            // don't process them.
            if (!node.getType().isUnknownType()) {
                javacTree.getType().accept(this, node.getType());
            }
        }
        // The name expression can be null, even when a name exists.
        if (javacTree.getNameExpression() != null) {
            javacTree.getNameExpression().accept(this, node.getName());
        }
        assert javacTree.getInitializer() == null;
    } else if (javaParserNode instanceof ReceiverParameter) {
        ReceiverParameter node = (ReceiverParameter) javaParserNode;
        processVariable(javacTree, node);
        javacTree.getType().accept(this, node.getType());
        // The name expression can be null, even when a name exists.
        if (javacTree.getNameExpression() != null) {
            javacTree.getNameExpression().accept(this, node.getName());
        }
        assert javacTree.getInitializer() == null;
    } else if (javaParserNode instanceof EnumConstantDeclaration) {
        // In javac, an enum constant is expanded as a variable declaration initialized to a
        // constuctor call.
        EnumConstantDeclaration node = (EnumConstantDeclaration) javaParserNode;
        processVariable(javacTree, node);
        if (javacTree.getNameExpression() != null) {
            javacTree.getNameExpression().accept(this, node.getName());
        }
        assert javacTree.getInitializer().getKind() == Tree.Kind.NEW_CLASS;
        NewClassTree constructor = (NewClassTree) javacTree.getInitializer();
        visitLists(constructor.getArguments(), node.getArguments());
        if (constructor.getClassBody() != null) {
            visitAnonymousClassBody(constructor.getClassBody(), node.getClassBody());
        } else {
            assert node.getClassBody().isEmpty();
        }
    } else {
        throwUnexpectedNodeType(javacTree, javaParserNode);
    }
    return null;
}
Also used : AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.type.TypeParameter) NewClassTree(com.sun.source.tree.NewClassTree) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree)

Example 55 with VariableDeclarator

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

the class JointJavacJavaParserVisitor method processStatements.

/**
 * Given a matching sequence of statements for a block, visits each javac statement with its
 * corresponding JavaParser statement, excluding synthetic javac trees like no-argument
 * constructors.
 *
 * @param javacStatements sequence of javac trees for statements
 * @param javaParserStatements sequence of JavaParser statements representing the same block as
 *     {@code javacStatements}
 */
private void processStatements(Iterable<? extends StatementTree> javacStatements, Iterable<Statement> javaParserStatements) {
    PeekingIterator<StatementTree> javacIter = Iterators.peekingIterator(javacStatements.iterator());
    PeekingIterator<Statement> javaParserIter = Iterators.peekingIterator(javaParserStatements.iterator());
    while (javacIter.hasNext() || javaParserIter.hasNext()) {
        // Skip synthetic javac super() calls by checking if the JavaParser statement matches.
        if (javacIter.hasNext() && isDefaultSuperConstructorCall(javacIter.peek()) && (!javaParserIter.hasNext() || !isDefaultSuperConstructorCall(javaParserIter.peek()))) {
            javacIter.next();
            continue;
        }
        // VariableDeclarators. Match the declarators with the VariableTrees.
        if (javaParserIter.hasNext() && javacIter.peek().getKind() == Tree.Kind.VARIABLE && javaParserIter.peek().isExpressionStmt() && javaParserIter.peek().asExpressionStmt().getExpression().isVariableDeclarationExpr()) {
            for (VariableDeclarator decl : javaParserIter.next().asExpressionStmt().getExpression().asVariableDeclarationExpr().getVariables()) {
                assert javacIter.hasNext();
                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 : ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) StatementTree(com.sun.source.tree.StatementTree) EmptyStatementTree(com.sun.source.tree.EmptyStatementTree) Statement(com.github.javaparser.ast.stmt.Statement) 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