use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.
the class AnnotationFileParser method processField.
/**
* Process the field declaration in decl: copy its annotations to {@code #annotationFileAnnos}.
*
* @param decl the declaration in the annotation file
* @param elt the element representing that same declaration
*/
private void processField(FieldDeclaration decl, VariableElement elt) {
if (skipNode(decl)) {
// and might refer to types that are not accessible.
return;
}
markAsFromStubFile(elt);
recordDeclAnnotation(elt, decl.getAnnotations(), decl);
// AnnotationFileParser parses all annotations in type annotation position as type annotations
recordDeclAnnotation(elt, decl.getElementType().getAnnotations(), decl);
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(), fieldVarDecl);
putMerge(annotationFileAnnos.atypes, elt, fieldType);
}
use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.
the class ToIndexFileConverter method visit.
@Override
public Void visit(VariableDeclarationExpr expr, AElement elem) {
List<AnnotationExpr> annos = expr.getAnnotations();
AMethod method = (AMethod) elem;
List<VariableDeclarator> varDecls = expr.getVariables();
for (int i = 0; i < varDecls.size(); i++) {
VariableDeclarator decl = varDecls.get(i);
LocalLocation loc = new LocalLocation(i, decl.getNameAsString());
AField field = method.body.locals.getVivify(loc);
visitType(expr.getCommonType(), field.type);
if (annos != null) {
for (AnnotationExpr annoExpr : annos) {
Annotation anno = extractAnnotation(annoExpr);
field.tlAnnotationsHere.add(anno);
}
}
}
return null;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.
the class ToIndexFileConverter method visit.
@Override
public Void visit(FieldDeclaration decl, AElement elem) {
for (VariableDeclarator v : decl.getVariables()) {
AClass clazz = (AClass) elem;
AField field = clazz.fields.getVivify(v.getNameAsString());
visitDecl(decl, field);
visitType(decl.getCommonType(), field.type);
}
return null;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitVariable.
@Override
public Void visitVariable(VariableTree javacTree, Node javaParserNode) {
// variable declarations, parameters, and fields.
if (javaParserNode instanceof VariableDeclarator) {
// JavaParser uses VariableDeclarator as parts of other declaration types like
// VariableDeclarationExpr when multiple variables may be declared.
VariableDeclarator node = (VariableDeclarator) javaParserNode;
processVariable(javacTree, node);
// Don't process the variable type when it's the Java keyword "var".
if (!node.getType().isVarType() && (!node.getType().isClassOrInterfaceType() || !node.getType().asClassOrInterfaceType().getName().asString().equals("var"))) {
javacTree.getType().accept(this, node.getType());
}
// The name expression can be null, even when a name exists.
if (javacTree.getNameExpression() != null) {
javacTree.getNameExpression().accept(this, node.getName());
}
visitOptional(javacTree.getInitializer(), node.getInitializer());
} else if (javaParserNode instanceof Parameter) {
Parameter node = (Parameter) javaParserNode;
processVariable(javacTree, node);
if (node.isVarArgs()) {
ArrayTypeTree arrayType;
// AnnotatedType depending on whether it has an annotation.
if (javacTree.getType().getKind() == Tree.Kind.ARRAY_TYPE) {
arrayType = (ArrayTypeTree) javacTree.getType();
} else {
AnnotatedTypeTree annotatedType = (AnnotatedTypeTree) javacTree.getType();
arrayType = (ArrayTypeTree) annotatedType.getUnderlyingType();
}
arrayType.getType().accept(this, node.getType());
} else {
// don't process them.
if (!node.getType().isUnknownType()) {
javacTree.getType().accept(this, node.getType());
}
}
// The name expression can be null, even when a name exists.
if (javacTree.getNameExpression() != null) {
javacTree.getNameExpression().accept(this, node.getName());
}
assert javacTree.getInitializer() == null;
} else if (javaParserNode instanceof ReceiverParameter) {
ReceiverParameter node = (ReceiverParameter) javaParserNode;
processVariable(javacTree, node);
javacTree.getType().accept(this, node.getType());
// The name expression can be null, even when a name exists.
if (javacTree.getNameExpression() != null) {
javacTree.getNameExpression().accept(this, node.getName());
}
assert javacTree.getInitializer() == null;
} else if (javaParserNode instanceof EnumConstantDeclaration) {
// In javac, an enum constant is expanded as a variable declaration initialized to a
// constuctor call.
EnumConstantDeclaration node = (EnumConstantDeclaration) javaParserNode;
processVariable(javacTree, node);
if (javacTree.getNameExpression() != null) {
javacTree.getNameExpression().accept(this, node.getName());
}
assert javacTree.getInitializer().getKind() == Tree.Kind.NEW_CLASS;
NewClassTree constructor = (NewClassTree) javacTree.getInitializer();
visitLists(constructor.getArguments(), node.getArguments());
if (constructor.getClassBody() != null) {
visitAnonymousClassBody(constructor.getClassBody(), node.getClassBody());
} else {
assert node.getClassBody().isEmpty();
}
} else {
throwUnexpectedNodeType(javacTree, javaParserNode);
}
return null;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method processStatements.
/**
* Given a matching sequence of statements for a block, visits each javac statement with its
* corresponding JavaParser statement, excluding synthetic javac trees like no-argument
* constructors.
*
* @param javacStatements sequence of javac trees for statements
* @param javaParserStatements sequence of JavaParser statements representing the same block as
* {@code javacStatements}
*/
private void processStatements(Iterable<? extends StatementTree> javacStatements, Iterable<Statement> javaParserStatements) {
PeekingIterator<StatementTree> javacIter = Iterators.peekingIterator(javacStatements.iterator());
PeekingIterator<Statement> javaParserIter = Iterators.peekingIterator(javaParserStatements.iterator());
while (javacIter.hasNext() || javaParserIter.hasNext()) {
// Skip synthetic javac super() calls by checking if the JavaParser statement matches.
if (javacIter.hasNext() && isDefaultSuperConstructorCall(javacIter.peek()) && (!javaParserIter.hasNext() || !isDefaultSuperConstructorCall(javaParserIter.peek()))) {
javacIter.next();
continue;
}
// VariableDeclarators. Match the declarators with the VariableTrees.
if (javaParserIter.hasNext() && javacIter.peek().getKind() == Tree.Kind.VARIABLE && javaParserIter.peek().isExpressionStmt() && javaParserIter.peek().asExpressionStmt().getExpression().isVariableDeclarationExpr()) {
for (VariableDeclarator decl : javaParserIter.next().asExpressionStmt().getExpression().asVariableDeclarationExpr().getVariables()) {
assert javacIter.hasNext();
javacIter.next().accept(this, decl);
}
continue;
}
assert javacIter.hasNext();
assert javaParserIter.hasNext();
javacIter.next().accept(this, javaParserIter.next());
}
assert !javacIter.hasNext();
assert !javaParserIter.hasNext();
}
Aggregations