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    
029    public abstract class VariableDescriptorWithInitializerImpl extends VariableDescriptorImpl {
030        private final boolean isVar;
031    
032        protected NullableLazyValue<ConstantValue<?>> compileTimeInitializer;
033    
034        public VariableDescriptorWithInitializerImpl(
035                @NotNull DeclarationDescriptor containingDeclaration,
036                @NotNull Annotations annotations,
037                @NotNull Name name,
038                @Nullable KotlinType outType,
039                boolean isVar,
040                @NotNull SourceElement source
041        ) {
042            super(containingDeclaration, annotations, name, outType, source);
043    
044            this.isVar = isVar;
045        }
046    
047        @Override
048        public boolean isVar() {
049            return isVar;
050        }
051    
052        @Nullable
053        @Override
054        public ConstantValue<?> getCompileTimeInitializer() {
055            if (compileTimeInitializer != null) {
056                return compileTimeInitializer.invoke();
057            }
058            return null;
059        }
060    
061        public void setCompileTimeInitializer(@NotNull NullableLazyValue<ConstantValue<?>> compileTimeInitializer) {
062            assert !isVar() : "Constant value for variable initializer should be recorded only for final variables: " + getName();
063            this.compileTimeInitializer = compileTimeInitializer;
064        }
065    }