use of com.github.javaparser.ast.Node in project drools by kiegroup.
the class DrlxParseUtil method rescopeNamesToNewScope.
/**
* Mutates expression
* such that, if it contains a NameExpr for any of the <code>names</code>,
* it is replaced with a FieldAccessExpr having <code>newScope</code> as the scope.
*/
public static void rescopeNamesToNewScope(Expression newScope, List<String> names, Node e) {
if (e instanceof NodeWithArguments) {
NodeWithArguments<?> arguments = (NodeWithArguments) e;
for (Expression argument : arguments.getArguments()) {
rescopeNamesToNewScope(newScope, names, argument);
}
}
if (e instanceof AssignExpr) {
AssignExpr assignExpr = (AssignExpr) e;
rescopeNamesToNewScope(newScope, names, assignExpr.getTarget());
rescopeNamesToNewScope(newScope, names, assignExpr.getValue());
} else if (e instanceof BinaryExpr) {
rescopeNamesToNewScope(newScope, names, ((BinaryExpr) e).getLeft());
rescopeNamesToNewScope(newScope, names, ((BinaryExpr) e).getRight());
} else if (e instanceof UnaryExpr) {
rescopeNamesToNewScope(newScope, names, ((UnaryExpr) e).getExpression());
} else if (e instanceof EnclosedExpr) {
rescopeNamesToNewScope(newScope, names, ((EnclosedExpr) e).getInner());
} else if (e instanceof Expression) {
Optional<Expression> rootNode = DrlxParseUtil.findRootNodeViaScope((Expression) e);
if (rootNode.isPresent() && rootNode.get() instanceof NameExpr) {
NameExpr nameExpr = (NameExpr) rootNode.get();
if (names.contains(nameExpr.getNameAsString())) {
Expression prepend = new FieldAccessExpr(newScope, nameExpr.getNameAsString());
if (e instanceof NameExpr) {
Optional<Node> parentNode = e.getParentNode();
if (parentNode.isPresent()) {
// actually `e` was not composite, it was already the NameExpr node I was looking to replace.
parentNode.get().replace(nameExpr, prepend);
} else {
throw new IllegalStateException("Cannot find parent node for " + ((NameExpr) e).getNameAsString() + "!");
}
} else {
e.replace(nameExpr, prepend);
}
}
}
} else {
for (Node child : e.getChildNodes()) {
rescopeNamesToNewScope(newScope, names, child);
}
}
}
use of com.github.javaparser.ast.Node in project drools by kiegroup.
the class DroolsMvelParserTest method testBindingOnRight.
@Test
public void testBindingOnRight() {
String expr = "$n : name == \"Mario\" && $a : age > 20";
DrlxExpression drlxExpression = parseExpression(parser, expr);
Expression bExpr = drlxExpression.getExpr();
assertTrue(bExpr instanceof BinaryExpr);
Node left = ((BinaryExpr) bExpr).getLeft();
assertTrue(left instanceof DrlxExpression);
DrlxExpression leftExpr = (DrlxExpression) left;
SimpleName leftBind = leftExpr.getBind();
assertEquals("$n", leftBind.asString());
Expression expression = leftExpr.getExpr();
BinaryExpr binaryExpr = ((BinaryExpr) expression);
assertEquals("name", toString(binaryExpr.getLeft()));
assertEquals("\"Mario\"", toString(binaryExpr.getRight()));
assertEquals(Operator.EQUALS, binaryExpr.getOperator());
Node right = ((BinaryExpr) bExpr).getRight();
assertTrue(right instanceof DrlxExpression);
DrlxExpression rightExpr = (DrlxExpression) right;
SimpleName rightBind = rightExpr.getBind();
assertEquals("$a", rightBind.asString());
BinaryExpr binaryExpr2 = ((BinaryExpr) rightExpr.getExpr());
assertEquals("age", toString(binaryExpr2.getLeft()));
assertEquals("20", toString(binaryExpr2.getRight()));
assertEquals(Operator.GREATER, binaryExpr2.getOperator());
}
use of com.github.javaparser.ast.Node in project drools by kiegroup.
the class DroolsMvelParserTest method testComplexEnclosedBindVariable.
@Test
public void testComplexEnclosedBindVariable() {
String expr = "($n : name == \"Mario\") && (age > 20)";
DrlxExpression drlxExpression = parseExpression(parser, expr);
Expression bExpr = drlxExpression.getExpr();
assertTrue(bExpr instanceof BinaryExpr);
Node left = ((BinaryExpr) bExpr).getLeft();
assertTrue(left instanceof EnclosedExpr);
Expression inner = ((EnclosedExpr) left).getInner();
assertTrue(inner instanceof DrlxExpression);
DrlxExpression innerDrlxExpression = (DrlxExpression) inner;
SimpleName bind = innerDrlxExpression.getBind();
assertEquals("$n", bind.asString());
Expression expression = innerDrlxExpression.getExpr();
BinaryExpr binaryExpr = ((BinaryExpr) expression);
assertEquals("name", toString(binaryExpr.getLeft()));
assertEquals("\"Mario\"", toString(binaryExpr.getRight()));
assertEquals(Operator.EQUALS, binaryExpr.getOperator());
Node right = ((BinaryExpr) bExpr).getRight();
assertTrue(right instanceof EnclosedExpr);
Expression expression2 = ((EnclosedExpr) right).getInner();
BinaryExpr binaryExpr2 = ((BinaryExpr) expression2);
assertEquals("age", toString(binaryExpr2.getLeft()));
assertEquals("20", toString(binaryExpr2.getRight()));
assertEquals(Operator.GREATER, binaryExpr2.getOperator());
}
use of com.github.javaparser.ast.Node in project checker-framework by typetools.
the class AnnotationEqualityVisitor method defaultAction.
@Override
public <T extends Node> void defaultAction(T node1, T node2) {
if (!(node1 instanceof NodeWithAnnotations<?>) || !(node2 instanceof NodeWithAnnotations<?>)) {
return;
}
// Comparing annotations with "equals" considers comments in the AST attached to the
// annotations. These should be ignored because two valid ASTs for the same file may differ in
// where comments appear, or whether they appear at all. So, to check if two nodes have the same
// annotations we create copies with all comments removed and compare their lists of annotations
// directly.
Node node1Copy = node1.clone();
Node node2Copy = node2.clone();
for (Comment comment : node1Copy.getAllContainedComments()) {
comment.remove();
}
for (Comment comment : node2Copy.getAllContainedComments()) {
comment.remove();
}
List<AnnotationExpr> node1annos = ((NodeWithAnnotations<?>) node1Copy).getAnnotations();
List<AnnotationExpr> node2annos = ((NodeWithAnnotations<?>) node2Copy).getAnnotations();
if (!node1annos.equals(node2annos)) {
annotationsMatch = false;
mismatchedNode1 = (NodeWithAnnotations<?>) node1;
mismatchedNode2 = (NodeWithAnnotations<?>) node2;
}
}
use of com.github.javaparser.ast.Node in project checker-framework by typetools.
the class RemoveAnnotationsForInference method isSuppressed.
// This approach searches upward to find all the active warning suppressions.
// An alternative, more efficient approach would be to track the current set of warning
// suppressions, using a stack.
// There are two problems with the alternative approach (and besides, this approach is fast
// enough as it is).
// 1. JavaParser sometimes visits members before the annotation, so there was not a chance to
// observe the annotation and place it on the suppression stack. This should be fixed for
// ModifierVisitor (but not for other visitors such as GenericListVisitorAdapter) in
// JavaParser release 3.19.0.
// 2. A user might write an annotation before @SuppressWarnings, as in:
// @Interned @SuppressWarnings("interning")
// The {@code @Interned} annotation is visited before the {@code @SuppressWarnings}
// annotation is. This could be addressed by searching just the parent's annotations.
/**
* Returns true if warnings about the given annotation are suppressed.
*
* <p>Its heuristic is to look for a {@code @SuppressWarnings} annotation on a containing program
* element, whose string is one of the elements of the annotation's fully-qualified name.
*
* @param arg an annotation
* @return true if warnings about the given annotation are suppressed
*/
private static boolean isSuppressed(AnnotationExpr arg) {
String name = arg.getNameAsString();
// If it's a simple name for which we know a fully-qualified name,
// try all fully-qualified names that it could expand to.
Collection<String> names;
if (simpleToFullyQualified.containsKey(name)) {
names = simpleToFullyQualified.get(name);
} else {
names = Collections.singletonList(name);
}
Iterator<Node> itor = new Node.ParentsVisitor(arg);
while (itor.hasNext()) {
Node n = itor.next();
if (n instanceof NodeWithAnnotations) {
for (AnnotationExpr ae : ((NodeWithAnnotations<?>) n).getAnnotations()) {
if (suppresses(ae, names)) {
return true;
}
}
}
}
return false;
}
Aggregations