use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitClassMembers.
/**
* Given a list of class members for javac and JavaParser, visits each javac member with its
* corresponding JavaParser member. Skips synthetic javac members.
*
* @param javacMembers a list of trees forming the members of a javac {@code ClassTree}
* @param javaParserMembers a list of nodes forming the members of a JavaParser {@code
* ClassOrInterfaceDeclaration} or an {@code ObjectCreationExpr} with an anonymous class body
* that corresponds to {@code javacMembers}
*/
private void visitClassMembers(List<? extends Tree> javacMembers, List<BodyDeclaration<?>> javaParserMembers) {
PeekingIterator<Tree> javacIter = Iterators.peekingIterator(javacMembers.iterator());
PeekingIterator<BodyDeclaration<?>> javaParserIter = Iterators.peekingIterator(javaParserMembers.iterator());
while (javacIter.hasNext() || javaParserIter.hasNext()) {
// Skip javac's synthetic no-argument constructors.
if (javacIter.hasNext() && isNoArgumentConstructor(javacIter.peek()) && (!javaParserIter.hasNext() || !isNoArgumentConstructor(javaParserIter.peek()))) {
javacIter.next();
continue;
}
// VariableDeclarators. Match the declarators with the VariableTrees.
if (javaParserIter.hasNext() && javaParserIter.peek().isFieldDeclaration()) {
for (VariableDeclarator decl : javaParserIter.next().asFieldDeclaration().getVariables()) {
assert javacIter.hasNext();
assert javacIter.peek().getKind() == Tree.Kind.VARIABLE;
javacIter.next().accept(this, decl);
}
continue;
}
assert javacIter.hasNext();
assert javaParserIter.hasNext();
javacIter.next().accept(this, javaParserIter.next());
}
assert !javacIter.hasNext();
assert !javaParserIter.hasNext();
}
use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitTry.
@Override
public Void visitTry(TryTree javacTree, Node javaParserNode) {
TryStmt node = castNode(TryStmt.class, javaParserNode, javacTree);
processTry(javacTree, node);
Iterator<? extends Tree> javacResources = javacTree.getResources().iterator();
for (Expression resource : node.getResources()) {
if (resource.isVariableDeclarationExpr()) {
for (VariableDeclarator declarator : resource.asVariableDeclarationExpr().getVariables()) {
assert javacResources.hasNext();
javacResources.next().accept(this, declarator);
}
} else {
assert javacResources.hasNext();
javacResources.next().accept(this, resource);
}
}
javacTree.getBlock().accept(this, node.getTryBlock());
visitLists(javacTree.getCatches(), node.getCatchClauses());
visitOptional(javacTree.getFinallyBlock(), node.getFinallyBlock());
return null;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitForLoop.
@Override
public Void visitForLoop(ForLoopTree javacTree, Node javaParserNode) {
ForStmt node = castNode(ForStmt.class, javaParserNode, javacTree);
processForLoop(javacTree, node);
Iterator<? extends StatementTree> javacInitializers = javacTree.getInitializer().iterator();
for (Expression initializer : node.getInitialization()) {
if (initializer.isVariableDeclarationExpr()) {
for (VariableDeclarator declarator : initializer.asVariableDeclarationExpr().getVariables()) {
assert javacInitializers.hasNext();
javacInitializers.next().accept(this, declarator);
}
} else if (initializer.isAssignExpr()) {
ExpressionStatementTree statement = (ExpressionStatementTree) javacInitializers.next();
statement.getExpression().accept(this, initializer);
} else {
assert javacInitializers.hasNext();
javacInitializers.next().accept(this, initializer);
}
}
assert !javacInitializers.hasNext();
visitOptional(javacTree.getCondition(), node.getCompare());
// the javac statements must be unwrapped.
assert javacTree.getUpdate().size() == node.getUpdate().size();
Iterator<Expression> javaParserUpdates = node.getUpdate().iterator();
for (ExpressionStatementTree javacUpdate : javacTree.getUpdate()) {
// Match the inner javac expression with the JavaParser expression.
javacUpdate.getExpression().accept(this, javaParserUpdates.next());
}
javacTree.getStatement().accept(this, node.getBody());
return null;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.
the class StubParser method processField.
private void processField(FieldDeclaration decl, VariableElement elt) {
addDeclAnnotations(declAnnos, elt);
annotateDecl(declAnnos, elt, decl.getAnnotations());
// StubParser parses all annotations in type annotation position as type annotations
annotateDecl(declAnnos, elt, decl.getElementType().getAnnotations());
AnnotatedTypeMirror fieldType = atypeFactory.fromElement(elt);
VariableDeclarator fieldVarDecl = null;
String eltName = elt.getSimpleName().toString();
for (VariableDeclarator var : decl.getVariables()) {
if (var.getName().toString().equals(eltName)) {
fieldVarDecl = var;
break;
}
}
assert fieldVarDecl != null;
annotate(fieldType, fieldVarDecl.getType(), decl.getAnnotations());
putNew(atypes, elt, fieldType);
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class NodeWithVariables method getElementType.
/**
* Returns the element type.
* <br/>For <code>int a;</code> this is int.
* <br/>For <code>int a,b,c,d;</code> this is also int.
* <br/>For <code>int a,b[],c;</code> this is also int. Note: no mention of b being an array.
* <br/>For <code>int a,b;</code>, then doing setType(String) on b, then calling getElementType(). This is an assertion error. It is also a situation that you don't really want.
*/
default Type getElementType() {
NodeList<VariableDeclarator> variables = getVariables();
if (variables.isEmpty()) {
throw new AssertionError("There is no element type since there are no variables.");
}
Type type = variables.get(0).getType().getElementType();
for (int i = 1; i < variables.size(); i++) {
if (!variables.get(i).getType().getElementType().equals(type)) {
throw new AssertionError("The variables do not have a common type.");
}
}
return type;
}
Aggregations