001    /*
002     * Copyright 2010-2013 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.descriptors.impl;
018    
019    import com.google.common.collect.Sets;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.jet.lang.descriptors.*;
023    import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
024    import org.jetbrains.jet.lang.resolve.name.Name;
025    import org.jetbrains.jet.lang.types.TypeSubstitutor;
026    
027    import java.util.Collections;
028    import java.util.List;
029    import java.util.Set;
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        @NotNull
077        @Override
078        public FunctionDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
079            throw new UnsupportedOperationException(); // TODO
080        }
081    
082        @NotNull
083        @Override
084        public List<TypeParameterDescriptor> getTypeParameters() {
085            return Collections.emptyList();
086        }
087    
088        @Override
089        public boolean hasStableParameterNames() {
090            return false;
091        }
092    
093        @Override
094        public boolean hasSynthesizedParameterNames() {
095            return false;
096        }
097    
098        @NotNull
099        @Override
100        public Modality getModality() {
101            return modality;
102        }
103    
104        @NotNull
105        @Override
106        public Visibility getVisibility() {
107            return visibility;
108        }
109    
110        public void setVisibility(Visibility visibility) {
111            this.visibility = visibility;
112        }
113    
114        @Override
115        @NotNull
116        public PropertyDescriptor getCorrespondingProperty() {
117            return correspondingProperty;
118        }
119    
120        @Nullable
121        @Override
122        public ReceiverParameterDescriptor getReceiverParameter() {
123            return getCorrespondingProperty().getReceiverParameter();
124        }
125    
126        @Nullable
127        @Override
128        public ReceiverParameterDescriptor getExpectedThisObject() {
129            return getCorrespondingProperty().getExpectedThisObject();
130        }
131    
132        @NotNull
133        @Override
134        public PropertyAccessorDescriptor copy(
135                DeclarationDescriptor newOwner,
136                Modality modality,
137                Visibility visibility,
138                Kind kind,
139                boolean copyOverrides
140        ) {
141            throw new UnsupportedOperationException("Accessors must be copied by the corresponding property");
142        }
143    
144        @NotNull
145        protected Set<PropertyAccessorDescriptor> getOverriddenDescriptors(boolean isGetter) {
146            Set<? extends PropertyDescriptor> overriddenProperties = getCorrespondingProperty().getOverriddenDescriptors();
147            Set<PropertyAccessorDescriptor> overriddenAccessors = Sets.newLinkedHashSet(); // LinkedHashSet for determinism
148            for (PropertyDescriptor overriddenProperty : overriddenProperties) {
149                PropertyAccessorDescriptor accessorDescriptor = isGetter ? overriddenProperty.getGetter() : overriddenProperty.getSetter();
150                if (accessorDescriptor != null) {
151                    overriddenAccessors.add(accessorDescriptor);
152                }
153            }
154            return overriddenAccessors;
155        }
156    
157        @Override
158        public void addOverriddenDescriptor(@NotNull CallableMemberDescriptor overridden) {
159            throw new IllegalStateException();
160        }
161    
162        @NotNull
163        @Override
164        public abstract PropertyAccessorDescriptor getOriginal();
165    }