Search in sources :

Example 6 with CtConstructorCall

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

the class AnnotationValuesTest method testValuesOnJava8Annotation.

@Test
public void testValuesOnJava8Annotation() throws Exception {
    CtType<AnnotationValues> aClass = buildClass(AnnotationValues.class);
    CtConstructorCall aConstructorCall = aClass.getMethod("method").getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    CtAnnotation<?> ctAnnotation = on(aConstructorCall.getType()).giveMeAnnotation(AnnotationValues.Annotation.class);
    on(ctAnnotation).giveMeAnnotationValue("integer").isTypedBy(CtLiteral.class);
    on(ctAnnotation).giveMeAnnotationValue("integers").isTypedBy(CtNewArray.class);
    on(ctAnnotation).giveMeAnnotationValue("string").isTypedBy(CtLiteral.class);
    on(ctAnnotation).giveMeAnnotationValue("strings").isTypedBy(CtLiteral.class);
    on(ctAnnotation).giveMeAnnotationValue("clazz").isTypedBy(CtFieldAccess.class);
    on(ctAnnotation).giveMeAnnotationValue("classes").isTypedBy(CtNewArray.class);
    on(ctAnnotation).giveMeAnnotationValue("b").isTypedBy(CtLiteral.class);
    on(ctAnnotation).giveMeAnnotationValue("e").isTypedBy(CtFieldAccess.class);
    on(ctAnnotation).giveMeAnnotationValue("ia").isTypedBy(CtAnnotation.class);
    on(ctAnnotation).giveMeAnnotationValue("ias").isTypedBy(CtNewArray.class);
}
Also used : CtConstructorCall(spoon.reflect.code.CtConstructorCall) AnnotationValues(spoon.test.annotation.testclasses.AnnotationValues) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 7 with CtConstructorCall

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

the class SpoonArchitectureEnforcerTest method testSrcMainJava.

// this test contains all the architectural rules that are valid for the whole src/main/java
// we put them in the same test in order to only build the full model once
@Test
public void testSrcMainJava() throws Exception {
    Launcher spoon = new Launcher();
    spoon.getEnvironment().setCommentEnabled(true);
    spoon.addInputResource("src/main/java/");
    // contract: all non-trivial public methods should be documented with proper API Javadoc
    spoon.buildModel();
    List<String> notDocumented = new ArrayList<>();
    for (CtMethod method : spoon.getModel().getElements(new TypeFilter<>(CtMethod.class))) {
        // now we see whether this should be documented
        if (// public methods should be documented
        method.hasModifier(ModifierKind.PUBLIC) && // all kinds of setters can be undocumented
        !method.getSimpleName().startsWith("get") && !method.getSimpleName().startsWith("set") && !method.getSimpleName().startsWith("is") && !method.getSimpleName().startsWith("add") && !method.getSimpleName().startsWith("remove") && // only the top declarations should be documented (not the overriding methods which are lower in the hierarchy)
        method.getTopDefinitions().size() == 0 && (// all interface methods and abstract class methods must be documented
        method.hasModifier(ModifierKind.ABSTRACT) || // 4) you commit your changes and create the corresponding pull requests
        method.filterChildren(new TypeFilter<>(CtCodeElement.class)).list().size() > // means that only large methods must be documented
        35)) {
            // is it really well documented?
            if (method.getDocComment().length() <= 15) {
                // the Javadoc must be at least at least 15 characters (still pretty short...)
                notDocumented.add(method.getParent(CtType.class).getQualifiedName() + "#" + method.getSignature());
            }
        }
    }
    if (notDocumented.size() > 0) {
        fail(notDocumented.size() + " public methods should be documented with proper API documentation: \n" + StringUtils.join(notDocumented, "\n"));
    }
    // contract: Spoon's code never uses TreeSet constructor, because they implicitly depend on Comparable (no static check, only dynamic checks)
    List<CtConstructorCall> treeSetWithoutComparators = spoon.getFactory().Package().getRootPackage().filterChildren(new AbstractFilter<CtConstructorCall>() {

        @Override
        public boolean matches(CtConstructorCall element) {
            return element.getType().getActualClass().equals(TreeSet.class) && element.getArguments().size() == 0;
        }
    }).list();
    assertEquals(0, treeSetWithoutComparators.size());
}
Also used : CtCodeElement(spoon.reflect.code.CtCodeElement) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtConstructorCall(spoon.reflect.code.CtConstructorCall) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 8 with CtConstructorCall

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

