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        ) {
050            super(correspondingProperty.getContainingDeclaration(), annotations, name);
051            this.modality = modality;
052            this.visibility = visibility;
053            this.correspondingProperty = correspondingProperty;
054            this.hasBody = hasBody;
055            this.isDefault = isDefault;
056            this.kind = kind;
057        }
058    
059        @Override
060        public boolean hasBody() {
061            return hasBody;
062        }
063    
064        @Override
065        public boolean isDefault() {
066            return isDefault;
067        }
068    
069        @NotNull
070        @Override
071        public Kind getKind() {
072            return kind;
073        }
074    
075        @NotNull
076        @Override
077        public FunctionDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
078            throw new UnsupportedOperationException(); // TODO
079        }
080    
081        @NotNull
082        @Override
083        public List<TypeParameterDescriptor> getTypeParameters() {
084            return Collections.emptyList();
085        }
086    
087        @Override
088        public boolean hasStableParameterNames() {
089            return false;
090        }
091    
092        @Override
093        public boolean hasSynthesizedParameterNames() {
094            return false;
095        }
096    
097        @NotNull
098        @Override
099        public Modality getModality() {
100            return modality;
101        }
102    
103        @NotNull
104        @Override
105        public Visibility getVisibility() {
106            return visibility;
107        }
108    
109        public void setVisibility(Visibility visibility) {
110            this.visibility = visibility;
111        }
112    
113        @Override
114        @NotNull
115        public PropertyDescriptor getCorrespondingProperty() {
116            return correspondingProperty;
117        }
118    
119        @Nullable
120        @Override
121        public ReceiverParameterDescriptor getReceiverParameter() {
122            return getCorrespondingProperty().getReceiverParameter();
123        }
124    
125        @Nullable
126        @Override
127        public ReceiverParameterDescriptor getExpectedThisObject() {
128            return getCorrespondingProperty().getExpectedThisObject();
129        }
130    
131        @NotNull
132        @Override
133        public PropertyAccessorDescriptor copy(
134                DeclarationDescriptor newOwner,
135                Modality modality,
136                Visibility visibility,
137                Kind kind,
138                boolean copyOverrides
139        ) {
140            throw new UnsupportedOperationException("Accessors must be copied by the corresponding property");
141        }
142    
143        @NotNull
144        protected Set<PropertyAccessorDescriptor> getOverriddenDescriptors(boolean isGetter) {
145            Set<? extends PropertyDescriptor> overriddenProperties = getCorrespondingProperty().getOverriddenDescriptors();
146            Set<PropertyAccessorDescriptor> overriddenAccessors = Sets.newLinkedHashSet(); // LinkedHashSet for determinism
147            for (PropertyDescriptor overriddenProperty : overriddenProperties) {
148                PropertyAccessorDescriptor accessorDescriptor = isGetter ? overriddenProperty.getGetter() : overriddenProperty.getSetter();
149                if (accessorDescriptor != null) {
150                    overriddenAccessors.add(accessorDescriptor);
151                }
152            }
153            return overriddenAccessors;
154        }
155    
156        @Override
157        public void addOverriddenDescriptor(@NotNull CallableMemberDescriptor overridden) {
158            throw new IllegalStateException();
159        }
160    
161        @NotNull
162        @Override
163        public abstract PropertyAccessorDescriptor getOriginal();
164    }