use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class LexicalPreservingPrinter method interpret.
private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText) {
LexicalDifferenceCalculator.CalculatedSyntaxModel calculatedSyntaxModel = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(csm, node);
List<TokenTextElement> indentation = findIndentation(node);
boolean pendingIndentation = false;
for (CsmElement element : calculatedSyntaxModel.elements) {
if (pendingIndentation && !(element instanceof CsmToken && ((CsmToken) element).isNewLine())) {
indentation.forEach(nodeText::addElement);
}
pendingIndentation = false;
if (element instanceof LexicalDifferenceCalculator.CsmChild) {
nodeText.addChild(((LexicalDifferenceCalculator.CsmChild) element).getChild());
} else if (element instanceof CsmToken) {
CsmToken csmToken = (CsmToken) element;
nodeText.addToken(csmToken.getTokenType(), csmToken.getContent(node));
if (csmToken.isNewLine()) {
pendingIndentation = true;
}
} else if (element instanceof CsmMix) {
CsmMix csmMix = (CsmMix) element;
csmMix.getElements().forEach(e -> interpret(node, e, nodeText));
} else {
throw new UnsupportedOperationException(element.getClass().getSimpleName());
}
}
// so they have to be handled in a special way
if (node instanceof VariableDeclarator) {
VariableDeclarator variableDeclarator = (VariableDeclarator) node;
variableDeclarator.getParentNode().ifPresent(parent -> ((NodeWithVariables<?>) parent).getMaximumCommonType().ifPresent(mct -> {
int extraArrayLevels = variableDeclarator.getType().getArrayLevel() - mct.getArrayLevel();
for (int i = 0; i < extraArrayLevels; i++) {
nodeText.addElement(new TokenTextElement(LBRACKET));
nodeText.addElement(new TokenTextElement(RBRACKET));
}
}));
}
return nodeText;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class DumpVisitor method visit.
@Override
public void visit(final FieldDeclaration n, final Object arg) {
printOrphanCommentsBeforeThisChildNode(n);
printJavaComment(n.getComment(), arg);
printJavadoc(n.getJavaDoc(), arg);
printMemberAnnotations(n.getAnnotations(), arg);
printModifiers(n.getModifiers());
n.getType().accept(this, arg);
printer.print(" ");
for (final Iterator<VariableDeclarator> i = n.getVariables().iterator(); i.hasNext(); ) {
final VariableDeclarator var = i.next();
var.accept(this, arg);
if (i.hasNext()) {
printer.print(", ");
}
}
printer.print(";");
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class DumpVisitor method visit.
@Override
public void visit(final VariableDeclarationExpr n, final Object arg) {
printJavaComment(n.getComment(), arg);
printAnnotations(n.getAnnotations(), arg);
printModifiers(n.getModifiers());
n.getType().accept(this, arg);
printer.print(" ");
for (final Iterator<VariableDeclarator> i = n.getVars().iterator(); i.hasNext(); ) {
final VariableDeclarator v = i.next();
v.accept(this, arg);
if (i.hasNext()) {
printer.print(", ");
}
}
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class VoidVisitorAdapter method visit.
@Override
public void visit(final VariableDeclarationExpr n, final A arg) {
visitComment(n.getComment(), arg);
if (n.getAnnotations() != null) {
for (final AnnotationExpr a : n.getAnnotations()) {
a.accept(this, arg);
}
}
n.getType().accept(this, arg);
for (final VariableDeclarator v : n.getVars()) {
v.accept(this, arg);
}
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class ModifierVisitorAdapter method visit.
@Override
public Node visit(final VariableDeclarationExpr n, final A arg) {
visitComment(n, arg);
final List<AnnotationExpr> annotations = n.getAnnotations();
if (annotations != null) {
for (int i = 0; i < annotations.size(); i++) {
annotations.set(i, (AnnotationExpr) annotations.get(i).accept(this, arg));
}
removeNulls(annotations);
}
final Type type = (Type) n.getElementType().accept(this, arg);
if (type == null) {
return null;
}
n.setElementType(type);
final List<VariableDeclarator> vars = n.getVariables();
for (int i = 0; i < vars.size(); ) {
final VariableDeclarator decl = (VariableDeclarator) vars.get(i).accept(this, arg);
if (decl == null) {
vars.remove(i);
} else {
vars.set(i++, decl);
}
}
if (vars.isEmpty()) {
return null;
}
return n;
}
Aggregations