use of com.sun.source.tree.ExpressionTree in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitAnnotation.
@Override
public Void visitAnnotation(AnnotationTree javacTree, Node javaParserNode) {
// as @MyAnno(value="myArg") which has a single element argument list with an assignment.
if (javaParserNode instanceof MarkerAnnotationExpr) {
processAnnotation(javacTree, (MarkerAnnotationExpr) javaParserNode);
} else if (javaParserNode instanceof SingleMemberAnnotationExpr) {
SingleMemberAnnotationExpr node = (SingleMemberAnnotationExpr) javaParserNode;
processAnnotation(javacTree, node);
assert javacTree.getArguments().size() == 1;
ExpressionTree value = javacTree.getArguments().get(0);
assert value instanceof AssignmentTree;
AssignmentTree assignment = (AssignmentTree) value;
assert assignment.getVariable().getKind() == Tree.Kind.IDENTIFIER;
assert ((IdentifierTree) assignment.getVariable()).getName().contentEquals("value");
assignment.getExpression().accept(this, node.getMemberValue());
} else if (javaParserNode instanceof NormalAnnotationExpr) {
NormalAnnotationExpr node = (NormalAnnotationExpr) javaParserNode;
processAnnotation(javacTree, node);
assert javacTree.getArguments().size() == node.getPairs().size();
Iterator<MemberValuePair> argIter = node.getPairs().iterator();
for (ExpressionTree arg : javacTree.getArguments()) {
assert arg instanceof AssignmentTree;
AssignmentTree assignment = (AssignmentTree) arg;
IdentifierTree memberName = (IdentifierTree) assignment.getVariable();
MemberValuePair javaParserArg = argIter.next();
assert memberName.getName().contentEquals(javaParserArg.getNameAsString());
assignment.getExpression().accept(this, javaParserArg.getValue());
}
} else {
throwUnexpectedNodeType(javacTree, javaParserNode);
}
return null;
}
use of com.sun.source.tree.ExpressionTree in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitMethodInvocation.
@Override
public Void visitMethodInvocation(MethodInvocationTree javacTree, Node javaParserNode) {
if (javaParserNode instanceof MethodCallExpr) {
MethodCallExpr node = (MethodCallExpr) javaParserNode;
processMethodInvocation(javacTree, node);
// JavaParser the type arguments will have the none Optional value.
assert javacTree.getTypeArguments().isEmpty() != node.getTypeArguments().isPresent();
if (!javacTree.getTypeArguments().isEmpty()) {
visitLists(javacTree.getTypeArguments(), node.getTypeArguments().get());
}
// In JavaParser, the method name itself and receiver are stored as fields of the
// invocation itself, but in javac they might be combined into one MemberSelectTree.
// That member select may also be a single IdentifierTree if no receiver was written.
// This requires one layer of unnesting.
ExpressionTree methodSelect = javacTree.getMethodSelect();
if (methodSelect.getKind() == Tree.Kind.IDENTIFIER) {
methodSelect.accept(this, node.getName());
} else if (methodSelect.getKind() == Tree.Kind.MEMBER_SELECT) {
MemberSelectTree selection = (MemberSelectTree) methodSelect;
assert node.getScope().isPresent();
selection.getExpression().accept(this, node.getScope().get());
} else {
throw new BugInCF("Unexpected method selection type: %s", methodSelect);
}
visitLists(javacTree.getArguments(), node.getArguments());
} else if (javaParserNode instanceof ExplicitConstructorInvocationStmt) {
ExplicitConstructorInvocationStmt node = (ExplicitConstructorInvocationStmt) javaParserNode;
processMethodInvocation(javacTree, node);
assert javacTree.getTypeArguments().isEmpty() != node.getTypeArguments().isPresent();
if (!javacTree.getTypeArguments().isEmpty()) {
visitLists(javacTree.getTypeArguments(), node.getTypeArguments().get());
}
visitLists(javacTree.getArguments(), node.getArguments());
} else {
throwUnexpectedNodeType(javacTree, javaParserNode);
}
return null;
}
use of com.sun.source.tree.ExpressionTree in project checker-framework by typetools.
the class ExpectedTreesVisitor method visitClass.
@Override
public Void visitClass(ClassTree tree, Void p) {
defaultAction(tree);
scan(tree.getModifiers(), p);
scan(tree.getTypeParameters(), p);
scan(tree.getExtendsClause(), p);
scan(tree.getImplementsClause(), p);
if (tree.getKind() == Tree.Kind.ENUM) {
// instance of an enum.
for (Tree member : tree.getMembers()) {
member.accept(this, p);
if (member.getKind() != Tree.Kind.VARIABLE) {
continue;
}
VariableTree variable = (VariableTree) member;
ExpressionTree initializer = variable.getInitializer();
if (initializer == null || initializer.getKind() != Tree.Kind.NEW_CLASS) {
continue;
}
NewClassTree constructor = (NewClassTree) initializer;
if (constructor.getIdentifier().getKind() != Tree.Kind.IDENTIFIER) {
continue;
}
IdentifierTree name = (IdentifierTree) constructor.getIdentifier();
if (name.getName().contentEquals(tree.getSimpleName())) {
trees.remove(variable.getType());
trees.remove(constructor);
trees.remove(constructor.getIdentifier());
}
}
// RECORD was added in Java 14, so use string comparison to be JDK 8,11 compatible:
} else if (tree.getKind().name().equals("RECORD")) {
// A record like:
// record MyRec(String myField) {}
// will be expanded by javac to:
// class MyRec {
// MyRec(String myField) {
// super();
// }
// private final String myField;
// }
// So the constructor and the field declarations have no matching trees in the JavaParser
// node, and we must remove those trees (and their subtrees) from the `trees` field.
TreeScannerWithDefaults removeAllVisitor = new TreeScannerWithDefaults() {
@Override
public void defaultAction(Tree node) {
trees.remove(node);
}
};
for (Tree member : tree.getMembers()) {
scan(member, p);
if (TreeUtils.isAutoGeneratedRecordMember(member)) {
member.accept(removeAllVisitor, null);
} else {
// the parameters. These trees also don't have a match:
if (member.getKind() == Tree.Kind.METHOD) {
MethodTree methodTree = (MethodTree) member;
if (TreeUtils.isCompactCanonicalRecordConstructor(methodTree)) {
for (VariableTree canonicalParameter : methodTree.getParameters()) {
canonicalParameter.accept(removeAllVisitor, null);
}
}
}
}
}
} else {
scan(tree.getMembers(), p);
}
return null;
}
use of com.sun.source.tree.ExpressionTree in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitWhileLoop.
@Override
public Void visitWhileLoop(WhileLoopTree javacTree, Node javaParserNode) {
WhileStmt node = castNode(WhileStmt.class, javaParserNode, javacTree);
processWhileLoop(javacTree, node);
// While loop conditions are always parenthesized in javac but never in JavaParser.
assert javacTree.getCondition().getKind() == Tree.Kind.PARENTHESIZED;
ExpressionTree condition = ((ParenthesizedTree) javacTree.getCondition()).getExpression();
condition.accept(this, node.getCondition());
javacTree.getStatement().accept(this, node.getBody());
return null;
}
use of com.sun.source.tree.ExpressionTree in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitDoWhileLoop.
@Override
public Void visitDoWhileLoop(DoWhileLoopTree javacTree, Node javaParserNode) {
DoStmt node = castNode(DoStmt.class, javaParserNode, javacTree);
processDoWhileLoop(javacTree, node);
// In javac the condition is parenthesized but not in JavaParser.
ExpressionTree condition = ((ParenthesizedTree) javacTree.getCondition()).getExpression();
condition.accept(this, node.getCondition());
javacTree.getStatement().accept(this, node.getBody());
return null;
}
Aggregations