Search in sources :

Example 21 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class JavaParser method attributeLineCommentToNodeOrChild.

private static boolean attributeLineCommentToNodeOrChild(Node node, LineComment lineComment) {
    // let's give to it the comment
    if (node.getBeginLine() == lineComment.getBeginLine() && !node.hasComment()) {
        node.setComment(lineComment);
        return true;
    } else {
        // try with all the children, sorted by reverse position (so the
        // first one is the nearest to the comment
        List<Node> children = new LinkedList<Node>();
        children.addAll(node.getChildrenNodes());
        PositionUtils.sortByBeginPosition(children);
        Collections.reverse(children);
        for (Node child : children) {
            if (attributeLineCommentToNodeOrChild(child, lineComment)) {
                return true;
            }
        }
        return false;
    }
}
Also used : Node(com.github.javaparser.ast.Node) LinkedList(java.util.LinkedList)

Example 22 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class CommentsInserter method attributeLineCommentToNodeOrChild.

private boolean attributeLineCommentToNodeOrChild(Node node, LineComment lineComment) {
    // let's give to it the comment
    if (node.getBegin().line == lineComment.getBegin().line && !node.hasComment()) {
        if (!(node instanceof Comment)) {
            node.setComment(lineComment);
        }
        return true;
    } else {
        // try with all the children, sorted by reverse position (so the
        // first one is the nearest to the comment
        List<Node> children = new LinkedList<Node>();
        children.addAll(node.getChildrenNodes());
        PositionUtils.sortByBeginPosition(children);
        Collections.reverse(children);
        for (Node child : children) {
            if (attributeLineCommentToNodeOrChild(child, lineComment)) {
                return true;
            }
        }
        return false;
    }
}
Also used : LineComment(com.github.javaparser.ast.comments.LineComment) Comment(com.github.javaparser.ast.comments.Comment) Node(com.github.javaparser.ast.Node)

Example 23 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class JavaParserFacade method solveMethodAsUsage.

public MethodUsage solveMethodAsUsage(MethodCallExpr call) {
    List<ResolvedType> params = new ArrayList<>();
    if (call.getArguments() != null) {
        for (Expression param : call.getArguments()) {
            // getTypeConcrete(Node node, boolean solveLambdas)
            try {
                params.add(getType(param, false));
            } catch (Exception e) {
                throw new RuntimeException(String.format("Error calculating the type of parameter %s of method call %s", param, call), e);
            }
        // params.add(getTypeConcrete(param, false));
        }
    }
    Context context = JavaParserFactory.getContext(call, typeSolver);
    Optional<MethodUsage> methodUsage = context.solveMethodAsUsage(call.getName().getId(), params, typeSolver);
    if (!methodUsage.isPresent()) {
        throw new RuntimeException("Method '" + call.getName() + "' cannot be resolved in context " + call + " (line: " + call.getRange().map(r -> "" + r.begin.line).orElse("??") + ") " + context + ". Parameter types: " + params);
    }
    return methodUsage.get();
}
Also used : FieldAccessContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.FieldAccessContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) FieldAccessContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.FieldAccessContext) java.util(java.util) Navigator.requireParentNode(com.github.javaparser.symbolsolver.javaparser.Navigator.requireParentNode) MethodUsage(com.github.javaparser.resolution.MethodUsage) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) Level(java.util.logging.Level) com.github.javaparser.ast.body(com.github.javaparser.ast.body) Context(com.github.javaparser.symbolsolver.core.resolution.Context) ConstructorResolutionLogic(com.github.javaparser.symbolsolver.resolution.ConstructorResolutionLogic) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) com.github.javaparser.ast.type(com.github.javaparser.ast.type) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) com.github.javaparser.symbolsolver.model.typesystem(com.github.javaparser.symbolsolver.model.typesystem) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) Logger(java.util.logging.Logger) com.github.javaparser.resolution.declarations(com.github.javaparser.resolution.declarations) Collectors(java.util.stream.Collectors) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) com.github.javaparser.symbolsolver.javaparsermodel.declarations(com.github.javaparser.symbolsolver.javaparsermodel.declarations) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) com.github.javaparser.resolution.types(com.github.javaparser.resolution.types) ConsoleHandler(java.util.logging.ConsoleHandler) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) com.github.javaparser.ast.expr(com.github.javaparser.ast.expr) MethodUsage(com.github.javaparser.resolution.MethodUsage) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException)

Example 24 with Node

use of com.github.javaparser.ast.Node 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)

Example 25 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class ConcreteSyntaxModelTest method printAnEmptyInterfaceWithModifier.

@Test
public void printAnEmptyInterfaceWithModifier() {
    Node node = JavaParser.parse("public interface A {}");
    assertEquals("public interface A {" + EOL + "}" + EOL, print(node));
}
Also used : Node(com.github.javaparser.ast.Node) Test(org.junit.Test)

Aggregations

Node (com.github.javaparser.ast.Node)95 Test (org.junit.Test)24 Expression (com.github.javaparser.ast.expr.Expression)22 NodeList (com.github.javaparser.ast.NodeList)18 Comment (com.github.javaparser.ast.comments.Comment)13 CompilationUnit (com.github.javaparser.ast.CompilationUnit)12 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 NameExpr (com.github.javaparser.ast.expr.NameExpr)12 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 ArrayList (java.util.ArrayList)11 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)8 Collectors (java.util.stream.Collectors)8 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)7 LineComment (com.github.javaparser.ast.comments.LineComment)7 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)7 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)7 SimpleName (com.github.javaparser.ast.expr.SimpleName)7 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)7 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)6 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)6