Search in sources :

Example 66 with Node

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

the class ParsingSteps method thenTheBeginColumnIs.

@Then("the begin column is $column")
public void thenTheBeginColumnIs(int column) {
    Node node = (Node) state.get("selectedNode");
    assertEquals(column, node.getBegin().get().column);
}
Also used : Node(com.github.javaparser.ast.Node) Then(org.jbehave.core.annotations.Then)

Example 67 with Node

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

the class ParsingSteps method thenTheBeginLineIs.

@Then("the begin line is $line")
public void thenTheBeginLineIs(int line) {
    Node node = (Node) state.get("selectedNode");
    assertEquals(line, node.getBegin().get().line);
}
Also used : Node(com.github.javaparser.ast.Node) Then(org.jbehave.core.annotations.Then)

Example 68 with Node

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

the class NodeMetaModelGenerator method generate.

public void generate(Class<? extends Node> nodeClass, ClassOrInterfaceDeclaration metaModelCoid, NodeList<Statement> initializeNodeMetaModelsStatements, NodeList<Statement> initializePropertyMetaModelsStatements, NodeList<Statement> initializeConstructorParametersStatements, SourceRoot sourceRoot) throws NoSuchMethodException {
    final String className = nodeMetaModelName(nodeClass);
    final String nodeMetaModelFieldName = decapitalize(className);
    metaModelCoid.getFieldByName(nodeMetaModelFieldName).ifPresent(Node::remove);
    final FieldDeclaration nodeField = metaModelCoid.addField(className, nodeMetaModelFieldName, PUBLIC, STATIC, FINAL);
    final Class<?> superclass = nodeClass.getSuperclass();
    final String superNodeMetaModel = nodeMetaModelName(superclass);
    boolean isRootNode = !isNode(superclass);
    nodeField.getVariable(0).setInitializer(parseExpression(f("new %s(%s)", className, optionalOf(decapitalize(superNodeMetaModel), !isRootNode))));
    initializeNodeMetaModelsStatements.add(parseStatement(f("nodeMetaModels.add(%s);", nodeMetaModelFieldName)));
    final CompilationUnit classMetaModelJavaFile = new CompilationUnit(METAMODEL_PACKAGE);
    classMetaModelJavaFile.addImport("java.util.Optional");
    sourceRoot.add(METAMODEL_PACKAGE, className + ".java", classMetaModelJavaFile);
    final ClassOrInterfaceDeclaration nodeMetaModelClass = classMetaModelJavaFile.addClass(className, PUBLIC);
    if (isRootNode) {
        nodeMetaModelClass.addExtendedType(BASE_NODE_META_MODEL);
    } else {
        nodeMetaModelClass.addExtendedType(superNodeMetaModel);
    }
    final AstTypeAnalysis typeAnalysis = new AstTypeAnalysis(nodeClass);
    final ConstructorDeclaration classMMConstructor = nodeMetaModelClass.addConstructor().addParameter("Optional<" + BASE_NODE_META_MODEL + ">", "super" + BASE_NODE_META_MODEL);
    classMMConstructor.getBody().addStatement(parseExplicitConstructorInvocationStmt(f("super(super%s, %s.class, \"%s\", \"%s\", %s, %s);", BASE_NODE_META_MODEL, nodeClass.getName(), nodeClass.getSimpleName(), nodeClass.getPackage().getName(), typeAnalysis.isAbstract, typeAnalysis.isSelfType)));
    if (typeAnalysis.isAbstract) {
        classMetaModelJavaFile.addImport(Node.class);
        nodeMetaModelClass.addMember(parseBodyDeclaration(f("protected %s(Optional<BaseNodeMetaModel> superNodeMetaModel, Class<? extends Node> type, String name, String packageName, boolean isAbstract, boolean hasWildcard) {" + "super(superNodeMetaModel, type, name, packageName, isAbstract, hasWildcard);" + " }", className)));
    }
    final List<Field> fields = new ArrayList<>(Arrays.asList(nodeClass.getDeclaredFields()));
    fields.sort(Comparator.comparing(Field::getName));
    for (Field field : fields) {
        if (fieldShouldBeIgnored(field)) {
            continue;
        }
        initializePropertyMetaModelsStatementsGenerator.generate(field, nodeMetaModelClass, nodeMetaModelFieldName, initializePropertyMetaModelsStatements);
    }
    final List<Method> methods = new ArrayList<>(Arrays.asList(nodeClass.getMethods()));
    methods.sort(Comparator.comparing(Method::getName));
    for (Method method : methods) {
        if (method.isAnnotationPresent(DerivedProperty.class)) {
            initializePropertyMetaModelsStatementsGenerator.generateDerivedProperty(method, nodeMetaModelClass, nodeMetaModelFieldName, initializePropertyMetaModelsStatements);
        }
    }
    initializeConstructorParametersStatementsGenerator.generate(nodeClass, initializeConstructorParametersStatements);
    moveStaticInitializeToTheEndOfTheClassBecauseWeNeedTheFieldsToInitializeFirst(metaModelCoid);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Node(com.github.javaparser.ast.Node) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field)

Example 69 with Node

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

the class PropagatingAstObserverTest method verifyPropagation.

@Test
public void verifyPropagation() {
    String code = "class A {  }";
    CompilationUnit cu = JavaParser.parse(code);
    List<String> changes = new ArrayList<>();
    AstObserver observer = new PropagatingAstObserver() {

        @Override
        public void concretePropertyChange(Node observedNode, ObservableProperty property, Object oldValue, Object newValue) {
            changes.add(String.format("%s.%s changed from %s to %s", observedNode.getClass().getSimpleName(), property.name().toLowerCase(), oldValue, newValue));
        }
    };
    cu.registerForSubtree(observer);
    assertEquals(Arrays.asList(), changes);
    FieldDeclaration fieldDeclaration = cu.getClassByName("A").get().addField("String", "foo");
    assertEquals(Arrays.asList(), changes);
    assertEquals(true, fieldDeclaration.isRegistered(observer));
    cu.getClassByName("A").get().getFieldByName("foo").get().getVariables().get(0).setName("Bar");
    assertEquals(Arrays.asList("VariableDeclarator.name changed from foo to Bar"), changes);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Node(com.github.javaparser.ast.Node) ArrayList(java.util.ArrayList) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) Test(org.junit.Test)

Example 70 with Node

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

the class TreeVisitorTest method issue743ConcurrentModificationProblem.

@Test
public void issue743ConcurrentModificationProblem() {
    Expression expression = JavaParser.parseExpression("new int[]{1,2,3,4}");
    StringBuilder result = new StringBuilder();
    TreeVisitor visitor = new TreeVisitor() {

        @Override
        public void process(Node node) {
            if (node instanceof IntegerLiteralExpr) {
                node.getParentNode().ifPresent(parent -> ((ArrayInitializerExpr) parent).getValues().add(new IntegerLiteralExpr("1")));
            }
            result.append("<").append(node).append("> ");
        }
    };
    visitor.visitPreOrder(expression);
    System.out.println(result);
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) 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