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