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