use of spoon.reflect.visitor.PrintingContext.Writable in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtFor.
@Override
public void visitCtFor(CtFor forLoop) {
enterCtStatement(forLoop);
printer.writeKeyword("for").writeSpace().writeSeparator("(");
List<CtStatement> st = forLoop.getForInit();
if (st.size() > 0) {
scan(st.get(0));
}
if (st.size() > 1) {
try (Writable _context = context.modify().noTypeDecl(true)) {
for (int i = 1; i < st.size(); i++) {
printer.writeSeparator(",").writeSpace();
scan(st.get(i));
}
}
}
printer.writeSeparator(";").writeSpace();
scan(forLoop.getExpression());
printer.writeSeparator(";");
if (!forLoop.getForUpdate().isEmpty()) {
printer.writeSpace();
}
try (ListPrinter lp = elementPrinterHelper.createListPrinter(false, null, false, true, ",", true, false, null)) {
for (CtStatement s : forLoop.getForUpdate()) {
lp.printSeparatorIfAppropriate();
scan(s);
}
}
printer.writeSeparator(")");
elementPrinterHelper.writeIfOrLoopBlock(forLoop.getBody());
}
use of spoon.reflect.visitor.PrintingContext.Writable in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtInvocation.
@Override
public <T> void visitCtInvocation(CtInvocation<T> invocation) {
enterCtStatement(invocation);
enterCtExpression(invocation);
if (invocation.getExecutable().isConstructor()) {
// It's a constructor (super or this)
elementPrinterHelper.writeActualTypeArguments(invocation.getExecutable());
CtType<?> parentType;
try {
parentType = invocation.getParent(CtType.class);
} catch (ParentNotInitializedException e) {
parentType = null;
}
if (parentType != null && parentType.getQualifiedName() != null && parentType.getQualifiedName().equals(invocation.getExecutable().getDeclaringType().getQualifiedName())) {
printer.writeKeyword("this");
} else {
if (invocation.getTarget() != null && !invocation.getTarget().isImplicit()) {
scan(invocation.getTarget());
printer.writeSeparator(".");
}
printer.writeKeyword("super");
}
} else {
// It's a method invocation
boolean isImported = this.isImported(invocation.getExecutable());
if (!isImported) {
try (Writable _context = context.modify()) {
if (invocation.getTarget() instanceof CtTypeAccess) {
_context.ignoreGenerics(true);
}
if (invocation.getTarget() != null && !invocation.getTarget().isImplicit()) {
scan(invocation.getTarget());
printer.writeSeparator(".");
}
}
}
elementPrinterHelper.writeActualTypeArguments(invocation);
if (env.isPreserveLineNumbers()) {
getPrinterHelper().adjustStartPosition(invocation);
}
printer.writeIdentifier(invocation.getExecutable().getSimpleName());
}
try (ListPrinter lp = elementPrinterHelper.createListPrinter(false, "(", false, false, ",", true, false, ")")) {
for (CtExpression<?> e : invocation.getArguments()) {
lp.printSeparatorIfAppropriate();
scan(e);
}
}
exitCtExpression(invocation);
}
use of spoon.reflect.visitor.PrintingContext.Writable in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtBinaryOperator.
@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
enterCtExpression(operator);
scan(operator.getLeftHandOperand());
printer.writeSpace();
printer.writeOperator(OperatorHelper.getOperatorText(operator.getKind()));
printer.writeSpace();
try (Writable _context = context.modify()) {
if (operator.getKind() == BinaryOperatorKind.INSTANCEOF) {
_context.forceWildcardGenerics(true);
}
scan(operator.getRightHandOperand());
}
exitCtExpression(operator);
}
use of spoon.reflect.visitor.PrintingContext.Writable in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtExecutableReferenceExpression.
@Override
public <T, E extends CtExpression<?>> void visitCtExecutableReferenceExpression(CtExecutableReferenceExpression<T, E> expression) {
enterCtExpression(expression);
try (Writable _context = context.modify()) {
if (expression.getExecutable().isStatic()) {
_context.ignoreGenerics(true);
}
scan(expression.getTarget());
}
printer.writeSeparator("::");
if (expression.getExecutable().isConstructor()) {
printer.writeKeyword("new");
} else {
printer.writeIdentifier(expression.getExecutable().getSimpleName());
}
exitCtExpression(expression);
}
use of spoon.reflect.visitor.PrintingContext.Writable in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method printCtFieldAccess.
private <T> void printCtFieldAccess(CtFieldAccess<T> f) {
enterCtExpression(f);
try (Writable _context = context.modify()) {
if ((f.getVariable().isStatic() || "class".equals(f.getVariable().getSimpleName())) && f.getTarget() instanceof CtTypeAccess) {
_context.ignoreGenerics(true);
}
CtExpression<?> target = f.getTarget();
if (target != null) {
boolean isInitializeStaticFinalField = isInitializeStaticFinalField(f.getTarget());
boolean isStaticField = f.getVariable().isStatic();
boolean isImportedField = this.isImported(f.getVariable());
if (!isInitializeStaticFinalField && !(isStaticField && isImportedField)) {
if (target.isImplicit() && !(f.getVariable().getFieldDeclaration() == null && this.env.getNoClasspath())) {
/*
* target is implicit, check whether there is no conflict with an local variable, catch variable or parameter
* in case of conflict make it explicit, otherwise the field access is shadowed by that variable.
* Search for potential variable declaration until we found a class which declares or inherits this field
*/
final CtField<?> field = f.getVariable().getFieldDeclaration();
if (field != null) {
final String fieldName = field.getSimpleName();
CtVariable<?> var = f.getVariable().map(new PotentialVariableDeclarationFunction(fieldName)).first();
if (var != field) {
// another variable declaration was found which is hiding the field declaration for this field access. Make the field access expicit
target.setImplicit(false);
}
} else {
// There is a model inconsistency
printer.writeComment(f.getFactory().createComment("ERROR: Missing field \"" + f.getVariable().getSimpleName() + "\", please check your model. The code may not compile.", CommentType.BLOCK)).writeSpace();
}
}
// the implicit drives the separator
if (!target.isImplicit()) {
scan(target);
printer.writeSeparator(".");
}
}
_context.ignoreStaticAccess(true);
}
scan(f.getVariable());
}
exitCtExpression(f);
}
Aggregations