Search in sources :

Example 11 with CtTypeAccess

use of spoon.reflect.code.CtTypeAccess 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)

Example 12 with CtTypeAccess

use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.

the class JDTTreeBuilder method createParameterizedType.

private boolean createParameterizedType(TypeReference parameterizedTypeReference) {
    if (skipTypeInAnnotation) {
        return true;
    }
    CtTypeReference typeReference = references.buildTypeReference(parameterizedTypeReference, null);
    CtTypeAccess typeAccess = factory.Code().createTypeAccessWithoutCloningReference(typeReference);
    context.enter(typeAccess, parameterizedTypeReference);
    return true;
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) CtTypeAccess(spoon.reflect.code.CtTypeAccess)

Example 13 with CtTypeAccess

use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.

the class ParentExiter method visitCtNewArray.

@Override
public <T> void visitCtNewArray(CtNewArray<T> newArray) {
    if (childJDT instanceof TypeReference && child instanceof CtTypeAccess) {
        final ArrayAllocationExpression arrayAlloc = (ArrayAllocationExpression) jdtTreeBuilder.getContextBuilder().stack.peek().node;
        newArray.setType((CtArrayTypeReference) jdtTreeBuilder.getFactory().Type().createArrayReference(((CtTypeAccess) child).getAccessedType(), arrayAlloc.dimensions.length));
    } else if (child instanceof CtExpression) {
        if (isContainedInDimensionExpression()) {
            newArray.addDimensionExpression((CtExpression<Integer>) child);
        } else if (child instanceof CtNewArray && childJDT instanceof ArrayInitializer && jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof ArrayAllocationExpression) {
            newArray.setElements(((CtNewArray) child).getElements());
        } else {
            newArray.addElement((CtExpression) child);
        }
    }
}
Also used : ArrayAllocationExpression(org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression) CtExpression(spoon.reflect.code.CtExpression) CtTypeAccess(spoon.reflect.code.CtTypeAccess) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) CtIntersectionTypeReference(spoon.reflect.reference.CtIntersectionTypeReference) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) UnionTypeReference(org.eclipse.jdt.internal.compiler.ast.UnionTypeReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtNewArray(spoon.reflect.code.CtNewArray) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Example 14 with CtTypeAccess

use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.

the class TypeTest method testTypeAccessForTypeAccessInInstanceOf.

@Test
public void testTypeAccessForTypeAccessInInstanceOf() throws Exception {
    // contract: the right hand operator must be a CtTypeAccess.
    final String target = "./target/type";
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/type/testclasses");
    launcher.setSourceOutputDirectory(target);
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    final CtClass<Pozole> aPozole = launcher.getFactory().Class().get(Pozole.class);
    final CtMethod<?> eat = aPozole.getMethodsByName("eat").get(0);
    final List<CtTypeAccess<?>> typeAccesses = eat.getElements(new TypeFilter<CtTypeAccess<?>>(CtTypeAccess.class));
    assertEquals(2, typeAccesses.size());
    assertTrue(typeAccesses.get(0).getParent() instanceof CtBinaryOperator);
    assertEquals(BinaryOperatorKind.INSTANCEOF, ((CtBinaryOperator) typeAccesses.get(0).getParent()).getKind());
    assertEquals("a instanceof java.lang.String", typeAccesses.get(0).getParent().toString());
    assertTrue(typeAccesses.get(1).getParent() instanceof CtBinaryOperator);
    assertEquals(BinaryOperatorKind.INSTANCEOF, ((CtBinaryOperator) typeAccesses.get(1).getParent()).getKind());
    assertEquals("a instanceof java.util.Collection<?>", typeAccesses.get(1).getParent().toString());
}
Also used : Pozole(spoon.test.type.testclasses.Pozole) CtBinaryOperator(spoon.reflect.code.CtBinaryOperator) Launcher(spoon.Launcher) CtTypeAccess(spoon.reflect.code.CtTypeAccess) Test(org.junit.Test)

Aggregations

CtTypeAccess (spoon.reflect.code.CtTypeAccess)14 Test (org.junit.Test)6 Launcher (spoon.Launcher)5 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 CtExpression (spoon.reflect.code.CtExpression)3 CtType (spoon.reflect.declaration.CtType)3 Factory (spoon.reflect.factory.Factory)3 Writable (spoon.reflect.visitor.PrintingContext.Writable)3 Pozole (spoon.test.type.testclasses.Pozole)3 Collection (java.util.Collection)2 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)2 CtFieldRead (spoon.reflect.code.CtFieldRead)2 CtNewArray (spoon.reflect.code.CtNewArray)2 CtFieldReference (spoon.reflect.reference.CtFieldReference)2 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)1 Field (java.lang.reflect.Field)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ArrayAllocationExpression (org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression)1