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.descriptors.ClassifierDescriptor;
021    import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
022    import org.jetbrains.kotlin.descriptors.SourceElement;
023    import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
024    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
025    import org.jetbrains.kotlin.name.Name;
026    import org.jetbrains.kotlin.storage.StorageManager;
027    import org.jetbrains.kotlin.types.JetType;
028    import org.jetbrains.kotlin.types.TypeConstructor;
029    import org.jetbrains.kotlin.types.Variance;
030    
031    import java.util.Collection;
032    import java.util.Collections;
033    import java.util.List;
034    
035    public abstract class AbstractLazyTypeParameterDescriptor extends AbstractTypeParameterDescriptor {
036        public AbstractLazyTypeParameterDescriptor(
037                @NotNull StorageManager storageManager,
038                @NotNull DeclarationDescriptor containingDeclaration,
039                @NotNull Name name,
040                @NotNull Variance variance,
041                boolean isReified,
042                int index,
043                @NotNull SourceElement source
044        ) {
045            super(storageManager, containingDeclaration, Annotations.EMPTY /* TODO */, name, variance, isReified, index, source);
046        }
047    
048        @NotNull
049        @Override
050        protected TypeConstructor createTypeConstructor() {
051            return new TypeConstructor() {
052                @NotNull
053                @Override
054                public Collection<JetType> getSupertypes() {
055                    return AbstractLazyTypeParameterDescriptor.this.getUpperBounds();
056                }
057    
058                @NotNull
059                @Override
060                public List<TypeParameterDescriptor> getParameters() {
061                    return Collections.emptyList();
062                }
063    
064                @Override
065                public boolean isFinal() {
066                    return false;
067                }
068    
069                @Override
070                public boolean isDenotable() {
071                    return true;
072                }
073    
074                @Override
075                public ClassifierDescriptor getDeclarationDescriptor() {
076                    return AbstractLazyTypeParameterDescriptor.this;
077                }
078    
079                @NotNull
080                @Override
081                public Annotations getAnnotations() {
082                    return AbstractLazyTypeParameterDescriptor.this.getAnnotations();
083                }
084    
085                @Override
086                public String toString() {
087                    return getName().toString();
088                }
089            };
090        }
091    
092        @Override
093        public String toString() {
094            // Not using descriptor renderer to preserve laziness
095            return String.format(
096                    "%s%s%s",
097                    isReified() ? "reified " : "",
098                    getVariance() == Variance.INVARIANT ? "" : getVariance() + " ",
099                    getName()
100            );
101        }
102    }