Search in sources :

Example 6 with CtAnonymousExecutable

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

the class CtTypeImpl method addTypeMemberAt.

@Override
public <C extends CtType<T>> C addTypeMemberAt(int position, CtTypeMember member) {
    if (member == null) {
        return (C) this;
    }
    if (this.typeMembers == CtElementImpl.<CtTypeMember>emptyList()) {
        this.typeMembers = new ArrayList<>();
    }
    if (!this.typeMembers.stream().anyMatch(m -> m == member)) {
        member.setParent(this);
        CtRole role;
        if (member instanceof CtMethod) {
            role = METHOD;
        } else if (member instanceof CtConstructor) {
            role = CONSTRUCTOR;
        } else if (member instanceof CtField) {
            role = FIELD;
        } else if (member instanceof CtAnonymousExecutable) {
            role = ANNONYMOUS_EXECUTABLE;
        } else {
            role = NESTED_TYPE;
        }
        getFactory().getEnvironment().getModelChangeListener().onListAdd(this, role, this.typeMembers, position, member);
        if (position < typeMembers.size()) {
            this.typeMembers.add(position, member);
        } else {
            this.typeMembers.add(member);
        }
    }
    return (C) this;
}
Also used : CtPackageReference(spoon.reflect.reference.CtPackageReference) Arrays(java.util.Arrays) CtRole(spoon.reflect.path.CtRole) CtType(spoon.reflect.declaration.CtType) QualifiedNameBasedSortedSet(spoon.support.util.QualifiedNameBasedSortedSet) SnippetCompilationHelper(spoon.support.compiler.SnippetCompilationHelper) CtExecutable(spoon.reflect.declaration.CtExecutable) Query(spoon.reflect.visitor.Query) ClassTypingContext(spoon.support.visitor.ClassTypingContext) CtField(spoon.reflect.declaration.CtField) CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) SpoonException(spoon.SpoonException) ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) Collection(java.util.Collection) Set(java.util.Set) ANNONYMOUS_EXECUTABLE(spoon.reflect.path.CtRole.ANNONYMOUS_EXECUTABLE) CtModifiable(spoon.reflect.declaration.CtModifiable) AllTypeMembersFunction(spoon.reflect.visitor.filter.AllTypeMembersFunction) CtIntersectionTypeReference(spoon.reflect.reference.CtIntersectionTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) List(java.util.List) Annotation(java.lang.annotation.Annotation) CtBlock(spoon.reflect.code.CtBlock) UnsettableProperty(spoon.support.UnsettableProperty) CtFormalTypeDeclarer(spoon.reflect.declaration.CtFormalTypeDeclarer) INTERFACE(spoon.reflect.path.CtRole.INTERFACE) CtMethod(spoon.reflect.declaration.CtMethod) SignatureBasedSortedSet(spoon.support.util.SignatureBasedSortedSet) EarlyTerminatingScanner(spoon.reflect.visitor.EarlyTerminatingScanner) TYPE_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY(spoon.reflect.ModelElementContainerDefaultCapacities.TYPE_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY) CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtExtendedModifier(spoon.support.reflect.CtExtendedModifier) CtPackage(spoon.reflect.declaration.CtPackage) TYPE_PARAMETER(spoon.reflect.path.CtRole.TYPE_PARAMETER) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Refactoring(spoon.refactoring.Refactoring) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtModifierHandler(spoon.support.reflect.CtModifierHandler) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) FIELD(spoon.reflect.path.CtRole.FIELD) IS_SHADOW(spoon.reflect.path.CtRole.IS_SHADOW) DerivedProperty(spoon.support.DerivedProperty) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) CtShadowable(spoon.reflect.declaration.CtShadowable) CtConstructor(spoon.reflect.declaration.CtConstructor) CONSTRUCTOR(spoon.reflect.path.CtRole.CONSTRUCTOR) MetamodelPropertyField(spoon.reflect.annotations.MetamodelPropertyField) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) METHOD(spoon.reflect.path.CtRole.METHOD) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) ModifierKind(spoon.reflect.declaration.ModifierKind) CtFieldReference(spoon.reflect.reference.CtFieldReference) NESTED_TYPE(spoon.reflect.path.CtRole.NESTED_TYPE) Comparator(java.util.Comparator) Collections(java.util.Collections) CtAnnotationType(spoon.reflect.declaration.CtAnnotationType) CtField(spoon.reflect.declaration.CtField) CtRole(spoon.reflect.path.CtRole) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) CtMethod(spoon.reflect.declaration.CtMethod) CtConstructor(spoon.reflect.declaration.CtConstructor)

Example 7 with CtAnonymousExecutable

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

the class InitializerTest method testModelBuildingStaticInitializer.

