Search in sources :

Example 16 with CtTypeReference

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

the class SnippetCompilationHelper method createWrapperContent.

private static String createWrapperContent(final CtElement element, final Factory f, final CtTypeReference returnType) {
    CtClass<?> w = f.Class().create(WRAPPER_CLASS_NAME);
    CtBlock body = f.Core().createBlock();
    if (element instanceof CtStatement) {
        body.addStatement((CtStatement) element);
    } else if (element instanceof CtExpression) {
        CtReturn ret = f.Core().createReturn();
        ret.setReturnedExpression((CtExpression) element);
        body.addStatement(ret);
    }
    Set<ModifierKind> modifiers = EnumSet.of(ModifierKind.STATIC);
    Set<CtTypeReference<? extends Throwable>> thrownTypes = new HashSet<>();
    thrownTypes.add(f.Class().<Throwable>get(Throwable.class).getReference());
    f.Method().create(w, modifiers, returnType, WRAPPER_METHOD_NAME, CtElementImpl.<CtParameter<?>>emptyList(), thrownTypes, body);
    String contents = w.toString();
    // Clean up (delete wrapper from factory) after it is printed. The DefaultJavaPrettyPrinter needs w in model to be able to print it correctly
    w.getPackage().removeType(w);
    return contents;
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) CtExpression(spoon.reflect.code.CtExpression) CtReturn(spoon.reflect.code.CtReturn) CtTypeReference(spoon.reflect.reference.CtTypeReference) ModifierKind(spoon.reflect.declaration.ModifierKind) HashSet(java.util.HashSet)

Example 17 with CtTypeReference

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

the class CtCatchVariableImpl method getType.

@SuppressWarnings("unchecked")
@Override
@DerivedProperty
public CtTypeReference<T> getType() {
    if (types.isEmpty()) {
        return null;
    } else if (types.size() == 1) {
        return (CtTypeReference<T>) types.get(0);
    }
    // compute common super type of exceptions
    List<CtTypeReference<?>> superTypesOfFirst = types.get(0).map(new SuperInheritanceHierarchyFunction().includingInterfaces(false).includingSelf(true).returnTypeReferences(true)).list();
    if (superTypesOfFirst.isEmpty()) {
        return null;
    }
    int commonSuperTypeIdx = 0;
    // index of Throwable. Last is Object
    int throwableIdx = superTypesOfFirst.size() - 2;
    for (int i = 1; i < types.size() && commonSuperTypeIdx != throwableIdx; i++) {
        CtTypeReference<?> nextException = types.get(i);
        while (commonSuperTypeIdx < throwableIdx) {
            if (nextException.isSubtypeOf(superTypesOfFirst.get(commonSuperTypeIdx))) {
                // nextException is sub type of actually selected commonSuperType
                break;
            }
            // try next super type
            commonSuperTypeIdx++;
        }
    }
    return (CtTypeReference<T>) superTypesOfFirst.get(commonSuperTypeIdx);
}
Also used : SuperInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction) CtTypeReference(spoon.reflect.reference.CtTypeReference) DerivedProperty(spoon.support.DerivedProperty)

Example 18 with CtTypeReference

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

the class CtTypeTest method testIsSubTypeOfonTypeReferences.

@Test
public void testIsSubTypeOfonTypeReferences() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "-c" });
    launcher.addInputResource("./src/test/java/spoon/test/ctType/testclasses/SubtypeModel.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtType<?> oCtType = factory.Class().get("spoon.test.ctType.testclasses.SubtypeModel");
    CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
    Map<String, CtTypeReference<?>> nameToTypeRef = new HashMap<>();
    O_FooMethod.filterChildren(new TypeFilter<>(CtLocalVariable.class)).forEach((CtLocalVariable var) -> {
        nameToTypeRef.put(var.getSimpleName(), var.getType());
    });
    int[] count = new int[1];
    O_FooMethod.filterChildren(new TypeFilter<>(CtAssignment.class)).forEach((CtAssignment ass) -> {
        for (CtComment comment : ass.getComments()) {
            checkIsNotSubtype(comment, nameToTypeRef);
            count[0]++;
        }
        ;
        count[0]++;
        checkIsSubtype(((CtVariableAccess) ass.getAssigned()).getVariable().getType(), ((CtVariableAccess) ass.getAssignment()).getVariable().getType(), nameToTypeRef);
    });
    assertTrue(count[0] > (9 * 8));
}
Also used : CtComment(spoon.reflect.code.CtComment) CtVariableAccess(spoon.reflect.code.CtVariableAccess) CtAssignment(spoon.reflect.code.CtAssignment) HashMap(java.util.HashMap) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 19 with CtTypeReference

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

