Search in sources :

Example 11 with Writable

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

the class DefaultJavaPrettyPrinter method visitCtFieldReference.

@Override
public <T> void visitCtFieldReference(CtFieldReference<T> reference) {
    boolean isStatic = reference.getSimpleName().equals("class") || !reference.getSimpleName().equals("super") && reference.isStatic();
    boolean printType = true;
    if (reference.isFinal() && reference.isStatic()) {
        CtTypeReference<?> declTypeRef = reference.getDeclaringType();
        if (declTypeRef.isAnonymous()) {
            // never print anonymous class ref
            printType = false;
        } else {
            if (context.isInCurrentScope(declTypeRef)) {
                // do not printType if we are in scope of that type
                printType = false;
            }
        }
    }
    if (isStatic && printType && !context.ignoreStaticAccess()) {
        try (Writable _context = context.modify().ignoreGenerics(true)) {
            scan(reference.getDeclaringType());
        }
        printer.writeSeparator(".");
    }
    if (reference.getSimpleName().equals("class")) {
        printer.writeKeyword("class");
    } else {
        printer.writeIdentifier(reference.getSimpleName());
    }
}
Also used : Writable(spoon.reflect.visitor.PrintingContext.Writable)

Example 12 with Writable

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

the class ElementPrinterHelper method writeAnnotationElement.

/**
 * Writes an annotation element.
 */
public void writeAnnotationElement(Factory factory, Object value) {
    if (value instanceof CtTypeAccess) {
        prettyPrinter.scan((CtTypeAccess) value);
        printer.writeSeparator(".").writeKeyword("class");
    } else if (value instanceof CtFieldReference) {
        prettyPrinter.scan(((CtFieldReference<?>) value).getDeclaringType());
        printer.writeSeparator(".").writeIdentifier(((CtFieldReference<?>) value).getSimpleName());
    } else if (value instanceof CtElement) {
        prettyPrinter.scan((CtElement) value);
    } else if (value instanceof String) {
        printer.writeLiteral("\"" + LiteralHelper.getStringLiteral((String) value, true) + "\"");
    } else if (value instanceof Collection) {
        try (ListPrinter lp = createListPrinter(false, "{", false, true, ",", false, false, "}")) {
            for (Object obj : (Collection<?>) value) {
                lp.printSeparatorIfAppropriate();
                writeAnnotationElement(factory, obj);
            }
        }
    } else if (value instanceof Object[]) {
        try (ListPrinter lp = createListPrinter(false, "{", false, true, ",", false, false, "}")) {
            for (Object obj : (Object[]) value) {
                lp.printSeparatorIfAppropriate();
                writeAnnotationElement(factory, obj);
            }
        }
    } else if (value instanceof Enum) {
        try (Writable c = prettyPrinter.getContext().modify().ignoreGenerics(true)) {
            prettyPrinter.scan(factory.Type().createReference(((Enum<?>) value).getDeclaringClass()));
        }
        printer.writeSeparator(".");
        printer.writeIdentifier(value.toString());
    } else {
        // it probably prints, boolean, number, ...
        printer.writeLiteral(value.toString());
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtTypeAccess(spoon.reflect.code.CtTypeAccess) CtFieldReference(spoon.reflect.reference.CtFieldReference) Collection(java.util.Collection) 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