Search in sources :

Example 1 with CtRole

use of spoon.reflect.path.CtRole in project spoon by INRIA.

the class RoleHandlerHelper method getRoleHandlerWrtParent.

/**
 * @param element the {@link CtElement} whose relation from `element.getParent()` to `element` is needed.
 * @return {@link RoleHandler} handling relation from `element.getParent()` to `element`
 */
public static RoleHandler getRoleHandlerWrtParent(CtElement element) {
    if (element.isParentInitialized() == false) {
        return null;
    }
    CtElement parent = element.getParent();
    CtRole roleInParent = element.getRoleInParent();
    if (roleInParent == null) {
        return null;
    }
    return RoleHandlerHelper.getRoleHandler(parent.getClass(), roleInParent);
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtRole(spoon.reflect.path.CtRole)

Example 2 with CtRole

use of spoon.reflect.path.CtRole in project spoon by INRIA.

the class RoleHandlerHelper method getRoleHandlers.

/**
 * @param targetClass a Class whose handlers we are looking for
 * @return all {@link RoleHandler}s available for the `targetClass`
 */
public static List<RoleHandler> getRoleHandlers(Class<? extends CtElement> targetClass) {
    List<RoleHandler> handlers = roleHandlersByClass.get(targetClass);
    if (handlers == null) {
        List<RoleHandler> modifiableHandlers = new ArrayList<>();
        for (CtRole role : CtRole.values()) {
            RoleHandler roleHandler = getOptionalRoleHandler(targetClass, role);
            if (roleHandler != null) {
                modifiableHandlers.add(roleHandler);
            }
        }
        handlers = Collections.unmodifiableList(modifiableHandlers);
        roleHandlersByClass.put(targetClass, handlers);
    }
    return handlers;
}
Also used : RoleHandler(spoon.reflect.meta.RoleHandler) ArrayList(java.util.ArrayList) CtRole(spoon.reflect.path.CtRole)

Example 3 with CtRole

use of spoon.reflect.path.CtRole in project spoon by INRIA.

the class MetaModelTest method checkRoleHandlersOfType.

private void checkRoleHandlersOfType(CtType iface) {
    Class ifaceClass = iface.getActualClass();
    // contract: check that for each Spoon model interface we have correct list of Role handlers
    List<RoleHandler> roleHandlersOfIFace = new ArrayList<>(RoleHandlerHelper.getRoleHandlers(ifaceClass));
    Set<RoleHandler> allRoleHandlers = new HashSet<>();
    RoleHandlerHelper.forEachRoleHandler(rh -> allRoleHandlers.add(rh));
    for (CtRole role : CtRole.values()) {
        RoleHandler rh = RoleHandlerHelper.getOptionalRoleHandler(ifaceClass, role);
        if (rh != null) {
            assertTrue("RoleHandler for role " + role + " is missing for " + ifaceClass, roleHandlersOfIFace.remove(rh));
            assertTrue("RoleHandler " + rh + " is not accessible by RoleHandlerHelper#forEachRoleHandler()", allRoleHandlers.contains(rh));
        }
    }
    assertTrue("There are unexpected RoleHandlers " + roleHandlersOfIFace + " for " + ifaceClass, roleHandlersOfIFace.isEmpty());
}
Also used : RoleHandler(spoon.reflect.meta.RoleHandler) ArrayList(java.util.ArrayList) CtRole(spoon.reflect.path.CtRole) CtClass(spoon.reflect.declaration.CtClass) HashSet(java.util.HashSet)

Example 4 with CtRole

use of spoon.reflect.path.CtRole 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 5 with CtRole

use of spoon.reflect.path.CtRole in project spoon by INRIA.

the class CtModuleImpl method addModuleDirective.

@Override
public <T extends CtModule> T addModuleDirective(CtModuleDirective moduleDirective) {
    if (moduleDirective == null) {
        return (T) this;
    }
    if (this.moduleDirectives == CtElementImpl.<CtModuleDirective>emptyList()) {
        this.moduleDirectives = new SortedList<>(new CtLineElementComparator());
    }
    if (!this.moduleDirectives.contains(moduleDirective)) {
        moduleDirective.setParent(this);
        CtRole role = this.computeRoleFromModuleDirectory(moduleDirective);
        getFactory().getEnvironment().getModelChangeListener().onListAdd(this, role, this.moduleDirectives, moduleDirective);
        this.moduleDirectives.add(moduleDirective);
    }
    return (T) this;
}
Also used : CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) CtRole(spoon.reflect.path.CtRole)

Aggregations

CtRole (spoon.reflect.path.CtRole)15 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)6 HashSet (java.util.HashSet)4 CtMethod (spoon.reflect.declaration.CtMethod)4 RoleHandler (spoon.reflect.meta.RoleHandler)4 CtElement (spoon.reflect.declaration.CtElement)3 CtLineElementComparator (spoon.support.comparator.CtLineElementComparator)3 SpoonMetaModel (spoon.test.metamodel.SpoonMetaModel)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Launcher (spoon.Launcher)2 SpoonException (spoon.SpoonException)2 CtBlock (spoon.reflect.code.CtBlock)2 CtAnonymousExecutable (spoon.reflect.declaration.CtAnonymousExecutable)2 CtClass (spoon.reflect.declaration.CtClass)2 CtConstructor (spoon.reflect.declaration.CtConstructor)2 CtField (spoon.reflect.declaration.CtField)2 MMMethod (spoon.test.metamodel.MMMethod)2 MetamodelProperty (spoon.test.metamodel.MetamodelProperty)2