All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.resolve.DelegationResolver Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2010-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.kotlin.resolve;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.psi.KtClassOrObject;
import org.jetbrains.kotlin.psi.KtDelegationSpecifier;
import org.jetbrains.kotlin.psi.KtDelegatorByExpressionSpecifier;
import org.jetbrains.kotlin.psi.KtTypeReference;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.TypeUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;

import static org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind.DELEGATION;
import static org.jetbrains.kotlin.diagnostics.Errors.MANY_IMPL_MEMBER_NOT_IMPLEMENTED;
import static org.jetbrains.kotlin.resolve.OverridingUtil.OverrideCompatibilityInfo.Result.OVERRIDABLE;

public final class DelegationResolver {

    @NotNull
    public static  Collection generateDelegatedMembers(
            @NotNull KtClassOrObject classOrObject,
            @NotNull ClassDescriptor ownerDescriptor,
            @NotNull Collection existingMembers,
            @NotNull BindingTrace trace,
            @NotNull MemberExtractor memberExtractor,
            @NotNull TypeResolver typeResolver
    ) {
        return new DelegationResolver(classOrObject, ownerDescriptor, existingMembers, trace, memberExtractor, typeResolver)
                .generateDelegatedMembers();
    }

    @NotNull private final KtClassOrObject classOrObject;
    @NotNull private final ClassDescriptor ownerDescriptor;
    @NotNull private final Collection existingMembers;
    @NotNull private final BindingTrace trace;
    @NotNull private final MemberExtractor memberExtractor;
    @NotNull private final TypeResolver typeResolver;

    private DelegationResolver(
            @NotNull KtClassOrObject classOrObject,
            @NotNull ClassDescriptor ownerDescriptor,
            @NotNull Collection existingMembers,
            @NotNull BindingTrace trace,
            @NotNull MemberExtractor extractor,
            @NotNull TypeResolver resolver
    ) {

        this.classOrObject = classOrObject;
        this.ownerDescriptor = ownerDescriptor;
        this.existingMembers = existingMembers;
        this.trace = trace;
        this.memberExtractor = extractor;
        this.typeResolver = resolver;
    }

    @NotNull
    private Collection generateDelegatedMembers() {
        Collection delegatedMembers = new HashSet();
        for (KtDelegationSpecifier delegationSpecifier : classOrObject.getDelegationSpecifiers()) {
            if (!(delegationSpecifier instanceof KtDelegatorByExpressionSpecifier)) {
                continue;
            }
            KtDelegatorByExpressionSpecifier specifier = (KtDelegatorByExpressionSpecifier) delegationSpecifier;
            KtTypeReference typeReference = specifier.getTypeReference();
            if (typeReference == null) {
                continue;
            }
            KotlinType delegatedTraitType = typeResolver.resolve(typeReference);
            if (delegatedTraitType == null || delegatedTraitType.isError()) {
                continue;
            }
            Collection delegatesForTrait = generateDelegatesForTrait(delegatedMembers, delegatedTraitType);
            delegatedMembers.addAll(delegatesForTrait);
        }
        return delegatedMembers;
    }

    @NotNull
    private Collection generateDelegatesForTrait(
            @NotNull Collection existingDelegates,
            @NotNull KotlinType delegatedTraitType
    ) {
        Collection result = new HashSet();
        Collection candidates = generateDelegationCandidates(delegatedTraitType);
        for (T candidate : candidates) {
            if (existingMemberOverridesDelegatedMember(candidate, existingMembers)) {
                continue;
            }
            //only leave the first delegated member
            if (checkClashWithOtherDelegatedMember(existingDelegates, candidate)) {
                continue;
            }

            result.add(candidate);
        }
        return result;
    }

    @NotNull
    private Collection generateDelegationCandidates(@NotNull KotlinType delegatedTraitType) {
        Collection descriptorsToDelegate = overridableMembersNotFromSuperClassOfTrait(delegatedTraitType);
        Collection result = new ArrayList(descriptorsToDelegate.size());
        for (T memberDescriptor : descriptorsToDelegate) {
            Modality newModality = memberDescriptor.getModality() == Modality.ABSTRACT ? Modality.OPEN : memberDescriptor.getModality();
            @SuppressWarnings("unchecked")
            T copy = (T) memberDescriptor.copy(ownerDescriptor, newModality, Visibilities.INHERITED, DELEGATION, false);
            result.add(copy);
        }
        return result;
    }

    private static boolean existingMemberOverridesDelegatedMember(
            @NotNull CallableMemberDescriptor candidate,
            @NotNull Collection existingMembers
    ) {
        for (CallableDescriptor existingDescriptor : existingMembers) {
            if (haveSameSignatures(existingDescriptor, candidate)) {
                return true;
            }
        }
        return false;
    }

    private boolean checkClashWithOtherDelegatedMember(@NotNull Collection delegatedMembers, @NotNull T candidate) {
        for (CallableMemberDescriptor alreadyDelegatedMember : delegatedMembers) {
            if (haveSameSignatures(alreadyDelegatedMember, candidate)) {
                //trying to delegate to many traits with the same methods
                trace.report(MANY_IMPL_MEMBER_NOT_IMPLEMENTED.on(classOrObject, classOrObject, alreadyDelegatedMember));
                return true;
            }
        }
        return false;
    }

    @NotNull
    private Collection overridableMembersNotFromSuperClassOfTrait(@NotNull KotlinType trait) {
        final Collection membersToSkip = getMembersFromClassSupertypeOfTrait(trait);
        return Collections2.filter(
                memberExtractor.getMembersByType(trait),
                new Predicate() {
                    @Override
                    public boolean apply(CallableMemberDescriptor descriptor) {
                        if (!descriptor.getModality().isOverridable()) {
                            return false;
                        }
                        for (CallableMemberDescriptor memberToSkip : membersToSkip) {
                            if (haveSameSignatures(memberToSkip, descriptor)) {
                                return false;
                            }
                        }
                        return true;
                    }
                });
    }

    private static boolean haveSameSignatures(@NotNull CallableDescriptor memberOne, @NotNull CallableDescriptor memberTwo) {
        //isOverridableBy ignores return types
        return OverridingUtil.DEFAULT.isOverridableBy(memberOne, memberTwo).getResult() == OVERRIDABLE;
    }

    @NotNull
    private Collection getMembersFromClassSupertypeOfTrait(@NotNull KotlinType traitType) {
        KotlinType classSupertype = null;
        for (KotlinType supertype : TypeUtils.getAllSupertypes(traitType)) {
            if (isNotTrait(supertype.getConstructor().getDeclarationDescriptor())) {
                classSupertype = supertype;
                break;
            }
        }
        return classSupertype != null ? memberExtractor.getMembersByType(classSupertype) : Collections.emptyList();
    }

    private static boolean isNotTrait(@Nullable DeclarationDescriptor descriptor) {
        if (descriptor instanceof ClassDescriptor) {
            ClassKind kind = ((ClassDescriptor) descriptor).getKind();
            return kind != ClassKind.INTERFACE;
        }
        return false;
    }

    public interface MemberExtractor {
        @NotNull
        Collection getMembersByType(@NotNull KotlinType type);
    }

    public interface TypeResolver {
        @Nullable
        KotlinType resolve(@NotNull KtTypeReference reference);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy