001    /*
002     * Copyright 2010-2015 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.kotlin.descriptors.impl;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
022    import org.jetbrains.kotlin.descriptors.SourceElement;
023    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
024    import org.jetbrains.kotlin.name.Name;
025    import org.jetbrains.kotlin.resolve.constants.ConstantValue;
026    import org.jetbrains.kotlin.storage.NullableLazyValue;
027    import org.jetbrains.kotlin.types.KotlinType;
028    import org.jetbrains.kotlin.types.LazyType;
029    
030    public abstract class VariableDescriptorWithInitializerImpl extends VariableDescriptorImpl {
031        private final boolean isVar;
032    
033        protected NullableLazyValue<ConstantValue<?>> compileTimeInitializer;
034    
035        public VariableDescriptorWithInitializerImpl(
036                @NotNull DeclarationDescriptor containingDeclaration,
037                @NotNull Annotations annotations,
038                @NotNull Name name,
039                @Nullable KotlinType outType,
040                boolean isVar,
041                @NotNull SourceElement source
042        ) {
043            super(containingDeclaration, annotations, name, outType, source);
044    
045            this.isVar = isVar;
046        }
047    
048        @Override
049        public boolean isVar() {
050            return isVar;
051        }
052    
053        @Nullable
054        @Override
055        public ConstantValue<?> getCompileTimeInitializer() {
056            // Force computation and setting of compileTimeInitializer, if needed
057            if (compileTimeInitializer == null && outType instanceof LazyType) {
058                outType.getConstructor();
059            }
060    
061            if (compileTimeInitializer != null) {
062                return compileTimeInitializer.invoke();
063            }
064            return null;
065        }
066    
067        public void setCompileTimeInitializer(@NotNull NullableLazyValue<ConstantValue<?>> compileTimeInitializer) {
068            assert !isVar() : "Constant value for variable initializer should be recorded only for final variables: " + getName();
069            this.compileTimeInitializer = compileTimeInitializer;
070        }
071    }