Search in sources :

Example 26 with CtTypeReference

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

the class ReferenceQueryTest method getAllTypeReferencesInEnum.

@Test
public void getAllTypeReferencesInEnum() throws Exception {
    CtEnum<ReferenceQueryTestEnum> testEnum = build("spoon.test.reflect.visitor", "ReferenceQueryTestEnum");
    List<CtTypeReference<?>> enumTypeRefs = Query.getElements(testEnum, new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class));
    TypeFactory typeFactory = testEnum.getFactory().Type();
    for (Class<?> c : new Class<?>[] { Integer.class, Long.class, Boolean.class, Number.class, String.class, Void.class }) {
        Assert.assertTrue("the reference query on the enum should return all the types defined in the enum declaration", enumTypeRefs.contains(typeFactory.createReference(c)));
    }
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) TypeFactory(spoon.reflect.factory.TypeFactory) Test(org.junit.Test)

Example 27 with CtTypeReference

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

the class TryCatchTest method testRethrowingExceptionsJava7.

@Test
public void testRethrowingExceptionsJava7() throws Exception {
    CtClass<?> clazz = build("spoon.test.trycatch", "RethrowingClass");
    CtMethod<?> method = (CtMethod<?>) clazz.getMethods().toArray()[0];
    Set<CtTypeReference<? extends Throwable>> thrownTypes = method.getThrownTypes();
    // Checks we throw 2 exceptions and not one.
    assertEquals(2, thrownTypes.size());
    CtTry ctTry = clazz.getElements(new TypeFilter<CtTry>(CtTry.class)).get(0);
    Class<? extends CtCatchVariableReference> exceptionClass = ctTry.getCatchers().get(0).getParameter().getReference().getClass();
    // Checks the exception in the catch isn't on the signature of the method.
    for (CtTypeReference<? extends Throwable> thrownType : thrownTypes) {
        assertNotEquals(thrownType.getClass(), exceptionClass);
    }
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtTry(spoon.reflect.code.CtTry) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 28 with CtTypeReference

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

the class TryCatchTest method testTryCatchVariableGetType.

@Test
public void testTryCatchVariableGetType() throws Exception {
    Factory factory = createFactory();
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " try{}catch(RuntimeException e){System.exit(0);}" + "}" + "};").compile();
    CtTry tryStmt = (CtTry) clazz.getElements(new TypeFilter<>(CtTry.class)).get(0);
    List<CtCatch> catchers = tryStmt.getCatchers();
    assertEquals(1, catchers.size());
    CtCatchVariable<?> catchVariable = catchers.get(0).getParameter();
    assertEquals(RuntimeException.class, catchVariable.getType().getActualClass());
    assertEquals(1, catchVariable.getMultiTypes().size());
    assertEquals(RuntimeException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    // contract: the manipulation with catch variable type is possible
    catchVariable.setType((CtTypeReference) factory.Type().createReference(IllegalArgumentException.class));
    assertEquals(IllegalArgumentException.class, catchVariable.getType().getActualClass());
    // contract setType influences multitypes
    assertEquals(1, catchVariable.getMultiTypes().size());
    assertEquals(IllegalArgumentException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    catchVariable.setMultiTypes(Collections.singletonList((CtTypeReference) factory.Type().createReference(UnsupportedOperationException.class)));
    assertEquals(UnsupportedOperationException.class, catchVariable.getType().getActualClass());
    // contract setType influences multitypes
    assertEquals(1, catchVariable.getMultiTypes().size());
    assertEquals(UnsupportedOperationException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    catchVariable.setMultiTypes(Arrays.asList(factory.Type().createReference(UnsupportedOperationException.class), factory.Type().createReference(IllegalArgumentException.class)));
    assertEquals(2, catchVariable.getMultiTypes().size());
    assertEquals(UnsupportedOperationException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    assertEquals(IllegalArgumentException.class, catchVariable.getMultiTypes().get(1).getActualClass());
    // contract setMultiTypes influences types, which contains common super class of all multi types
    assertEquals(RuntimeException.class, catchVariable.getType().getActualClass());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtCatch(spoon.reflect.code.CtCatch) CtTry(spoon.reflect.code.CtTry) Test(org.junit.Test)

Example 29 with CtTypeReference

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

the class DefaultJavaPrettyPrinter method visitCtProvidedService.

@Override
public void visitCtProvidedService(CtProvidedService moduleProvidedService) {
    printer.writeKeyword("provides").writeSpace();
    scan(moduleProvidedService.getServiceType());
    try (ListPrinter lp = this.elementPrinterHelper.createListPrinter(false, " with", true, false, ",", true, false, null)) {
        for (CtTypeReference implementations : moduleProvidedService.getImplementationTypes()) {
            lp.printSeparatorIfAppropriate();
            scan(implementations);
        }
    }
    printer.writeSeparator(";").writeln();
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference)

Example 30 with CtTypeReference

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

the class DefaultJavaPrettyPrinter method isImported.

private boolean isImported(CtFieldReference fieldReference) {
    CtImport fieldImport = fieldReference.getFactory().createImport(fieldReference);
    if (this.imports.contains(fieldImport)) {
        return true;
    } else {
        if (fieldReference.getDeclaringType() == null) {
            return false;
        }
        CtTypeReference staticTypeMemberReference = fieldReference.getFactory().Type().createWildcardStaticTypeMemberReference(fieldReference.getDeclaringType());
        CtImport staticClassImport = fieldReference.getFactory().createImport(staticTypeMemberReference);
        return this.imports.contains(staticClassImport);
    }
}
Also used : CtImport(spoon.reflect.declaration.CtImport) CtTypeReference(spoon.reflect.reference.CtTypeReference)

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