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.*;
022 import org.jetbrains.kotlin.descriptors.annotations.Annotations;
023 import org.jetbrains.kotlin.name.Name;
024 import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant;
025 import org.jetbrains.kotlin.storage.NullableLazyValue;
026 import org.jetbrains.kotlin.types.JetType;
027 import org.jetbrains.kotlin.types.LazyType;
028
029 import java.util.Collections;
030 import java.util.List;
031 import java.util.Set;
032
033 public abstract class VariableDescriptorImpl extends DeclarationDescriptorNonRootImpl implements VariableDescriptor {
034 private JetType outType;
035 private NullableLazyValue<CompileTimeConstant<?>> compileTimeInitializer;
036
037 public VariableDescriptorImpl(
038 @NotNull DeclarationDescriptor containingDeclaration,
039 @NotNull Annotations annotations,
040 @NotNull Name name,
041 @Nullable JetType outType,
042 @NotNull SourceElement source
043 ) {
044 super(containingDeclaration, annotations, name, source);
045
046 this.outType = outType;
047 }
048
049 @NotNull
050 @Override
051 public JetType getType() {
052 return outType;
053 }
054
055 public void setOutType(JetType outType) {
056 assert this.outType == null;
057 this.outType = outType;
058 }
059
060 @Nullable
061 @Override
062 public CompileTimeConstant<?> getCompileTimeInitializer() {
063 // Force computation and setting of compileTimeInitializer, if needed
064 if (compileTimeInitializer == null && outType instanceof LazyType) {
065 outType.getConstructor();
066 }
067
068 if (compileTimeInitializer != null) {
069 return compileTimeInitializer.invoke();
070 }
071 return null;
072 }
073
074 public void setCompileTimeInitializer(@NotNull NullableLazyValue<CompileTimeConstant<?>> compileTimeInitializer) {
075 assert !isVar() : "Compile-time value for property initializer should be recorded only for final variables " + getName();
076 this.compileTimeInitializer = compileTimeInitializer;
077 }
078
079 @Override
080 @NotNull
081 public VariableDescriptor getOriginal() {
082 return (VariableDescriptor) super.getOriginal();
083 }
084
085 @NotNull
086 @Override
087 public List<ValueParameterDescriptor> getValueParameters() {
088 return Collections.emptyList();
089 }
090
091 @Override
092 public boolean hasStableParameterNames() {
093 return false;
094 }
095
096 @Override
097 public boolean hasSynthesizedParameterNames() {
098 return false;
099 }
100
101 @NotNull
102 @Override
103 public Set<? extends CallableDescriptor> getOverriddenDescriptors() {
104 return Collections.emptySet();
105 }
106
107 @NotNull
108 @Override
109 public List<TypeParameterDescriptor> getTypeParameters() {
110 return Collections.emptyList();
111 }
112
113 @Override
114 public ReceiverParameterDescriptor getExtensionReceiverParameter() {
115 return ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER;
116 }
117
118 @Override
119 public ReceiverParameterDescriptor getDispatchReceiverParameter() {
120 return ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER;
121 }
122
123 @NotNull
124 @Override
125 public JetType getReturnType() {
126 return getType();
127 }
128 }