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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations