Search in sources :

Example 6 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class CtTypeTest method testHasMethodInSuperClass.

@Test
public void testHasMethodInSuperClass() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/ctType/testclasses/X.java");
    launcher.run();
    final CtClass<?> xClass = launcher.getFactory().Class().get("spoon.test.ctType.testclasses.X");
    final CtClass<?> yClass = launcher.getFactory().Class().get("spoon.test.ctType.testclasses.Y");
    final CtMethod<?> superMethod = xClass.getMethods().iterator().next();
    assertTrue(yClass.hasMethod(superMethod));
}
Also used : Launcher(spoon.Launcher) Test(org.junit.Test)

Example 7 with Launcher

use of spoon.Launcher 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 8 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class EqualTest method testEqualsComment.

@Test
public void testEqualsComment() throws Exception {
    Factory factory = new Launcher().createFactory();
    CtLocalVariable<?> var = factory.Code().createCodeSnippetStatement("int i=0").compile();
    CtLocalVariable<?> var2 = var.clone();
    var2.addComment(factory.Code().createComment("foo", CtComment.CommentType.INLINE));
    assertNotEquals(1, var.getComments().size());
    assertNotEquals(var2, var);
}
Also used : Factory(spoon.reflect.factory.Factory) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 9 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class EqualTest method testEqualsMultitype.

@Test
public void testEqualsMultitype() throws Exception {
    Factory factory = new Launcher().createFactory();
    CtTry var = factory.Code().createCodeSnippetStatement("try{}catch(RuntimeException | AssertionError e){}").compile();
    CtTry var2 = var.clone();
    assertEquals(2, var2.getCatchers().get(0).getParameter().getMultiTypes().size());
    // removing a multitype
    var2.getCatchers().get(0).getParameter().getMultiTypes().remove(0);
    assertEquals(1, var2.getCatchers().get(0).getParameter().getMultiTypes().size());
    assertNotEquals(var2, var);
}
Also used : Factory(spoon.reflect.factory.Factory) Launcher(spoon.Launcher) CtTry(spoon.reflect.code.CtTry) Test(org.junit.Test)

Example 10 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class EqualTest method testEqualsActualTypeRef.

@Test
public void testEqualsActualTypeRef() throws Exception {
    // contract: actual type refs are part of the identity
    Factory factory = new Launcher().createFactory();
    CtLocalVariable var = factory.Code().createCodeSnippetStatement("java.util.List<String> l ").compile();
    CtLocalVariable var2 = factory.Code().createCodeSnippetStatement("java.util.List<Object> l ").compile();
    assertNotEquals(var2, var);
}
Also used : Factory(spoon.reflect.factory.Factory) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)524 Test (org.junit.Test)486 Factory (spoon.reflect.factory.Factory)163 CtClass (spoon.reflect.declaration.CtClass)111 CtMethod (spoon.reflect.declaration.CtMethod)79 File (java.io.File)75 CtType (spoon.reflect.declaration.CtType)66 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)60 CtTypeReference (spoon.reflect.reference.CtTypeReference)48 SpoonModelBuilder (spoon.SpoonModelBuilder)44 ArrayList (java.util.ArrayList)43 CtAnnotation (spoon.reflect.declaration.CtAnnotation)38 CtInvocation (spoon.reflect.code.CtInvocation)32 CtElement (spoon.reflect.declaration.CtElement)27 CtPackage (spoon.reflect.declaration.CtPackage)27 FileSystemFile (spoon.support.compiler.FileSystemFile)26 CtStatement (spoon.reflect.code.CtStatement)25 CtField (spoon.reflect.declaration.CtField)24 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)24 SpoonAPI (spoon.SpoonAPI)22