Search in sources :

Example 51 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class GenericsTest method testMethodTypingContext.

@Test
public void testMethodTypingContext() throws Exception {
    Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
    CtClass<?> ctClassWeddingLunch = factory.Class().get(WeddingLunch.class);
    CtMethod<?> trWeddingLunch_eatMe = ctClassWeddingLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
    MethodTypingContext methodSTH = new MethodTypingContext().setMethod(trWeddingLunch_eatMe);
    // contract: the method typing context provides its scope
    assertSame(trWeddingLunch_eatMe, methodSTH.getAdaptationScope());
    CtClass<?> ctClassLunch = factory.Class().get(Lunch.class);
    CtMethod<?> trLunch_eatMe = ctClassLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
    CtInvocation<?> invokeReserve = factory.Class().get(CelebrationLunch.class).filterChildren(new TypeFilter<>(CtInvocation.class)).select((CtInvocation i) -> "reserve".equals(i.getExecutable().getSimpleName())).first();
    MethodTypingContext methodReserveTC = new MethodTypingContext().setInvocation(invokeReserve);
    // contract: the method typing context provides its scope
    assertSame(invokeReserve.getExecutable().getDeclaration(), methodReserveTC.getAdaptationScope());
    // check that MethodTypingContext made from invocation knows actual type arguments of method and all declaring types
    // 1) check method actual type argument
    CtMethod<?> methodReserve = (CtMethod<?>) invokeReserve.getExecutable().getDeclaration();
    CtTypeParameter methodReserve_S = methodReserve.getFormalCtTypeParameters().get(0);
    assertEquals("S", methodReserve_S.getSimpleName());
    assertEquals("spoon.test.generics.testclasses.Tacos", methodReserveTC.adaptType(methodReserve_S).getQualifiedName());
    // 2) check actual type arguments of declaring type `Section`
    CtClass classSection = (CtClass) methodReserve.getDeclaringType();
    assertEquals("spoon.test.generics.testclasses.CelebrationLunch$WeddingLunch$Section", classSection.getQualifiedName());
    CtTypeParameter classSection_Y = classSection.getFormalCtTypeParameters().get(0);
    assertEquals("Y", classSection_Y.getSimpleName());
    assertEquals("spoon.test.generics.testclasses.Paella", methodReserveTC.adaptType(classSection_Y).getQualifiedName());
    // 3) check actual type arguments of declaring type `WeddingLunch`
    CtClass classWeddingLunch = (CtClass) classSection.getDeclaringType();
    assertEquals("spoon.test.generics.testclasses.CelebrationLunch$WeddingLunch", classWeddingLunch.getQualifiedName());
    CtTypeParameter classWeddingLunch_X = classWeddingLunch.getFormalCtTypeParameters().get(0);
    assertEquals("X", classWeddingLunch_X.getSimpleName());
    assertEquals("spoon.test.generics.testclasses.Mole", methodReserveTC.adaptType(classWeddingLunch_X).getQualifiedName());
    // 4) check actual type arguments of declaring type `CelebrationLunch`
    CtClass classCelebrationLunch = (CtClass) classWeddingLunch.getDeclaringType();
    assertEquals("spoon.test.generics.testclasses.CelebrationLunch", classCelebrationLunch.getQualifiedName());
    CtTypeParameter classCelebrationLunch_K = classCelebrationLunch.getFormalCtTypeParameters().get(0);
    CtTypeParameter classCelebrationLunch_L = classCelebrationLunch.getFormalCtTypeParameters().get(1);
    CtTypeParameter classCelebrationLunch_M = classCelebrationLunch.getFormalCtTypeParameters().get(2);
    assertEquals("K", classCelebrationLunch_K.getSimpleName());
    assertEquals("L", classCelebrationLunch_L.getSimpleName());
    assertEquals("M", classCelebrationLunch_M.getSimpleName());
    assertEquals("spoon.test.generics.testclasses.Tacos", methodReserveTC.adaptType(classCelebrationLunch_K).getQualifiedName());
    assertEquals("spoon.test.generics.testclasses.Paella", methodReserveTC.adaptType(classCelebrationLunch_L).getQualifiedName());
    assertEquals("spoon.test.generics.testclasses.Mole", methodReserveTC.adaptType(classCelebrationLunch_M).getQualifiedName());
    // method->Section->WeddingLunch->CelebrationLunch
    GenericTypeAdapter celebrationLunchTC = methodReserveTC.getEnclosingGenericTypeAdapter().getEnclosingGenericTypeAdapter().getEnclosingGenericTypeAdapter();
    assertEquals("java.lang.Integer", celebrationLunchTC.adaptType(classCelebrationLunch_K).getQualifiedName());
    assertEquals("java.lang.Long", celebrationLunchTC.adaptType(classCelebrationLunch_L).getQualifiedName());
    assertEquals("java.lang.Double", celebrationLunchTC.adaptType(classCelebrationLunch_M).getQualifiedName());
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) GenericTypeAdapter(spoon.support.visitor.GenericTypeAdapter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) MethodTypingContext(spoon.support.visitor.MethodTypingContext) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) CelebrationLunch(spoon.test.generics.testclasses.CelebrationLunch) File(java.io.File) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 52 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class GenericsTest method testClassTypingContextMethodSignature.

