001    /*
002     * Copyright 2010-2014 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.lang.resolve.kotlin;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.descriptors.serialization.NameResolver;
022    import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
023    import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotatedCallableKind;
024    import org.jetbrains.jet.descriptors.serialization.descriptors.ConstantLoader;
025    import org.jetbrains.jet.lang.descriptors.ClassOrPackageFragmentDescriptor;
026    import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
027    import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter;
028    
029    import javax.inject.Inject;
030    
031    import static org.jetbrains.jet.lang.resolve.kotlin.DescriptorLoadersStorage.MemberSignature;
032    
033    public class ConstantDescriptorLoader extends BaseDescriptorLoader implements ConstantLoader {
034        @Inject
035        @Override
036        public void setStorage(@NotNull DescriptorLoadersStorage storage) {
037            this.storage = storage;
038        }
039    
040        @Inject
041        @Override
042        public void setKotlinClassFinder(@NotNull KotlinClassFinder kotlinClassFinder) {
043            this.kotlinClassFinder = kotlinClassFinder;
044        }
045    
046        @Inject
047        @Override
048        public void setErrorReporter(@NotNull ErrorReporter errorReporter) {
049            this.errorReporter = errorReporter;
050        }
051    
052        @Nullable
053        @Override
054        public CompileTimeConstant<?> loadPropertyConstant(
055                @NotNull ClassOrPackageFragmentDescriptor container,
056                @NotNull ProtoBuf.Callable proto,
057                @NotNull NameResolver nameResolver,
058                @NotNull AnnotatedCallableKind kind
059        ) {
060            MemberSignature signature = getCallableSignature(proto, nameResolver, kind);
061            if (signature == null) return null;
062    
063            KotlinJvmBinaryClass kotlinClass = findClassWithAnnotationsAndInitializers(container, proto, nameResolver, kind);
064            if (kotlinClass == null) {
065                errorReporter.reportLoadingError("Kotlin class for loading property constant is not found: " + container, null);
066                return null;
067            }
068    
069            return storage.getStorage().invoke(kotlinClass).getPropertyConstants().get(signature);
070        }
071    }