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