@Test
public void testClassTypingContextMethodSignature() throws Exception {
    // core contracts of MethodTypingContext#adaptMethod
    Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
    CtClass<?> ctClassLunch = factory.Class().get(Lunch.class);
    CtClass<?> ctClassWeddingLunch = factory.Class().get(WeddingLunch.class);
    // represents <C> void eatMe(A paramA, B paramB, C paramC){}
    CtMethod<?> trLunch_eatMe = ctClassLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
    // represents <C> void eatMe(M paramA, K paramB, C paramC)
    CtMethod<?> trWeddingLunch_eatMe = ctClassWeddingLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
    ClassTypingContext ctcWeddingLunch = new ClassTypingContext(ctClassWeddingLunch);
    assertTrue(ctcWeddingLunch.isOverriding(trLunch_eatMe, trLunch_eatMe));
    assertTrue(ctcWeddingLunch.isOverriding(trLunch_eatMe, trWeddingLunch_eatMe));
    assertTrue(ctcWeddingLunch.isSubSignature(trLunch_eatMe, trWeddingLunch_eatMe));
    // contract: check that adapting of methods still produces same results, even when scopeMethod is already assigned
    assertTrue(ctcWeddingLunch.isOverriding(trWeddingLunch_eatMe, trLunch_eatMe));
    assertTrue(ctcWeddingLunch.isOverriding(trWeddingLunch_eatMe, trWeddingLunch_eatMe));
    assertTrue(ctcWeddingLunch.isSubSignature(trWeddingLunch_eatMe, trWeddingLunch_eatMe));
}
Also used : ClassTypingContext(spoon.support.visitor.ClassTypingContext) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) File(java.io.File) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 53 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class ImportTest method testImportOfInvocationOfPrivateClass.

@Test
public void testImportOfInvocationOfPrivateClass() throws Exception {
    final Factory factory = getFactory("./src/test/java/spoon/test/imports/testclasses/internal2/Chimichanga.java", "./src/test/java/spoon/test/imports/testclasses/Mole.java");
    ImportScanner importContext = new ImportScannerImpl();
    importContext.computeImports(factory.Class().get(Mole.class));
    Collection<CtImport> imports = importContext.getAllImports();
    assertEquals(1, imports.size());
    assertEquals("import spoon.test.imports.testclasses.internal2.Chimichanga;\n", imports.toArray()[0].toString());
}
Also used : ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) CtImport(spoon.reflect.declaration.CtImport) Factory(spoon.reflect.factory.Factory) ImportScanner(spoon.reflect.visitor.ImportScanner) Mole(spoon.test.imports.testclasses.Mole) Test(org.junit.Test)

Example 54 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class ImportTest method testImportOfInvocationOfStaticMethod.

@Test
public void testImportOfInvocationOfStaticMethod() throws Exception {
    final Factory factory = getFactory("./src/test/java/spoon/test/imports/testclasses/internal2/Menudo.java", "./src/test/java/spoon/test/imports/testclasses/Pozole.java");
    ImportScanner importContext = new ImportScannerImpl();
    importContext.computeImports(factory.Class().get(Pozole.class));
    Collection<CtImport> imports = importContext.getAllImports();
    assertEquals(1, imports.size());
    assertEquals("import spoon.test.imports.testclasses.internal2.Menudo;\n", imports.toArray()[0].toString());
}
Also used : ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) CtImport(spoon.reflect.declaration.CtImport) Pozole(spoon.test.imports.testclasses.Pozole) Factory(spoon.reflect.factory.Factory) ImportScanner(spoon.reflect.visitor.ImportScanner) Test(org.junit.Test)

Example 55 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class ImportTest method testNewInnerClassDefinesInItsClassAndSuperClass.

@Test
public void testNewInnerClassDefinesInItsClassAndSuperClass() throws Exception {
    Launcher spoon = new Launcher();
    spoon.setArgs(new String[] { "--output-type", "nooutput" });
    Factory factory = spoon.createFactory();
    SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/imports/testclasses/SuperClass.java", "./src/test/java/spoon/test/imports/testclasses/SubClass.java"));
    compiler.build();
    final CtClass<?> subClass = (CtClass<?>) factory.Type().get(SubClass.class);
    final CtConstructorCall<?> ctConstructorCall = subClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0);
    assertEquals("new spoon.test.imports.testclasses.SubClass.Item(\"\")", ctConstructorCall.toString());
    // here the buggy behavior with type members was encoded
    // so we fix it
    final String expected = "public class SubClass extends spoon.test.imports.testclasses.SuperClass {" + System.lineSeparator() + "    public void aMethod() {" + System.lineSeparator() + "        new spoon.test.imports.testclasses.SubClass.Item(\"\");" + System.lineSeparator() + "    }" + System.lineSeparator() + System.lineSeparator() + "    public static class Item extends spoon.test.imports.testclasses.SuperClass.Item {" + System.lineSeparator() + "        public Item(java.lang.String s) {" + System.lineSeparator() + "            super(1, s);" + System.lineSeparator() + "        }" + System.lineSeparator() + "    }" + System.lineSeparator() + "}";
    assertEquals(expected, subClass.toString());
}
Also used : SubClass(spoon.test.imports.testclasses.SubClass) SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

Factory (spoon.reflect.factory.Factory)364 Test (org.junit.Test)322 Launcher (spoon.Launcher)154 CtClass (spoon.reflect.declaration.CtClass)87 CtMethod (spoon.reflect.declaration.CtMethod)73 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)65 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)53 File (java.io.File)41 CtStatement (spoon.reflect.code.CtStatement)36 CtTypeReference (spoon.reflect.reference.CtTypeReference)32 CtAnnotation (spoon.reflect.declaration.CtAnnotation)31 CtInvocation (spoon.reflect.code.CtInvocation)30 SpoonModelBuilder (spoon.SpoonModelBuilder)29 DefaultCoreFactory (spoon.support.DefaultCoreFactory)29 List (java.util.List)25 FileSystemFile (spoon.support.compiler.FileSystemFile)25 ArrayList (java.util.ArrayList)24 CtExpression (spoon.reflect.code.CtExpression)20 Annotation (java.lang.annotation.Annotation)19 MainTest (spoon.test.main.MainTest)19