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.annotations.Nullable;
021    import org.jetbrains.jet.descriptors.serialization.DescriptorDeserializer;
022    import org.jetbrains.jet.descriptors.serialization.Flags;
023    import org.jetbrains.jet.descriptors.serialization.NameResolver;
024    import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
025    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
026    import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
027    import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor;
028    import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
029    import org.jetbrains.jet.lang.descriptors.impl.FunctionDescriptorImpl;
030    import org.jetbrains.jet.lang.descriptors.impl.SimpleFunctionDescriptorImpl;
031    import org.jetbrains.jet.lang.resolve.name.Name;
032    
033    public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescriptorImpl implements DeserializedCallableMemberDescriptor {
034    
035        private final ProtoBuf.Callable proto;
036        private final NameResolver nameResolver;
037    
038        private DeserializedSimpleFunctionDescriptor(
039                @NotNull DeclarationDescriptor containingDeclaration,
040                @Nullable SimpleFunctionDescriptor original,
041                @NotNull Annotations annotations,
042                @NotNull Name name,
043                @NotNull Kind kind,
044                @NotNull ProtoBuf.Callable proto,
045                @NotNull NameResolver nameResolver) {
046            super(containingDeclaration, original, annotations, name, kind);
047            this.proto = proto;
048            this.nameResolver = nameResolver;
049        }
050    
051        @NotNull
052        @Override
053        protected FunctionDescriptorImpl createSubstitutedCopy(
054                @NotNull DeclarationDescriptor newOwner,
055                @Nullable FunctionDescriptor original,
056                @NotNull Kind kind
057        ) {
058            return new DeserializedSimpleFunctionDescriptor(
059                    newOwner,
060                    (DeserializedSimpleFunctionDescriptor) original,
061                    getAnnotations(),
062                    getName(),
063                    kind,
064                    proto,
065                    nameResolver
066            );
067        }
068    
069        @NotNull
070        @Override
071        public DeserializedSimpleFunctionDescriptor getOriginal() {
072            return (DeserializedSimpleFunctionDescriptor) super.getOriginal();
073        }
074    
075        @NotNull
076        @Override
077        public ProtoBuf.Callable getProto() {
078            return proto;
079        }
080    
081        @NotNull
082        @Override
083        public NameResolver getNameResolver() {
084            return nameResolver;
085        }
086    
087        public static DeserializedSimpleFunctionDescriptor create(
088                @NotNull DeclarationDescriptor containingDeclaration,
089                @NotNull ProtoBuf.Callable proto,
090                @NotNull Deserializers deserializers,
091                @NotNull NameResolver nameResolver
092        ) {
093            Annotations annotations = DescriptorDeserializer.getAnnotations(containingDeclaration, proto, proto.getFlags(),
094                                                                            Deserializers.AnnotatedCallableKind.FUNCTION,
095                                                                            deserializers.getAnnotationDeserializer(),
096                                                                            nameResolver);
097            return new DeserializedSimpleFunctionDescriptor(
098                    containingDeclaration,
099                    null,
100                    annotations,
101                    nameResolver.getName(proto.getName()),
102                    DescriptorDeserializer.memberKind(Flags.MEMBER_KIND.get(proto.getFlags())),
103                    proto,
104                    nameResolver);
105        }
106    }