Search in sources :

Example 6 with Writable

use of spoon.reflect.visitor.PrintingContext.Writable in project spoon by INRIA.

the class DefaultJavaPrettyPrinter method visitCtAnnotationFieldAccess.

@Override
public <T> void visitCtAnnotationFieldAccess(CtAnnotationFieldAccess<T> annotationFieldAccess) {
    enterCtExpression(annotationFieldAccess);
    try (Writable _context = context.modify()) {
        if (annotationFieldAccess.getTarget() != null) {
            scan(annotationFieldAccess.getTarget());
            printer.writeSeparator(".");
            _context.ignoreStaticAccess(true);
        }
        _context.ignoreGenerics(true);
        scan(annotationFieldAccess.getVariable());
        printer.writeSeparator("(").writeSeparator(")");
    }
    exitCtExpression(annotationFieldAccess);
}
Also used : Writable(spoon.reflect.visitor.PrintingContext.Writable)

Example 7 with Writable

use of spoon.reflect.visitor.PrintingContext.Writable in project spoon by INRIA.

the class DefaultJavaPrettyPrinter method printConstructorCall.

private <T> void printConstructorCall(CtConstructorCall<T> ctConstructorCall) {
    try (Writable _context = context.modify()) {
        if (ctConstructorCall.getTarget() != null) {
            scan(ctConstructorCall.getTarget());
            printer.writeSeparator(".");
            _context.ignoreEnclosingClass(true);
        }
        if (hasDeclaringTypeWithGenerics(ctConstructorCall.getType())) {
            _context.ignoreEnclosingClass(true);
        }
        printer.writeKeyword("new").writeSpace();
        if (ctConstructorCall.getActualTypeArguments().size() > 0) {
            elementPrinterHelper.writeActualTypeArguments(ctConstructorCall);
        }
        scan(ctConstructorCall.getType());
    }
    try (ListPrinter lp = elementPrinterHelper.createListPrinter(false, "(", false, false, ",", true, false, ")")) {
        for (CtCodeElement exp : ctConstructorCall.getArguments()) {
            lp.printSeparatorIfAppropriate();
            scan(exp);
        }
    }
}
Also used : CtCodeElement(spoon.reflect.code.CtCodeElement) Writable(spoon.reflect.visitor.PrintingContext.Writable)

Example 8 with Writable

use of spoon.reflect.visitor.PrintingContext.Writable in project spoon by INRIA.

the class DefaultJavaPrettyPrinter method visitCtMethod.

@Override
public <T> void visitCtMethod(CtMethod<T> m) {
    elementPrinterHelper.writeComment(m);
    elementPrinterHelper.visitCtNamedElement(m, sourceCompilationUnit);
    elementPrinterHelper.writeModifiers(m);
    if (m.isDefaultMethod()) {
        printer.writeKeyword("default").writeSpace();
    }
    elementPrinterHelper.writeFormalTypeParameters(m);
    if (m.getFormalCtTypeParameters().size() > 0) {
        printer.writeSpace();
    }
    try (Writable _context = context.modify().ignoreGenerics(false)) {
        scan(m.getType());
    }
    printer.writeSpace();
    printer.writeIdentifier(m.getSimpleName());
    elementPrinterHelper.writeExecutableParameters(m);
    elementPrinterHelper.writeThrowsClause(m);
    if (m.getBody() != null) {
        printer.writeSpace();
        scan(m.getBody());
        if (m.getBody().getPosition() != null) {
            if (m.getBody().getPosition().getCompilationUnit() == sourceCompilationUnit) {
                if (m.getBody().getStatements().isEmpty() || !(m.getBody().getStatements().get(m.getBody().getStatements().size() - 1) instanceof CtReturn)) {
                    getPrinterHelper().putLineNumberMapping(m.getBody().getPosition().getEndLine());
                }
            } else {
                getPrinterHelper().undefineLine();
            }
        } else {
            getPrinterHelper().undefineLine();
        }
    } else {
        printer.writeSeparator(";");
    }
}
Also used : CtReturn(spoon.reflect.code.CtReturn) Writable(spoon.reflect.visitor.PrintingContext.Writable)

Example 9 with Writable

use of spoon.reflect.visitor.PrintingContext.Writable in project spoon by INRIA.

the class DefaultJavaPrettyPrinter method visitCtNewArray.

