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());
}
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());
}
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());
}
Aggregations