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 org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.descriptors.*;
022    import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
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.JetType;
026    import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
027    
028    import java.util.Collections;
029    import java.util.List;
030    import java.util.Set;
031    
032    public class PropertySetterDescriptorImpl extends PropertyAccessorDescriptorImpl implements PropertySetterDescriptor {
033    
034        private ValueParameterDescriptor parameter;
035        @NotNull
036        private final PropertySetterDescriptor original;
037    
038        public PropertySetterDescriptorImpl(
039                @NotNull PropertyDescriptor correspondingProperty,
040                @NotNull Annotations annotations,
041                @NotNull Modality modality,
042                @NotNull Visibility visibility,
043                boolean hasBody,
044                boolean isDefault,
045                @NotNull Kind kind
046        ) {
047            this(correspondingProperty, annotations, modality, visibility, hasBody, isDefault, kind, null);
048        }
049    
050        public PropertySetterDescriptorImpl(
051                @NotNull PropertyDescriptor correspondingProperty,
052                @NotNull Annotations annotations,
053                @NotNull Modality modality,
054                @NotNull Visibility visibility,
055                boolean hasBody,
056                boolean isDefault,
057                @NotNull Kind kind,
058                @Nullable PropertySetterDescriptor original
059        ) {
060            super(modality, visibility, correspondingProperty, annotations, Name.special("<set-" + correspondingProperty.getName() + ">"), hasBody, isDefault, kind);
061            this.original = original != null ? original : this;
062        }
063    
064        public void initialize(@NotNull ValueParameterDescriptor parameter) {
065            assert this.parameter == null;
066            this.parameter = parameter;
067        }
068    
069        public void initializeDefault() {
070            assert parameter == null;
071            parameter = createSetterParameter(this, getCorrespondingProperty().getReturnType());
072        }
073    
074        public static ValueParameterDescriptorImpl createSetterParameter(
075                @NotNull PropertySetterDescriptor setterDescriptor,
076                @NotNull JetType type
077        ) {
078            return new ValueParameterDescriptorImpl(
079                    setterDescriptor, 0, Annotations.EMPTY, Name.special("<set-?>"), type, false, null
080            );
081        }
082    
083        @NotNull
084        @Override
085        public Set<? extends PropertyAccessorDescriptor> getOverriddenDescriptors() {
086            return super.getOverriddenDescriptors(false);
087        }
088    
089        @NotNull
090        @Override
091        public List<ValueParameterDescriptor> getValueParameters() {
092            if (parameter == null) {
093                throw new IllegalStateException();
094            }
095            return Collections.singletonList(parameter);
096        }
097    
098        @NotNull
099        @Override
100        public JetType getReturnType() {
101            return KotlinBuiltIns.getInstance().getUnitType();
102        }
103    
104        @Override
105        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
106            return visitor.visitPropertySetterDescriptor(this, data);
107        }
108    
109        @NotNull
110        @Override
111        public PropertySetterDescriptor getOriginal() {
112            return this.original;
113        }
114    }