Search in sources :

Example 31 with CtClass

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

the class TemplateTest method substituteTypeAccessReference.

@Test
public void substituteTypeAccessReference() throws Exception {
    // contract: the substitution of CtTypeAccess expression ignores actual type arguments if it have to
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/TypeReferenceClassAccessTemplate.java"));
    String outputDir = "./target/spooned/test/template/testclasses";
    spoon.setSourceOutputDirectory(outputDir);
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    // contract: String value is substituted in substring of literal, named element and reference
    CtTypeReference<?> typeRef = factory.Type().createReference(TypeReferenceClassAccessTemplate.Example.class);
    typeRef.addActualTypeArgument(factory.Type().DATE);
    final CtClass<?> result = (CtClass<?>) new TypeReferenceClassAccessTemplate(typeRef).apply(factory.Class().create("spoon.test.template.TypeReferenceClassAccess"));
    spoon.prettyprint();
    ModelUtils.canBeBuilt(outputDir, 8);
    CtMethod<?> method = result.getMethodsByName("someMethod").get(0);
    assertEquals("spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date>", method.getType().toString());
    assertEquals("spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date>", method.getParameters().get(0).getType().toString());
    assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.out", method.getBody().getStatement(0).toString());
    assertEquals("spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date> ret = new spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date>()", method.getBody().getStatement(1).toString());
    assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.currentTimeMillis()", method.getBody().getStatement(2).toString());
    assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.class", method.getBody().getStatement(3).toString());
    assertEquals("o = (o) instanceof spoon.test.template.TypeReferenceClassAccess.Example<?>", method.getBody().getStatement(4).toString());
    assertEquals("java.util.function.Supplier<java.lang.Long> p = spoon.test.template.TypeReferenceClassAccess.Example::currentTimeMillis", method.getBody().getStatement(5).toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeReferenceClassAccessTemplate(spoon.test.template.testclasses.TypeReferenceClassAccessTemplate) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 32 with CtClass

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

the class TemplateTest method testAnotherFieldAccessNameSubstitution.

@Test
public void testAnotherFieldAccessNameSubstitution() throws Exception {
    // contract: the substitution of name of whole field is possible
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/AnotherFieldAccessTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    {
        // contract: String value is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new AnotherFieldAccessTemplate().apply(factory.Class().create("x.X"));
        assertEquals("int x;", result.getField("x").toString());
        assertEquals("int m_x;", result.getField("m_x").toString());
        assertEquals("java.lang.System.out.println(((x) + (m_x)))", result.getAnonymousExecutables().get(0).getBody().getStatement(0).toString());
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) AnotherFieldAccessTemplate(spoon.test.template.testclasses.AnotherFieldAccessTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 33 with CtClass

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

the class SignatureTest method testMethodInvocationSignatureWithVariableAccess.

@Test
public void testMethodInvocationSignatureWithVariableAccess() throws Exception {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    factory.getEnvironment().setNoClasspath(true);
    String content = "" + "class PR {" + "static String PRS = null;" + "public Object foo(String p) {" + " int s = 0; 	" + " this.foo(s);" + "this.foo(p);" + " return null;" + "}" + " public Object foo(int p) {" + " String s = null;" + " this.foo(s);" + "this.foo(p);" + "return null;" + "}" + "};";
    SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
    builder.build();
    CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
    assertNotNull(clazz1);
    // **FIRST PART: passing local variable access.
    // /--------From the first method we take the method invocations
    TreeSet<CtMethod<?>> ts = new TreeSet<CtMethod<?>>(new DeepRepresentationComparator());
    ts.addAll(clazz1.getMethods());
    CtMethod[] methodArray = ts.toArray(new CtMethod[0]);
    CtMethod<?> methodInteger = methodArray[0];
    assertEquals("foo(int)", methodInteger.getSignature());
    CtInvocation<?> invoToInt1 = (CtInvocation<?>) methodInteger.getBody().getStatement(1);
    CtExpression<?> argumentToInt1 = invoToInt1.getArguments().get(0);
    // ----------From the second method we take the Method Inv
    CtMethod<?> methodString = (CtMethod<?>) methodArray[1];
    assertEquals("foo(java.lang.String)", methodString.getSignature());
    CtInvocation<?> invoToString = (CtInvocation<?>) methodString.getBody().getStatement(1);
    CtExpression<?> argumentToString = invoToString.getArguments().get(0);
    // we compare the signatures of " this.foo(s);"	from both methods
    assertNotEquals(invoToInt1, invoToString);
    // Now we check that we have two invocation to "foo(s)",
    // but one invocation is with var 's' type integer, the other var 's' type int
    assertNotEquals(argumentToInt1, argumentToString);
    // / ***SECOND PART, passing Parameters
    CtInvocation<?> invoToString2 = (CtInvocation<?>) methodInteger.getBody().getStatement(2);
    CtExpression<?> argumentToString2 = invoToString2.getArguments().get(0);
    CtInvocation<?> invoToInt2 = (CtInvocation<?>) methodString.getBody().getStatement(2);
    CtExpression<?> argumentToInt2 = invoToInt2.getArguments().get(0);
    // /
    // Compare the method invo signature (same real argument's name, different type)
    assertNotEquals(invoToString2, invoToInt2);
    // Compare signature of parameters (same var name, different type)
    assertNotEquals(argumentToString2, argumentToInt2);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) DefaultCoreFactory(spoon.support.DefaultCoreFactory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) CtClass(spoon.reflect.declaration.CtClass) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) CtInvocation(spoon.reflect.code.CtInvocation) TreeSet(java.util.TreeSet) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) CtMethod(spoon.reflect.declaration.CtMethod) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 34 with CtClass

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

the class SignatureTest method testNullSignatureInUnboundVariable.

@Test
public void testNullSignatureInUnboundVariable() throws Exception {
    // Unbound variable access bug fix:
    // Bug description: The signature printer ignored the element Unbound variable reference
    // (as well all Visitor that extend CtVisitor)
    // Fix description: modify CtVisitor (including SignaturePrinter) for visiting unbound variable access.
    Factory factory = new Launcher().createFactory();
    // We want to compile a class with an reference to a class that is not
    // in the classpath
    // As consequence, we set the option NoClasspath as true.
    factory.getEnvironment().setNoClasspath(true);
    String unboundVarAccess = "Complex.I";
    String content = "" + "class X {" + "public Object foo(java.util.List<String> l) {" + " Integer.toString(" + unboundVarAccess + ");" + " return null;" + "}};";
    SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
    try {
        builder.build();
        Assert.fail();
    } catch (Exception e) {
    // Must fail due to the unbound element "Complex.I"
    }
    CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
    Set<CtMethod<?>> methods = clazz1.getMethods();
    CtMethod<?> method = (CtMethod<?>) methods.toArray()[0];
    assertEquals("foo(java.util.List)", method.getSignature());
    CtInvocation<?> invo = (CtInvocation<?>) method.getBody().getStatement(0);
    CtExpression<?> argument1 = invo.getArguments().get(0);
    assertEquals(unboundVarAccess, argument1.toString());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 35 with CtClass

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

the class SignatureTest method testUnboundFieldSignature.

@Test
public void testUnboundFieldSignature() {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    factory.getEnvironment().setNoClasspath(true);
    String content = "" + "class PR {" + "public java.io.File foo(String p) {" + " this.mfield = p; 	" + " return null;" + "}" + "};";
    SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
    try {
        builder.build();
        fail();
    } catch (Exception e) {
    // must fail
    }
    CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
    assertNotNull(clazz1);
    // **FIRST PART: passing local variable access.
    // /--------From the first method we take the method invocations
    CtMethod<?> methodString = (CtMethod<?>) clazz1.getMethods().toArray()[0];
    assertEquals("foo(java.lang.String)", methodString.getSignature());
    CtAssignment<?, ?> invoToInt1 = (CtAssignment<?, ?>) methodString.getBody().getStatement(0);
    CtExpression<?> left = invoToInt1.getAssigned();
    assertEquals("this.mfield", left.toString());
    // null because noclasspath
    assertEquals(null, left.getType());
    assertEquals("this.mfield = p", invoToInt1.toString());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtAssignment(spoon.reflect.code.CtAssignment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) CtClass(spoon.reflect.declaration.CtClass) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) CtMethod(spoon.reflect.declaration.CtMethod) StandardEnvironment(spoon.support.StandardEnvironment) 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