the class InitializerTest method testModelBuildingInitializer.

@Test
public void testModelBuildingInitializer() throws Exception {
    CtClass<?> type = build("spoon.test.initializers", "InstanceInitializers");
    assertEquals("InstanceInitializers", type.getSimpleName());
    CtField<?> k = type.getElements(new NamedElementFilter<>(CtField.class, "k")).get(0);
    assertTrue(k.getDefaultExpression() instanceof CtConstructorCall);
    CtField<?> l = type.getElements(new NamedElementFilter<>(CtField.class, "l")).get(0);
    assertTrue(l.getDefaultExpression() instanceof CtConstructorCall);
    CtField<?> x = type.getElements(new NamedElementFilter<>(CtField.class, "x")).get(0);
    assertTrue(x.getDefaultExpression() == null);
    CtField<?> y = type.getElements(new NamedElementFilter<>(CtField.class, "y")).get(0);
    assertTrue(y.getDefaultExpression() instanceof CtLiteral);
    CtField<?> z = type.getElements(new NamedElementFilter<>(CtField.class, "z")).get(0);
    assertTrue(z.getDefaultExpression().toString().equals("5"));
    // static initializer
    CtAnonymousExecutable ex = type.getElements(new TypeFilter<CtAnonymousExecutable>(CtAnonymousExecutable.class)).get(0);
    assertEquals("x = 3", ex.getBody().getStatements().get(0).toString());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtConstructorCall(spoon.reflect.code.CtConstructorCall) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) Test(org.junit.Test)

Example 9 with CtConstructorCall

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

the class TypeReferenceTest method testIgnoreEnclosingClassInActualTypes.

@Test
public void testIgnoreEnclosingClassInActualTypes() throws Exception {
    final CtType<Panini> aPanini = buildClass(Panini.class);
    final CtStatement ctReturn = aPanini.getMethod("entryIterator").getBody().getStatement(0);
    assertTrue(ctReturn instanceof CtReturn);
    final CtExpression ctConstructorCall = ((CtReturn) ctReturn).getReturnedExpression();
    assertTrue(ctConstructorCall instanceof CtConstructorCall);
    assertEquals("spoon.test.reference.testclasses.Panini<K, V>.Itr<java.util.Map.Entry<K, V>>", ctConstructorCall.getType().toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtReturn(spoon.reflect.code.CtReturn) CtExpression(spoon.reflect.code.CtExpression) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Panini(spoon.test.reference.testclasses.Panini) Test(org.junit.Test)

Example 10 with CtConstructorCall

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

the class TypeTest method testPolyTypBindingInTernaryExpression.

@Test
public void testPolyTypBindingInTernaryExpression() throws Exception {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/ternary-bug");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();
    CtType<Object> aType = launcher.getFactory().Type().get("de.uni_bremen.st.quide.persistence.transformators.IssueTransformator");
    CtConstructorCall ctConstructorCall = aType.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class) {

        @Override
        public boolean matches(CtConstructorCall element) {
            return "TOIssue".equals(element.getExecutable().getType().getSimpleName()) && super.matches(element);
        }
    }).get(0);
    assertEquals(launcher.getFactory().Type().objectType(), ctConstructorCall.getExecutable().getParameters().get(9));
}
Also used : CtConstructorCall(spoon.reflect.code.CtConstructorCall) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Aggregations

CtConstructorCall (spoon.reflect.code.CtConstructorCall)26 Test (org.junit.Test)20 Launcher (spoon.Launcher)14 Factory (spoon.reflect.factory.Factory)9 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)9 CtClass (spoon.reflect.declaration.CtClass)6 CtReturn (spoon.reflect.code.CtReturn)5 CtElement (spoon.reflect.declaration.CtElement)4 CtMethod (spoon.reflect.declaration.CtMethod)4 MainTest (spoon.test.main.MainTest)4 ArrayList (java.util.ArrayList)3 CtComment (spoon.reflect.code.CtComment)3 CtExpression (spoon.reflect.code.CtExpression)3 CtIf (spoon.reflect.code.CtIf)3 CtInvocation (spoon.reflect.code.CtInvocation)3 CtLocalVariable (spoon.reflect.code.CtLocalVariable)3 CtStatement (spoon.reflect.code.CtStatement)3 CtConstructor (spoon.reflect.declaration.CtConstructor)3 CtParameter (spoon.reflect.declaration.CtParameter)3 CtTypeReference (spoon.reflect.reference.CtTypeReference)3