Search in sources :

Example 11 with CtModel

use of spoon.reflect.CtModel in project spoon by INRIA.

the class TryCatchTest method testCatchWithUnknownExceptions.

@Test
public void testCatchWithUnknownExceptions() {
    // contract: unknown exception type in multicatch should not cause IndexOutOfBoundsException
    String inputResource = "./src/test/resources/spoon/test/noclasspath/exceptions/Foo.java";
    Launcher launcher = new Launcher();
    launcher.addInputResource(inputResource);
    launcher.getEnvironment().setNoClasspath(true);
    CtModel model = launcher.buildModel();
    List<CtCatch> catches = model.getElements(new TypeFilter<CtCatch>(CtCatch.class));
    // catch with single UnknownException
    assertNotNull(catches.get(0).getParameter().getType());
    // multicatch with UnknownException
    assertNull(catches.get(1).getParameter().getType());
    // multicatch with UnknownException
    assertNull(catches.get(2).getParameter().getType());
}
Also used : Launcher(spoon.Launcher) CtCatch(spoon.reflect.code.CtCatch) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 12 with CtModel

use of spoon.reflect.CtModel in project spoon by INRIA.

the class ImportTest method testSuperInheritanceHierarchyFunctionNoClasspath.

@Test
public void testSuperInheritanceHierarchyFunctionNoClasspath() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("src/test/resources/noclasspath/superclass/UnknownSuperClass.java");
    launcher.buildModel();
    final CtModel model = launcher.getModel();
    CtClass<?> classUSC = launcher.getFactory().Class().get("UnknownSuperClass");
    // contract: super inheritance scanner returns only Types on class path including final Object
    List<CtType> types = classUSC.map(new SuperInheritanceHierarchyFunction().includingSelf(true)).list();
    assertEquals(2, types.size());
    assertEquals("UnknownSuperClass", types.get(0).getQualifiedName());
    assertEquals("java.lang.Object", types.get(1).getQualifiedName());
    // contract: super inheritance scanner in reference mode returns type references including these which are not on class path and including final Object
    List<CtTypeReference> typeRefs = classUSC.map(new SuperInheritanceHierarchyFunction().includingSelf(true).returnTypeReferences(true)).list();
    assertEquals(3, typeRefs.size());
    assertEquals("UnknownSuperClass", typeRefs.get(0).getQualifiedName());
    assertEquals("NotInClasspath", typeRefs.get(1).getQualifiedName());
    assertEquals("java.lang.Object", typeRefs.get(2).getQualifiedName());
    // contract: super inheritance scanner in reference mode, which starts on class which is not available in model returns no Object, because it does not know if type is class or interface
    typeRefs = classUSC.getSuperclass().map(new SuperInheritanceHierarchyFunction().includingSelf(true).returnTypeReferences(true)).list();
    assertEquals(1, typeRefs.size());
    assertEquals("NotInClasspath", typeRefs.get(0).getQualifiedName());
    // contract: super inheritance scanner in type mode, which starts on class which is not available in model returns nothing
    types = classUSC.getSuperclass().map(new SuperInheritanceHierarchyFunction().includingSelf(true)).list();
    assertEquals(0, types.size());
}
Also used : SuperInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 13 with CtModel

use of spoon.reflect.CtModel in project spoon by INRIA.

the class MethodReferenceTest method testNoClasspathSuperExecutable.

@Test
public void testNoClasspathSuperExecutable() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("src/test/resources/noclasspath/superclass/UnknownSuperClass.java");
    launcher.buildModel();
    final CtModel model = launcher.getModel();
    final CtTypeReference overrideRef = launcher.getFactory().Annotation().createReference(Override.class);
    // call `getSuperClass()` indirectly using `getOverridingExecutable()`
    // some consistency checks...
    assertEquals(1, model.getElements(new NamedElementFilter<>(CtMethod.class, "a")).size());
    assertEquals(1, model.getElements(new NamedElementFilter<>(CtMethod.class, "b")).size());
    assertEquals(1, model.getElements(new NamedElementFilter<>(CtMethod.class, "toString")).size());
    // get super method of a class not available in classpath
    final CtMethod bMethod = model.getElements(new NamedElementFilter<>(CtMethod.class, "b")).get(0);
    assertNotNull(bMethod.getAnnotation(overrideRef));
    assertNull(bMethod.getReference().getOverridingExecutable());
    // get super method of a class available in classpath (Object)
    final CtMethod toStringMethod = model.getElements(new NamedElementFilter<>(CtMethod.class, "toString")).get(0);
    assertNotNull(toStringMethod.getAnnotation(overrideRef));
    assertNotNull(toStringMethod.getReference().getOverridingExecutable());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtModel(spoon.reflect.CtModel) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)13 CtModel (spoon.reflect.CtModel)13 Launcher (spoon.Launcher)12 SpoonAPI (spoon.SpoonAPI)3 CtClass (spoon.reflect.declaration.CtClass)3 CtMethod (spoon.reflect.declaration.CtMethod)3 CtType (spoon.reflect.declaration.CtType)3 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3 CtConstructorCall (spoon.reflect.code.CtConstructorCall)2 CtNewClass (spoon.reflect.code.CtNewClass)2 CtReturn (spoon.reflect.code.CtReturn)2 CtPackage (spoon.reflect.declaration.CtPackage)2 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)2 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)2 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertNull (org.junit.Assert.assertNull)1