Search in sources :

Example 1 with CtAssignment

use of spoon.reflect.code.CtAssignment 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 2 with CtAssignment

use of spoon.reflect.code.CtAssignment in project spoon by INRIA.

the class DeleteTest method testDeleteChainOfAssignment.

@Test
public void testDeleteChainOfAssignment() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtMethod method = adobada.getMethod("m4", factory.Type().INTEGER_PRIMITIVE, factory.Type().FLOAT_PRIMITIVE, factory.Type().STRING);
    final CtAssignment chainOfAssignment = method.getElements(new TypeFilter<>(CtAssignment.class)).get(0);
    assertNotNull(chainOfAssignment.getAssignment());
    chainOfAssignment.getAssignment().delete();
    assertNull(chainOfAssignment.getAssignment());
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Adobada(spoon.test.delete.testclasses.Adobada) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 3 with CtAssignment

use of spoon.reflect.code.CtAssignment in project spoon by INRIA.

the class CtBodyHolderTest method checkCtBody.

private void checkCtBody(CtBodyHolder p_bodyHolder, String p_constant, int off) {
    CtStatement body = p_bodyHolder.getBody();
    assertTrue(body instanceof CtBlock<?>);
    CtBlock<?> block = (CtBlock) body;
    assertEquals(1 + off, block.getStatements().size());
    assertTrue(block.getStatement(off) instanceof CtAssignment);
    CtAssignment assignment = block.getStatement(off);
    assertEquals(p_constant, ((CtLiteral<String>) assignment.getAssignment().partiallyEvaluate()).getValue());
    Factory f = body.getFactory();
    CtStatement newStat = new CWBStatementTemplate("xx").apply(body.getParent(CtType.class));
    try {
        newStat.getParent();
        fail();
    } catch (ParentNotInitializedException e) {
    // expected exception
    }
    // try to set statement and get CtBlock
    p_bodyHolder.setBody(newStat);
    CtBlock newBlock = (CtBlock) p_bodyHolder.getBody();
    assertSame(p_bodyHolder, newBlock.getParent());
    assertSame(newBlock, newStat.getParent());
    // try to set CtBlock and get the same CtBlock
    CtStatement newStat2 = newStat.clone();
    try {
        newStat2.getParent();
        fail();
    } catch (ParentNotInitializedException e) {
    // expected exception
    }
    CtBlock newBlock2 = f.Code().createCtBlock(newStat2);
    assertSame(newBlock2, newStat2.getParent());
    try {
        newBlock2.getParent();
        fail();
    } catch (ParentNotInitializedException e) {
    // expected exception
    }
    p_bodyHolder.setBody(newBlock2);
    assertSame(newBlock2, p_bodyHolder.getBody());
    assertSame(p_bodyHolder, newBlock2.getParent());
    assertSame(newBlock2, newStat2.getParent());
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtBlock(spoon.reflect.code.CtBlock) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) CWBStatementTemplate(spoon.test.ctBodyHolder.testclasses.CWBStatementTemplate)

Example 4 with CtAssignment

use of spoon.reflect.code.CtAssignment in project spoon by INRIA.

the class EnumsTypeTest method testEnumsType.

@Test
public void testEnumsType() throws Exception {
    // contract: shadow enum should still be considered as an enum
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/reference-test/EnumsRef.java");
    Factory factory = launcher.getFactory();
    List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/reference-test/EnumJar.jar");
    String[] dependencyClasspath = new String[] { classpath.get(0).getPath() };
    factory.getEnvironment().setSourceClasspath(dependencyClasspath);
    assertEquals(1, classpath.size());
    launcher.buildModel();
    List<CtAssignment> assignments = Query.getElements(factory, new TypeFilter<>(CtAssignment.class));
    CtTypeReference typeRefFromSource = assignments.get(0).getType();
    CtType typeFromSource = typeRefFromSource.getTypeDeclaration();
    assertTrue(typeRefFromSource.isEnum());
    assertTrue(typeFromSource.isEnum());
    assertTrue(typeFromSource instanceof CtEnum);
    CtTypeReference typeRefFromJar = assignments.get(1).getType();
    CtType typeFromJar = typeRefFromJar.getTypeDeclaration();
    // fail
    assertTrue(typeRefFromJar.isEnum());
    // fail
    assertTrue(typeFromJar.isEnum());
    // fail
    assertTrue(typeFromJar instanceof CtEnum);
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtEnum(spoon.reflect.declaration.CtEnum) SpoonResource(spoon.compiler.SpoonResource) Test(org.junit.Test)

Example 5 with CtAssignment

use of spoon.reflect.code.CtAssignment 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

CtAssignment (spoon.reflect.code.CtAssignment)12 Test (org.junit.Test)9 Factory (spoon.reflect.factory.Factory)9 CtExpression (spoon.reflect.code.CtExpression)5 Launcher (spoon.Launcher)4 CtStatement (spoon.reflect.code.CtStatement)4 CtMethod (spoon.reflect.declaration.CtMethod)4 CtType (spoon.reflect.declaration.CtType)4 CtTypeReference (spoon.reflect.reference.CtTypeReference)4 CtBlock (spoon.reflect.code.CtBlock)3 CtLocalVariable (spoon.reflect.code.CtLocalVariable)3 CtVariableAccess (spoon.reflect.code.CtVariableAccess)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CtComment (spoon.reflect.code.CtComment)2 CtFieldAccess (spoon.reflect.code.CtFieldAccess)2 CtIf (spoon.reflect.code.CtIf)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtClass (spoon.reflect.declaration.CtClass)2