@Override
@SuppressWarnings("rawtypes")
public <T> void visitCtNewArray(CtNewArray<T> newArray) {
    enterCtExpression(newArray);
    boolean isNotInAnnotation;
    try {
        isNotInAnnotation = (newArray.getParent(CtAnnotationType.class) == null) && (newArray.getParent(CtAnnotation.class) == null);
    } catch (ParentNotInitializedException e) {
        isNotInAnnotation = true;
    }
    if (isNotInAnnotation) {
        CtTypeReference<?> ref = newArray.getType();
        if (ref != null) {
            printer.writeKeyword("new").writeSpace();
        }
        try (Writable _context = context.modify().skipArray(true)) {
            scan(ref);
        }
        for (int i = 0; ref instanceof CtArrayTypeReference; i++) {
            printer.writeSeparator("[");
            if (newArray.getDimensionExpressions().size() > i) {
                CtExpression<Integer> e = newArray.getDimensionExpressions().get(i);
                scan(e);
            }
            printer.writeSeparator("]");
            ref = ((CtArrayTypeReference) ref).getComponentType();
        }
    }
    if (newArray.getDimensionExpressions().size() == 0) {
        try (ListPrinter lp = elementPrinterHelper.createListPrinter(false, "{", true, false, ",", true, true, "}")) {
            for (CtExpression e : newArray.getElements()) {
                lp.printSeparatorIfAppropriate();
                scan(e);
            }
            elementPrinterHelper.writeComment(newArray, CommentOffset.INSIDE);
        }
    }
    elementPrinterHelper.writeComment(newArray, CommentOffset.AFTER);
    exitCtExpression(newArray);
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtExpression(spoon.reflect.code.CtExpression) Writable(spoon.reflect.visitor.PrintingContext.Writable) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference)

Example 10 with Writable

use of spoon.reflect.visitor.PrintingContext.Writable in project spoon by INRIA.

the class DefaultJavaPrettyPrinter method visitCtTypeReference.

private void visitCtTypeReference(CtTypeReference<?> ref, boolean withGenerics) {
    if (ref.isImplicit()) {
        return;
    }
    if (ref.isPrimitive()) {
        elementPrinterHelper.writeAnnotations(ref);
        printer.writeKeyword(ref.getSimpleName());
        return;
    }
    boolean isInner = ref.getDeclaringType() != null;
    if (isInner) {
        if (!context.ignoreEnclosingClass() && !ref.isLocalType()) {
            // compute visible type which can be used to print access path to ref
            CtTypeReference<?> accessType = ref.getAccessType();
            if (!accessType.isAnonymous()) {
                try (Writable _context = context.modify()) {
                    if (!withGenerics) {
                        _context.ignoreGenerics(true);
                    }
                    scan(accessType);
                }
                printer.writeSeparator(".");
            }
        }
        // ?? are these annotations on correct place ??
        elementPrinterHelper.writeAnnotations(ref);
        if (ref.isLocalType()) {
            printer.writeIdentifier(ref.getSimpleName().replaceAll("^[0-9]*", ""));
        } else {
            printer.writeIdentifier(ref.getSimpleName());
        }
    } else {
        if (ref.getPackage() != null && printQualified(ref)) {
            if (!ref.getPackage().isUnnamedPackage()) {
                scan(ref.getPackage());
                printer.writeSeparator(CtPackage.PACKAGE_SEPARATOR);
            }
        }
        elementPrinterHelper.writeAnnotations(ref);
        printer.writeIdentifier(ref.getSimpleName());
    }
    if (withGenerics && !context.ignoreGenerics()) {
        try (Writable _context = context.modify().ignoreEnclosingClass(false)) {
            elementPrinterHelper.writeActualTypeArguments(ref);
        }
    }
}
Also used : Writable(spoon.reflect.visitor.PrintingContext.Writable)

Aggregations

Writable (spoon.reflect.visitor.PrintingContext.Writable)12 CtTypeAccess (spoon.reflect.code.CtTypeAccess)3 ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)2 Collection (java.util.Collection)1 CtCodeElement (spoon.reflect.code.CtCodeElement)1 CtExpression (spoon.reflect.code.CtExpression)1 CtReturn (spoon.reflect.code.CtReturn)1 CtStatement (spoon.reflect.code.CtStatement)1 CtElement (spoon.reflect.declaration.CtElement)1 CtType (spoon.reflect.declaration.CtType)1 CtArrayTypeReference (spoon.reflect.reference.CtArrayTypeReference)1 CtFieldReference (spoon.reflect.reference.CtFieldReference)1 PotentialVariableDeclarationFunction (spoon.reflect.visitor.filter.PotentialVariableDeclarationFunction)1