use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class ConcreteSyntaxModelTest method printSimplestClass.
@Test
public void printSimplestClass() {
Node node = JavaParser.parse("class A {}");
assertEquals("class A {" + EOL + "}" + EOL, print(node));
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class NodeWithModifiersTest method addModifierTriggerNotification.
@Test
public void addModifierTriggerNotification() {
List<String> changes = new LinkedList<>();
ClassOrInterfaceDeclaration decl = new ClassOrInterfaceDeclaration(EnumSet.noneOf(Modifier.class), false, "Foo");
decl.register(new AstObserverAdapter() {
@Override
public void propertyChange(Node observedNode, ObservableProperty property, Object oldValue, Object newValue) {
changes.add("property " + property.name() + " is changed to " + newValue);
}
});
decl.addModifier(Modifier.PUBLIC);
assertEquals(1, changes.size());
assertEquals("property MODIFIERS is changed to [PUBLIC]", changes.get(0));
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class Node method getAllContainedComments.
/**
* This is the list of Comment which are contained in the Node either because
* they are properly associated to one of its children or because they are floating
* around inside the Node
*
* @return all Comments within the node as a list
*/
public List<Comment> getAllContainedComments() {
List<Comment> comments = new LinkedList<>();
comments.addAll(getOrphanComments());
for (Node child : getChildNodes()) {
child.getComment().ifPresent(comments::add);
comments.addAll(child.getAllContainedComments());
}
return comments;
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class VarValidator method accept.
@Override
public void accept(VarType node, ProblemReporter reporter) {
// All allowed locations are within a VariableDeclaration inside a VariableDeclarationExpr inside something else.
Optional<VariableDeclarator> variableDeclarator = node.findParent(VariableDeclarator.class);
if (!variableDeclarator.isPresent()) {
// Java 11's var in lambda's
if (varAllowedInLambdaParameters) {
boolean valid = node.findParent(Parameter.class).flatMap(Node::getParentNode).map((Node p) -> p instanceof LambdaExpr).orElse(false);
if (valid) {
return;
}
}
reportIllegalPosition(node, reporter);
return;
}
variableDeclarator.ifPresent(vd -> {
Optional<Node> variableDeclarationExpr = vd.getParentNode();
if (!variableDeclarationExpr.isPresent()) {
reportIllegalPosition(node, reporter);
return;
}
variableDeclarationExpr.ifPresent(vdeNode -> {
if (!(vdeNode instanceof VariableDeclarationExpr)) {
reportIllegalPosition(node, reporter);
return;
}
VariableDeclarationExpr vde = (VariableDeclarationExpr) vdeNode;
if (vde.getVariables().size() > 1) {
reporter.report(vde, "\"var\" only takes a single variable.");
}
Optional<Node> container = vdeNode.getParentNode();
if (!container.isPresent()) {
reportIllegalPosition(node, reporter);
return;
}
container.ifPresent(c -> {
boolean positionIsFine = c instanceof ForStmt || c instanceof ForeachStmt || c instanceof ExpressionStmt;
if (!positionIsFine) {
reportIllegalPosition(node, reporter);
}
// A local variable declaration ends up inside an ExpressionStmt.
if (c instanceof ExpressionStmt) {
if (!vd.getInitializer().isPresent()) {
reporter.report(node, "\"var\" needs an initializer.");
}
vd.getInitializer().ifPresent(initializer -> {
if (initializer instanceof NullLiteralExpr) {
reporter.report(node, "\"var\" cannot infer type from just null.");
}
if (initializer instanceof ArrayCreationExpr) {
reporter.report(node, "\"var\" cannot infer array types.");
}
});
}
});
});
});
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class TreeVisitor method visitBreadthFirst.
/**
* https://en.wikipedia.org/wiki/Breadth-first_search
*
* @param node the start node, and the first one that is passed to process(node).
*/
public void visitBreadthFirst(Node node) {
final Queue<Node> queue = new LinkedList<>();
queue.offer(node);
while (queue.size() > 0) {
final Node head = queue.peek();
for (Node child : head.getChildNodes()) {
queue.offer(child);
}
process(queue.poll());
}
}
Aggregations