@Test
public void testModelBuildingStaticInitializer() throws Exception {
    CtClass<?> type = build("spoon.test.initializers", "InternalClassStaticFieldInit");
    assertEquals("InternalClassStaticFieldInit", type.getSimpleName());
    CtClass<?> InternalClass = type.getNestedType("InternalClass");
    assertTrue(InternalClass.getModifiers().contains(ModifierKind.STATIC));
    CtAnonymousExecutable staticBlock = type.getElements(new TypeFilter<CtAnonymousExecutable>(CtAnonymousExecutable.class)).get(0);
    assertTrue(staticBlock.getModifiers().contains(ModifierKind.STATIC));
    assertEquals(1, staticBlock.getBody().getStatements().size());
// this fails: regression or known bug?
// RP: this look OK. Spoon adds the full path
// assertEquals("InternalClass.tmp = \"nop\"",
// staticBlock.getBody().getStatements().get(0).toString());
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) Test(org.junit.Test)

Example 8 with CtAnonymousExecutable

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

the class InitializerTest method testModelBuildingInitializer.

@Test
public void testModelBuildingInitializer() throws Exception {
    CtClass<?> type = build("spoon.test.initializers", "InstanceInitializers");
    assertEquals("InstanceInitializers", type.getSimpleName());
    CtField<?> k = type.getElements(new NamedElementFilter<>(CtField.class, "k")).get(0);
    assertTrue(k.getDefaultExpression() instanceof CtConstructorCall);
    CtField<?> l = type.getElements(new NamedElementFilter<>(CtField.class, "l")).get(0);
    assertTrue(l.getDefaultExpression() instanceof CtConstructorCall);
    CtField<?> x = type.getElements(new NamedElementFilter<>(CtField.class, "x")).get(0);
    assertTrue(x.getDefaultExpression() == null);
    CtField<?> y = type.getElements(new NamedElementFilter<>(CtField.class, "y")).get(0);
    assertTrue(y.getDefaultExpression() instanceof CtLiteral);
    CtField<?> z = type.getElements(new NamedElementFilter<>(CtField.class, "z")).get(0);
    assertTrue(z.getDefaultExpression().toString().equals("5"));
    // static initializer
    CtAnonymousExecutable ex = type.getElements(new TypeFilter<CtAnonymousExecutable>(CtAnonymousExecutable.class)).get(0);
    assertEquals("x = 3", ex.getBody().getStatements().get(0).toString());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtConstructorCall(spoon.reflect.code.CtConstructorCall) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) Test(org.junit.Test)

Example 9 with CtAnonymousExecutable

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

the class ExecutableFactory method createAnonymous.

/**
 * Creates an anonymous executable (initializer block) in a target class).
 */
public CtAnonymousExecutable createAnonymous(CtClass<?> target, CtBlock<Void> body) {
    CtAnonymousExecutable a = factory.Core().createAnonymousExecutable();
    target.addAnonymousExecutable(a);
    a.setBody(body);
    return a;
}
Also used : CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable)

Example 10 with CtAnonymousExecutable

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

the class DeleteTest method testDeleteAStatementInAnonymousExecutable.

@Test
public void testDeleteAStatementInAnonymousExecutable() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final List<CtAnonymousExecutable> anonymousExecutables = adobada.getAnonymousExecutables();
    final CtAnonymousExecutable instanceExec = anonymousExecutables.get(0);
    assertEquals(2, instanceExec.getBody().getStatements().size());
    final CtStatement statement = instanceExec.getBody().getStatement(1);
    statement.delete();
    assertEquals(1, instanceExec.getBody().getStatements().size());
    assertFalse(instanceExec.getBody().getStatements().contains(statement));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) Adobada(spoon.test.delete.testclasses.Adobada) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) Test(org.junit.Test)

Aggregations

CtAnonymousExecutable (spoon.reflect.declaration.CtAnonymousExecutable)17 Test (org.junit.Test)9 CtConstructor (spoon.reflect.declaration.CtConstructor)5 CtMethod (spoon.reflect.declaration.CtMethod)5 Factory (spoon.reflect.factory.Factory)5 CtStatement (spoon.reflect.code.CtStatement)4 CtConstructorCall (spoon.reflect.code.CtConstructorCall)3 CtIf (spoon.reflect.code.CtIf)3 CtSwitch (spoon.reflect.code.CtSwitch)3 CtClass (spoon.reflect.declaration.CtClass)3 CtElement (spoon.reflect.declaration.CtElement)3 ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3 ArrayList (java.util.ArrayList)2 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)2 CtBlock (spoon.reflect.code.CtBlock)2 CtComment (spoon.reflect.code.CtComment)2 CtConditional (spoon.reflect.code.CtConditional)2 CtDo (spoon.reflect.code.CtDo)2 CtFor (spoon.reflect.code.CtFor)2