Search in sources :

Example 6 with CtArrayTypeReference

use of spoon.reflect.reference.CtArrayTypeReference in project spoon by INRIA.

the class EqualsChecker method visitCtArrayTypeReference.

@Override
public <T> void visitCtArrayTypeReference(CtArrayTypeReference<T> e) {
    final CtArrayTypeReference peek = (CtArrayTypeReference) this.other;
    if (e.getDimensionCount() != peek.getDimensionCount()) {
        isNotEqual = true;
        return;
    }
    super.visitCtArrayTypeReference(e);
}
Also used : CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference)

Example 7 with CtArrayTypeReference

use of spoon.reflect.reference.CtArrayTypeReference in project spoon by INRIA.

the class LambdaTest method testTypeParameterWithImplicitArrayType.

@Test
public void testTypeParameterWithImplicitArrayType() throws Exception {
    final CtLambda<?> lambda = panini.getElements(new TypeFilter<CtLambda<?>>(CtLambda.class)).get(0);
    assertEquals(1, lambda.getParameters().size());
    final CtParameter<?> ctParameter = lambda.getParameters().get(0);
    assertEquals("a", ctParameter.getSimpleName());
    assertTrue(ctParameter.getType().isImplicit());
    assertEquals("", ctParameter.getType().toString());
    assertEquals("Object[]", ctParameter.getType().getSimpleName());
    final CtArrayTypeReference typeParameter = (CtArrayTypeReference) ctParameter.getType();
    assertTrue(typeParameter.getComponentType().isImplicit());
    assertEquals("", typeParameter.getComponentType().toString());
    assertEquals("Object", typeParameter.getComponentType().getSimpleName());
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) Test(org.junit.Test)

Example 8 with CtArrayTypeReference

use of spoon.reflect.reference.CtArrayTypeReference in project spoon by INRIA.

the class ParentTest method testParentSetInSetter.

@Test
// too fragile because of conventions
@Ignore
public void testParentSetInSetter() throws Exception {
    // contract: Check that all setters protect their parameter.
    final Launcher launcher = new Launcher();
    final Factory factory = launcher.getFactory();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.getEnvironment().setNoClasspath(true);
    // interfaces.
    launcher.addInputResource("./src/main/java/spoon/reflect/code");
    launcher.addInputResource("./src/main/java/spoon/reflect/declaration");
    launcher.addInputResource("./src/main/java/spoon/reflect/reference");
    // implementations.
    launcher.addInputResource("./src/main/java/spoon/support/reflect/code");
    launcher.addInputResource("./src/main/java/spoon/support/reflect/declaration");
    launcher.addInputResource("./src/main/java/spoon/support/reflect/reference");
    // Utils.
    launcher.addInputResource("./src/test/java/spoon/reflect/ast/");
    launcher.buildModel();
    // Asserts.
    new IntercessionScanner(launcher.getFactory()) {

        @Override
        protected boolean isToBeProcessed(CtMethod<?> candidate) {
            return (// 
            candidate.getSimpleName().startsWith("set") || // 
            candidate.getSimpleName().startsWith("add")) && // 
            candidate.hasModifier(ModifierKind.PUBLIC) && // 
            takeSetterForCtElement(candidate) && // 
            avoidInterfaces(candidate) && avoidThrowUnsupportedOperationException(candidate);
        }

        @Override
        public void process(CtMethod<?> element) {
            if (element.getAnnotation(UnsettableProperty.class) != null) {
                // we don't check the contracts for unsettable setters
                return;
            }
            if (element.getSimpleName().startsWith("add")) {
                checkAddStrategy(element);
            } else {
                checkSetStrategy(element);
            }
        }

        private void checkAddStrategy(CtMethod<?> element) {
            final CtStatement statement = element.getBody().getStatement(0);
            if (!(statement instanceof CtIf)) {
                fail("First statement should be an if to check the parameter of the setter." + element.getSignature() + " declared in " + element.getDeclaringType().getQualifiedName());
            }
            if (!createCheckNull(element.getParameters().get(0)).equals(((CtIf) statement).getCondition())) {
                fail("Condition should test if the parameter is null. The condition was " + ((CtIf) statement).getCondition() + "in " + element.getSignature() + " declared in " + element.getDeclaringType().getQualifiedName());
            }
        }

        private void checkSetStrategy(CtMethod<?> element) {
            final CtTypeReference<?> type = element.getParameters().get(0).getType();
            if (!COLLECTIONS.contains(type) && !(type instanceof CtArrayTypeReference)) {
                CtInvocation<?> setParent = searchSetParent(element.getBody());
                if (setParent == null) {
                    return;
                }
                try {
                    if (setParent.getParent(CtIf.class) == null) {
                        fail("Missing condition in " + element.getSignature() + " declared in the class " + element.getDeclaringType().getQualifiedName());
                    }
                } catch (ParentNotInitializedException e) {
                    fail("Missing parent condition in " + element.getSignature() + " declared in the class " + element.getDeclaringType().getQualifiedName());
                }
            }
        }

        /**
         * Creates <code>parameter == null</code>.
         *
         * @param ctParameter <code>parameter</code>
         */
        private CtBinaryOperator<Boolean> createCheckNull(CtParameter<?> ctParameter) {
            final CtLiteral nullLiteral = factory.Code().createLiteral(null);
            nullLiteral.setType(factory.Type().NULL_TYPE.clone());
            final CtBinaryOperator<Boolean> operator = // 
            factory.Code().createBinaryOperator(// 
            factory.Code().createVariableRead(ctParameter.getReference(), true), nullLiteral, BinaryOperatorKind.EQ);
            operator.setType(factory.Type().BOOLEAN_PRIMITIVE);
            return operator;
        }

        private CtInvocation<?> searchSetParent(CtBlock<?> body) {
            final List<CtInvocation<?>> ctInvocations = body.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

                @Override
                public boolean matches(CtInvocation<?> element) {
                    return "setParent".equals(element.getExecutable().getSimpleName()) && super.matches(element);
                }
            });
            return ctInvocations.size() > 0 ? ctInvocations.get(0) : null;
        }
    }.scan(launcher.getModel().getRootPackage());
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtBinaryOperator(spoon.reflect.code.CtBinaryOperator) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) CtIf(spoon.reflect.code.CtIf) CtInvocation(spoon.reflect.code.CtInvocation) CtLiteral(spoon.reflect.code.CtLiteral) CtStatement(spoon.reflect.code.CtStatement) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) CtStatementList(spoon.reflect.code.CtStatementList) List(java.util.List) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) IntercessionScanner(spoon.test.intercession.IntercessionScanner) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with CtArrayTypeReference

