001    /*
002     * Copyright 2010-2013 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.jet.descriptors.serialization.descriptors;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
021    import org.jetbrains.jet.descriptors.serialization.TypeDeserializer;
022    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
023    import org.jetbrains.jet.lang.descriptors.SourceElement;
024    import org.jetbrains.jet.lang.descriptors.impl.AbstractLazyTypeParameterDescriptor;
025    import org.jetbrains.jet.lang.resolve.name.Name;
026    import org.jetbrains.jet.lang.types.JetType;
027    import org.jetbrains.jet.lang.types.Variance;
028    import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
029    import org.jetbrains.jet.storage.StorageManager;
030    
031    import java.util.LinkedHashSet;
032    import java.util.Set;
033    
034    public class DeserializedTypeParameterDescriptor extends AbstractLazyTypeParameterDescriptor {
035    
036        private final ProtoBuf.TypeParameter proto;
037        private final TypeDeserializer typeDeserializer;
038    
039        public DeserializedTypeParameterDescriptor(
040                @NotNull StorageManager storageManager,
041                @NotNull TypeDeserializer typeDeserializer,
042                @NotNull ProtoBuf.TypeParameter proto,
043                @NotNull DeclarationDescriptor containingDeclaration,
044                @NotNull Name name,
045                @NotNull Variance variance,
046                boolean isReified,
047                int index
048        ) {
049            super(storageManager, containingDeclaration, name, variance, isReified, index, SourceElement.NO_SOURCE);
050            this.proto = proto;
051            this.typeDeserializer = typeDeserializer;
052        }
053    
054        @NotNull
055        @Override
056        protected Set<JetType> resolveUpperBounds() {
057            Set<JetType> result = new LinkedHashSet<JetType>(proto.getUpperBoundCount());
058            for (ProtoBuf.Type upperBound : proto.getUpperBoundList()) {
059                result.add(typeDeserializer.type(upperBound));
060            }
061            if (result.isEmpty()) {
062                result.add(KotlinBuiltIns.getInstance().getDefaultBound());
063            }
064            return result;
065        }
066    
067        public int getProtoId() {
068            return proto.getId();
069        }
070    }