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.types.TypeSubstitutor;
025    
026    import java.util.ArrayList;
027    import java.util.Collection;
028    import java.util.Collections;
029    import java.util.List;
030    
031    public abstract class PropertyAccessorDescriptorImpl extends DeclarationDescriptorNonRootImpl implements PropertyAccessorDescriptor {
032    
033        private final boolean hasBody;
034        private final boolean isDefault;
035        private final Modality modality;
036        private final PropertyDescriptor correspondingProperty;
037        private final Kind kind;
038        private Visibility visibility;
039    
040        public PropertyAccessorDescriptorImpl(
041                @NotNull Modality modality,
042                @NotNull Visibility visibility,
043                @NotNull PropertyDescriptor correspondingProperty,
044                @NotNull Annotations annotations,
045                @NotNull Name name,
046                boolean hasBody,
047                boolean isDefault,
048                Kind kind,
049                @NotNull SourceElement source
050        ) {
051            super(correspondingProperty.getContainingDeclaration(), annotations, name, source);
052            this.modality = modality;
053            this.visibility = visibility;
054            this.correspondingProperty = correspondingProperty;
055            this.hasBody = hasBody;
056            this.isDefault = isDefault;
057            this.kind = kind;
058        }
059    
060        @Override
061        public boolean hasBody() {
062            return hasBody;
063        }
064    
065        @Override
066        public boolean isDefault() {
067            return isDefault;
068        }
069    
070        @NotNull
071        @Override
072        public Kind getKind() {
073            return kind;
074        }
075    
076        @Override
077        public boolean isOperator() {
078            return false;
079        }
080    
081        @NotNull
082        @Override
083        public FunctionDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
084            throw new UnsupportedOperationException(); // TODO
085        }
086    
087        @NotNull
088        @Override
089        public List<TypeParameterDescriptor> getTypeParameters() {
090            return Collections.emptyList();
091        }
092    
093        @Override
094        public boolean hasStableParameterNames() {
095            return false;
096        }
097    
098        @Override
099        public boolean hasSynthesizedParameterNames() {
100            return false;
101        }
102    
103        @NotNull
104        @Override
105        public Modality getModality() {
106            return modality;
107        }
108    
109        @NotNull
110        @Override
111        public Visibility getVisibility() {
112            return visibility;
113        }
114    
115        public void setVisibility(Visibility visibility) {
116            this.visibility = visibility;
117        }
118    
119        @Override
120        @NotNull
121        public PropertyDescriptor getCorrespondingProperty() {
122            return correspondingProperty;
123        }
124    
125        @Nullable
126        @Override
127        public ReceiverParameterDescriptor getExtensionReceiverParameter() {
128            return getCorrespondingProperty().getExtensionReceiverParameter();
129        }
130    
131        @Nullable
132        @Override
133        public ReceiverParameterDescriptor getDispatchReceiverParameter() {
134            return getCorrespondingProperty().getDispatchReceiverParameter();
135        }
136    
137        @NotNull
138        @Override
139        public PropertyAccessorDescriptor copy(
140                DeclarationDescriptor newOwner,
141                Modality modality,
142                Visibility visibility,
143                Kind kind,
144                boolean copyOverrides
145        ) {
146            throw new UnsupportedOperationException("Accessors must be copied by the corresponding property");
147        }
148    
149        @NotNull
150        protected Collection<PropertyAccessorDescriptor> getOverriddenDescriptors(boolean isGetter) {
151            Collection<PropertyAccessorDescriptor> result = new ArrayList<PropertyAccessorDescriptor>(0);
152            for (PropertyDescriptor overriddenProperty : getCorrespondingProperty().getOverriddenDescriptors()) {
153                PropertyAccessorDescriptor accessorDescriptor = isGetter ? overriddenProperty.getGetter() : overriddenProperty.getSetter();
154                if (accessorDescriptor != null) {
155                    result.add(accessorDescriptor);
156                }
157            }
158            return result;
159        }
160    
161        @Override
162        public void addOverriddenDescriptor(@NotNull CallableMemberDescriptor overridden) {
163            throw new IllegalStateException();
164        }
165    
166        @NotNull
167        @Override
168        public abstract PropertyAccessorDescriptor getOriginal();
169    }