001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.descriptors.impl;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
021    import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
022    import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
023    import org.jetbrains.kotlin.descriptors.SourceElement;
024    import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
025    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
026    import org.jetbrains.kotlin.name.Name;
027    import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
028    import org.jetbrains.kotlin.storage.StorageManager;
029    import org.jetbrains.kotlin.types.KotlinType;
030    import org.jetbrains.kotlin.types.TypeConstructor;
031    import org.jetbrains.kotlin.types.Variance;
032    
033    import java.util.Collection;
034    import java.util.Collections;
035    import java.util.List;
036    
037    public abstract class AbstractLazyTypeParameterDescriptor extends AbstractTypeParameterDescriptor {
038        public AbstractLazyTypeParameterDescriptor(
039                @NotNull StorageManager storageManager,
040                @NotNull DeclarationDescriptor containingDeclaration,
041                @NotNull Name name,
042                @NotNull Variance variance,
043                boolean isReified,
044                int index,
045                @NotNull SourceElement source
046        ) {
047            super(storageManager, containingDeclaration, Annotations.Companion.getEMPTY() /* TODO */, name, variance, isReified, index, source);
048        }
049    
050        @NotNull
051        @Override
052        protected TypeConstructor createTypeConstructor() {
053            return new TypeConstructor() {
054                @NotNull
055                @Override
056                public Collection<KotlinType> getSupertypes() {
057                    return AbstractLazyTypeParameterDescriptor.this.getUpperBounds();
058                }
059    
060                @NotNull
061                @Override
062                public List<TypeParameterDescriptor> getParameters() {
063                    return Collections.emptyList();
064                }
065    
066                @Override
067                public boolean isFinal() {
068                    return false;
069                }
070    
071                @Override
072                public boolean isDenotable() {
073                    return true;
074                }
075    
076                @Override
077                public ClassifierDescriptor getDeclarationDescriptor() {
078                    return AbstractLazyTypeParameterDescriptor.this;
079                }
080    
081                @NotNull
082                @Override
083                public Annotations getAnnotations() {
084                    return AbstractLazyTypeParameterDescriptor.this.getAnnotations();
085                }
086    
087                @NotNull
088                @Override
089                public KotlinBuiltIns getBuiltIns() {
090                    return DescriptorUtilsKt.getBuiltIns(AbstractLazyTypeParameterDescriptor.this);
091                }
092    
093                @Override
094                public String toString() {
095                    return getName().toString();
096                }
097            };
098        }
099    
100        @Override
101        public String toString() {
102            // Not using descriptor renderer to preserve laziness
103            return String.format(
104                    "%s%s%s",
105                    isReified() ? "reified " : "",
106                    getVariance() == Variance.INVARIANT ? "" : getVariance() + " ",
107                    getName()
108            );
109        }
110    }