Search in sources :

Example 11 with BodyDeclaration

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

the class AnnotationFileParser method putNewElement.

// Used only by getMembers().
/**
 * If {@code typeElt} contains an element for {@code member}, adds to {@code elementsToDecl} a
 * mapping from member's element to member. Does nothing if a mapping already exists.
 *
 * <p>Otherwise (if there is no element for {@code member}), adds to {@code fakeOverrideDecls}
 * zero or more mappings. Each mapping is from an element that {@code member} would override to
 * {@code member}.
 *
 * <p>This method does not read or write field {@link annotationFileAnnos}.
 *
 * @param elementsToDecl the mapping that is side-effected by this method
 * @param fakeOverrideDecls fake overrides, also side-effected by this method
 * @param typeElt the class in which {@code member} is declared
 * @param member the stub file declaration of a method
 * @param typeDeclName used only for debugging
 * @param astNode where to report errors
 */
private void putNewElement(Map<Element, BodyDeclaration<?>> elementsToDecl, Map<Element, List<BodyDeclaration<?>>> fakeOverrideDecls, TypeElement typeElt, BodyDeclaration<?> member, String typeDeclName, NodeWithRange<?> astNode) {
    if (member instanceof MethodDeclaration) {
        MethodDeclaration method = (MethodDeclaration) member;
        Element elt = findElement(typeElt, method, /*noWarn=*/
        true);
        if (elt != null) {
            putIfAbsent(elementsToDecl, elt, method);
        } else {
            ExecutableElement overriddenMethod = fakeOverriddenMethod(typeElt, method);
            if (overriddenMethod == null) {
                // Didn't find the element and it isn't a fake override.  Issue a warning.
                findElement(typeElt, method, /*noWarn=*/
                false);
            } else {
                List<BodyDeclaration<?>> l = fakeOverrideDecls.computeIfAbsent(overriddenMethod, __ -> new ArrayList<>());
                l.add(member);
            }
        }
    } else if (member instanceof ConstructorDeclaration) {
        Element elt = findElement(typeElt, (ConstructorDeclaration) member);
        if (elt != null) {
            putIfAbsent(elementsToDecl, elt, member);
        }
    } else if (member instanceof FieldDeclaration) {
        FieldDeclaration fieldDecl = (FieldDeclaration) member;
        for (VariableDeclarator var : fieldDecl.getVariables()) {
            Element varelt = findElement(typeElt, var);
            if (varelt != null) {
                putIfAbsent(elementsToDecl, varelt, fieldDecl);
            }
        }
    } else if (member instanceof EnumConstantDeclaration) {
        Element elt = findElement(typeElt, (EnumConstantDeclaration) member, astNode);
        if (elt != null) {
            putIfAbsent(elementsToDecl, elt, member);
        }
    } else if (member instanceof ClassOrInterfaceDeclaration) {
        Element elt = findElement(typeElt, (ClassOrInterfaceDeclaration) member);
        if (elt != null) {
            putIfAbsent(elementsToDecl, elt, member);
        }
    } else if (member instanceof EnumDeclaration) {
        Element elt = findElement(typeElt, (EnumDeclaration) member);
        if (elt != null) {
            putIfAbsent(elementsToDecl, elt, member);
        }
    } else {
        stubDebug(String.format("Ignoring element of type %s in %s", member.getClass(), typeDeclName));
    }
}
Also used : EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration)

Example 12 with BodyDeclaration

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

the class ToIndexFileConverter method visit.

@Override
public Void visit(ObjectCreationExpr expr, AElement elem) {
    ClassOrInterfaceType type = expr.getType();
    AClass clazz = scene.classes.getVivify(type.getNameAsString());
    Expression scope = expr.getScope().orElse(null);
    List<Type> typeArgs = expr.getTypeArguments().orElse(null);
    List<Expression> args = expr.getArguments();
    NodeList<BodyDeclaration<?>> bodyDecls = expr.getAnonymousClassBody().orElse(null);
    if (scope != null) {
        scope.accept(this, elem);
    }
    if (args != null) {
        for (Expression arg : args) {
            arg.accept(this, elem);
        }
    }
    if (typeArgs != null) {
        for (Type typeArg : typeArgs) {
            typeArg.accept(this, elem);
        }
    }
    type.accept(this, clazz);
    if (bodyDecls != null) {
        for (BodyDeclaration<?> decl : bodyDecls) {
            decl.accept(this, clazz);
        }
    }
    return null;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) ReferenceType(com.github.javaparser.ast.type.ReferenceType) VoidType(com.github.javaparser.ast.type.VoidType) ArrayType(com.github.javaparser.ast.type.ArrayType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) WildcardType(com.github.javaparser.ast.type.WildcardType) Expression(com.github.javaparser.ast.expr.Expression) AClass(scenelib.annotations.el.AClass) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 13 with BodyDeclaration

use of com.github.javaparser.ast.body.BodyDeclaration 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 14 with BodyDeclaration

use of com.github.javaparser.ast.body.BodyDeclaration in project eap-additional-testsuite by jboss-set.

the class ParsedTests method readInternalClassMethods.

private static void readInternalClassMethods(String file, FileData fd) throws IOException {
    InputStream in = null;
    CompilationUnit cu = null;
    try {
        in = new FileInputStream(file);
        cu = JavaParser.parse(in);
        if (internalClassMethods.get(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", "")) == null) {
            internalClassMethods.put(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", ""), new HashMap<String, ArrayList<String[]>>());
        }
        NodeList<TypeDeclaration<?>> types = cu.getTypes();
        for (TypeDeclaration<?> type : types) {
            NodeList<BodyDeclaration<?>> members = type.getMembers();
            for (BodyDeclaration<?> member : members) {
                if (member instanceof MethodDeclaration) {
                    MethodDeclaration method = (MethodDeclaration) member;
                    HashMap<String, ArrayList<String[]>> arg = internalClassMethods.get(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", ""));
                    // System.out.println("yyy " + method.getParameters().size() + " " + method.getParameters());
                    String[] params = new String[method.getParameters().size()];
                    for (int i = 0; i < method.getParameters().size(); i++) {
                        params[i] = method.getParameters().get(i).getTypeAsString();
                    }
                    if (!arg.keySet().contains(method.getName().toString())) {
                        ArrayList<String[]> paramArrayList = new ArrayList<>();
                        paramArrayList.add(params);
                        arg.put(method.getName().toString(), paramArrayList);
                        ArrayList<String[]> paramRTArrayList = new ArrayList<>();
                        paramRTArrayList.add(new String[] { method.getTypeAsString() });
                        arg.put(method.getName().toString() + "_Return_Type", paramRTArrayList);
                    } else {
                        arg.get(method.getName().toString()).add(params);
                        arg.get(method.getName().toString() + "_Return_Type").add(new String[] { method.getTypeAsString() });
                    }
                }
                if (member instanceof ConstructorDeclaration) {
                    ConstructorDeclaration construct = (ConstructorDeclaration) member;
                    HashMap<String, ArrayList<String[]>> arg = internalClassMethods.get(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", ""));
                    String[] params = new String[construct.getParameters().size()];
                    for (int i = 0; i < construct.getParameters().size(); i++) {
                        params[i] = construct.getParameters().get(i).getTypeAsString();
                    }
                    if (!arg.keySet().contains(construct.getName().toString() + "_Constructor")) {
                        ArrayList<String[]> paramCTArrayList = new ArrayList<>();
                        paramCTArrayList.add(params);
                        arg.put(construct.getName().toString() + "_Constructor", paramCTArrayList);
                    } else {
                        arg.get(construct.getName().toString() + "_Constructor").add(params);
                    }
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        in.close();
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 15 with BodyDeclaration

use of com.github.javaparser.ast.body.BodyDeclaration in project eap-additional-testsuite by jboss-set.

the class ParsedTests method readInternalClassFields.

private static void readInternalClassFields(String file, FileData fd) throws IOException {
    InputStream in = null;
    CompilationUnit cu = null;
    try {
        in = new FileInputStream(file);
        cu = JavaParser.parse(in);
        if (internalClassFields.get(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", "")) == null) {
            internalClassFields.put(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", ""), new HashMap<String, String>());
        }
        NodeList<TypeDeclaration<?>> types = cu.getTypes();
        for (TypeDeclaration<?> type : types) {
            NodeList<BodyDeclaration<?>> members = type.getMembers();
            for (BodyDeclaration<?> member : members) {
                if (member instanceof FieldDeclaration) {
                    FieldDeclaration field = (FieldDeclaration) member;
                    HashMap<String, String> arg = internalClassFields.get(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", ""));
                    if (!field.getModifiers().contains("private")) {
                        arg.put(field.getVariables().get(0).getName().toString(), field.getVariables().get(0).getType().toString());
                    }
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        in.close();
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Aggregations

BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)29 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)10 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)9 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)9 CompilationUnit (com.github.javaparser.ast.CompilationUnit)7 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)7 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)7 NodeList (com.github.javaparser.ast.NodeList)6 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)6 ArrayList (java.util.ArrayList)6 EnumConstantDeclaration (com.github.javaparser.ast.body.EnumConstantDeclaration)5 BlockComment (com.github.javaparser.ast.comments.BlockComment)5 Comment (com.github.javaparser.ast.comments.Comment)5 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)5 LineComment (com.github.javaparser.ast.comments.LineComment)5 List (java.util.List)5 Parameter (com.github.javaparser.ast.body.Parameter)4 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)3 Expression (com.github.javaparser.ast.expr.Expression)3 Type (com.github.javaparser.ast.type.Type)3