the class ConstructorTest method assertIntersectionTypeInConstructor.

private void assertIntersectionTypeInConstructor(CtTypeReference<?> boundingType1) {
    assertTrue(boundingType1 instanceof CtIntersectionTypeReference);
    CtIntersectionTypeReference<?> boundingType = boundingType1.asCtIntersectionTypeReference();
    final List<CtTypeReference<?>> bounds = boundingType.getBounds().stream().collect(Collectors.toList());
    CtTypeReference<?> genericTacos = bounds.get(0);
    assertEquals("Tacos", genericTacos.getSimpleName());
    assertEquals(1, genericTacos.getAnnotations().size());
    assertEquals(1, genericTacos.getActualTypeArguments().size());
    CtTypeParameterReference wildcard = (CtTypeParameterReference) genericTacos.getActualTypeArguments().get(0);
    assertEquals("?", wildcard.getSimpleName());
    assertEquals(1, wildcard.getAnnotations().size());
    assertEquals("C", wildcard.getBoundingType().getSimpleName());
    assertEquals(1, wildcard.getBoundingType().getAnnotations().size());
    assertEquals("Serializable", bounds.get(1).getSimpleName());
    assertEquals(1, bounds.get(1).getAnnotations().size());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtIntersectionTypeReference(spoon.reflect.reference.CtIntersectionTypeReference)

Example 20 with CtTypeReference

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

the class ExecutableFactoryTest method testCreateReference.

@Test
public void testCreateReference() {
    Factory f = createFactory();
    ExecutableFactory ef = f.Executable();
    String signature = "boolean Object#equals(Object)";
    CtExecutableReference<Object> eref = ef.createReference(signature);
    String type = eref.getType().getQualifiedName();
    String decltype = eref.getDeclaringType().getQualifiedName();
    String name = eref.getSimpleName();
    List<CtTypeReference<?>> params = eref.getParameters();
    List<CtTypeReference<?>> atas = eref.getActualTypeArguments();
    Assert.assertEquals("boolean", type);
    Assert.assertEquals("Object", decltype);
    Assert.assertEquals("equals", name);
    Assert.assertEquals(1, params.size());
    Assert.assertEquals(0, atas.size());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) ExecutableFactory(spoon.reflect.factory.ExecutableFactory) Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) ExecutableFactory(spoon.reflect.factory.ExecutableFactory) Test(org.junit.Test)

Aggregations

CtTypeReference (spoon.reflect.reference.CtTypeReference)121 Test (org.junit.Test)56 Launcher (spoon.Launcher)43 Factory (spoon.reflect.factory.Factory)32 CtMethod (spoon.reflect.declaration.CtMethod)26 CtType (spoon.reflect.declaration.CtType)24 ArrayList (java.util.ArrayList)22 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)17 CtClass (spoon.reflect.declaration.CtClass)16 CtField (spoon.reflect.declaration.CtField)16 List (java.util.List)15 CtElement (spoon.reflect.declaration.CtElement)14 CtInvocation (spoon.reflect.code.CtInvocation)13 CtReference (spoon.reflect.reference.CtReference)13 CtParameter (spoon.reflect.declaration.CtParameter)12 CtExpression (spoon.reflect.code.CtExpression)11 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)11 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)11 CtStatement (spoon.reflect.code.CtStatement)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9