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;
018    
019    import com.intellij.util.Function;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationDeserializer;
023    import org.jetbrains.jet.descriptors.serialization.descriptors.DeserializedClassDescriptor;
024    import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
025    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
026    import org.jetbrains.jet.lang.resolve.lazy.storage.MemoizedFunctionToNullable;
027    import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
028    
029    import static org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager.ReferenceKind.STRONG;
030    
031    public abstract class AbstractDescriptorFinder implements DescriptorFinder {
032    
033        private final MemoizedFunctionToNullable<ClassId, ClassDescriptor> findClass;
034        private final AnnotationDeserializer annotationDeserializer;
035    
036        public AbstractDescriptorFinder(
037                @NotNull final StorageManager storageManager,
038                @NotNull AnnotationDeserializer annotationDeserializer
039        ) {
040            this.annotationDeserializer = annotationDeserializer;
041    
042            this.findClass = storageManager.createMemoizedFunctionWithNullableValues(new Function<ClassId, ClassDescriptor>() {
043                @Override
044                public ClassDescriptor fun(ClassId classId) {
045                    ClassData classData = getClassData(classId);
046                    if (classData == null) {
047                        return null;
048                    }
049    
050                    ProtoBuf.Class classProto = classData.getClassProto();
051    
052                    DeclarationDescriptor owner =
053                            classId.isTopLevelClass() ? findPackage(classId.getPackageFqName()) : findClass(classId.getOuterClassId());
054                    assert owner != null : "No owner found for " + classId;
055    
056                    AbstractDescriptorFinder _this = AbstractDescriptorFinder.this;
057                    ClassDescriptor classDescriptor = new DeserializedClassDescriptor(
058                            classId, storageManager, owner, classData.getNameResolver(),
059                            _this.annotationDeserializer, _this, classProto, null);
060                    classDescriptorCreated(classDescriptor);
061                    return classDescriptor;
062                }
063            }, STRONG);
064        }
065    
066        @Nullable
067        @Override
068        public ClassDescriptor findClass(@NotNull ClassId classId) {
069            return findClass.fun(classId);
070        }
071    
072        @Nullable
073        protected abstract ClassData getClassData(@NotNull ClassId classId);
074    
075        protected void classDescriptorCreated(@NotNull ClassDescriptor classDescriptor) {
076        }
077    }