Search in sources :

Example 26 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class GenericsTest method testIsSameSignatureWithGenerics.

@Test
public void testIsSameSignatureWithGenerics() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/SameSignature.java");
    launcher.buildModel();
    CtClass ctClass = launcher.getFactory().Class().get(SameSignature.class);
    List<CtMethod> methods = ctClass.getMethodsByName("forEach");
    assertEquals(1, methods.size());
    CtType<?> iterableItf = launcher.getFactory().Type().get(Iterable.class);
    List<CtMethod<?>> methodsItf = iterableItf.getMethodsByName("forEach");
    assertEquals(1, methodsItf.size());
    ClassTypingContext ctc = new ClassTypingContext(ctClass.getReference());
    assertTrue(ctc.isOverriding(methods.get(0), methodsItf.get(0)));
    assertTrue(ctc.isSubSignature(methods.get(0), methodsItf.get(0)));
    assertTrue(ctc.isSameSignature(methods.get(0), methodsItf.get(0)));
}
Also used : LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) ClassTypingContext(spoon.support.visitor.ClassTypingContext) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 27 with CtClass

use of spoon.reflect.declaration.CtClass 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)

Example 28 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class TemplateTest method substituteStringLiteral.

@Test
public void substituteStringLiteral() throws Exception {
    // contract: the substitution of literals is possible too
    // contract: the template engine supports substitution of root element
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SubstituteLiteralTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    {
        // contract: String value is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new SubstituteLiteralTemplate("value1").apply(factory.createClass());
        assertEquals("java.lang.String stringField1 = \"value1\";", result.getField("stringField1").toString());
        assertEquals("java.lang.String stringField2 = \"Substring value1 is substituted too - value1\";", result.getField("stringField2").toString());
        // contract: the parameter of type string replaces only method name
        assertEquals("java.lang.System.out.println(spoon.test.template.testclasses.Params.value1())", result.getMethodsByName("m1").get(0).getBody().getStatement(0).toString());
    }
    {
        // contract: String Literal value is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new SubstituteLiteralTemplate(factory.createLiteral("value2")).apply(factory.createClass());
        assertEquals("java.lang.String stringField1 = \"value2\";", result.getField("stringField1").toString());
        assertEquals("java.lang.String stringField2 = \"Substring value2 is substituted too - value2\";", result.getField("stringField2").toString());
        // contract: the parameter of type String literal replaces whole invocation
        assertEquals("java.lang.System.out.println(\"value2\")", result.getMethodsByName("m1").get(0).getBody().getStatement(0).toString());
    }
    {
        // contract: simple name of type reference is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new SubstituteLiteralTemplate(factory.Type().createReference("some.ignored.package.TypeName")).apply(factory.createClass());
        assertEquals("java.lang.String stringField1 = \"TypeName\";", result.getField("stringField1").toString());
        assertEquals("java.lang.String stringField2 = \"Substring TypeName is substituted too - TypeName\";", result.getField("stringField2").toString());
        // contract type reference is substituted in invocation as class access
        assertEquals("java.lang.System.out.println(some.ignored.package.TypeName.class)", result.getMethodsByName("m1").get(0).getBody().getStatement(0).toString());
    }
    {
        // contract: number literal is substituted in String literal as number converted to string
        final CtClass<?> result = (CtClass<?>) new SubstituteLiteralTemplate(factory.createLiteral(7)).apply(factory.createClass());
        assertEquals("java.lang.String stringField1 = \"7\";", result.getField("stringField1").toString());
        assertEquals("java.lang.String stringField2 = \"Substring 7 is substituted too - 7\";", result.getField("stringField2").toString());
        // contract number literal is substituted in invocation as number literal
        assertEquals("java.lang.System.out.println(7)", result.getMethodsByName("m1").get(0).getBody().getStatement(0).toString());
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) SubstituteLiteralTemplate(spoon.test.template.testclasses.SubstituteLiteralTemplate) Test(org.junit.Test)

Example 29 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class TemplateTest method testObjectIsNotParamTemplate.

@Test
public void testObjectIsNotParamTemplate() throws Exception {
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/ObjectIsNotParamTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    // contract: String value is substituted in substring of literal, named element and reference
    final CtClass<?> result = (CtClass<?>) new ObjectIsNotParamTemplate().apply(factory.createClass());
    assertEquals(0, result.getMethodsByName("methXXXd").size());
    assertEquals(1, result.getMethodsByName("method").size());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ObjectIsNotParamTemplate(spoon.test.template.testclasses.ObjectIsNotParamTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 30 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class TemplateTest method testTemplateMatcherWithWholePackage.

@Test
public void testTemplateMatcherWithWholePackage() throws Exception {
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/ContextHelper.java");
    spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/BServiceImpl.java");
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SecurityCheckerTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> templateKlass = factory.Class().get(SecurityCheckerTemplate.class);
    CtMethod templateMethod = (CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher1")).get(0);
    CtIf templateRoot = (CtIf) templateMethod.getBody().getStatement(0);
    TemplateMatcher matcher = new TemplateMatcher(templateRoot);
    List<CtElement> matches = matcher.find(factory.getModel().getRootPackage());
    assertEquals(1, matches.size());
    CtElement match = matches.get(0);
    assertTrue("Match is not a if", match instanceof CtIf);
    CtElement matchParent = match.getParent();
    assertTrue("Match parent is not a block", matchParent instanceof CtBlock);
    CtElement matchParentParent = matchParent.getParent();
    assertTrue("Match grand parent is not a method", matchParentParent instanceof CtMethod);
    CtMethod methodHello = (CtMethod) matchParentParent;
    assertEquals("Match grand parent is not a method called hello", "hello", methodHello.getSimpleName());
    CtElement methodParent = methodHello.getParent();
    assertTrue("Parent of the method is not a class", methodParent instanceof CtClass);
    CtClass bservice = (CtClass) methodParent;
    assertEquals("Parent of the method is not a class called BServiceImpl", "BServiceImpl", bservice.getSimpleName());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtBlock(spoon.reflect.code.CtBlock) CtElement(spoon.reflect.declaration.CtElement) TemplateMatcher(spoon.template.TemplateMatcher) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) CtMethod(spoon.reflect.declaration.CtMethod) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Aggregations

CtClass (spoon.reflect.declaration.CtClass)168 Test (org.junit.Test)151 Launcher (spoon.Launcher)102 Factory (spoon.reflect.factory.Factory)84 CtMethod (spoon.reflect.declaration.CtMethod)42 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)22 CtAnnotation (spoon.reflect.declaration.CtAnnotation)19 SpoonModelBuilder (spoon.SpoonModelBuilder)17 CtInvocation (spoon.reflect.code.CtInvocation)16 File (java.io.File)15 CtTypeReference (spoon.reflect.reference.CtTypeReference)15 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)15 Annotation (java.lang.annotation.Annotation)14 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)14 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)14 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)14 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)14 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)14 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)14 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)14