use of spoon.reflect.reference.CtArrayTypeReference 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 CtArrayTypeReference

use of spoon.reflect.reference.CtArrayTypeReference in project spoon by INRIA.

the class ArraysTest method testInitializeWithNewArray.

@Test
public void testInitializeWithNewArray() throws Exception {
    Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/resources/noclasspath/Foo.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    CtType<Object> aType = launcher.getFactory().Type().get("com.example.Foo");
    final List<CtNewArray> elements = aType.getElements(new TypeFilter<>(CtNewArray.class));
    assertEquals(2, elements.size());
    final CtNewArray attribute = elements.get(0);
    assertEquals(1, attribute.getDimensionExpressions().size());
    assertEquals(0, ((CtLiteral) attribute.getDimensionExpressions().get(0)).getValue());
    assertTrue(attribute.getType() instanceof CtArrayTypeReference);
    assertEquals("new java.lang.String[0]", attribute.toString());
    final CtNewArray local = elements.get(1);
    assertEquals(1, local.getDimensionExpressions().size());
    assertTrue(local.getDimensionExpressions().get(0) instanceof CtInvocation);
    assertTrue(local.getType() instanceof CtArrayTypeReference);
    assertEquals("new com.example.Type[list.size()]", local.toString());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) CtNewArray(spoon.reflect.code.CtNewArray) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) Test(org.junit.Test)

Aggregations

CtArrayTypeReference (spoon.reflect.reference.CtArrayTypeReference)13 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 Test (org.junit.Test)4 List (java.util.List)2 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)2 TypeVariableBinding (org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding)2 WildcardBinding (org.eclipse.jdt.internal.compiler.lookup.WildcardBinding)2 Launcher (spoon.Launcher)2 CtExpression (spoon.reflect.code.CtExpression)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtNewArray (spoon.reflect.code.CtNewArray)2 CtMethod (spoon.reflect.declaration.CtMethod)2 ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)2 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)2 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 ArrayAllocationExpression (org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression)1 ArrayInitializer (org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)1