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.KotlinType;
025    
026    import java.util.Collection;
027    import java.util.Collections;
028    import java.util.List;
029    
030    import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getBuiltIns;
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                boolean isExternal,
046                @NotNull Kind kind,
047                @Nullable PropertySetterDescriptor original,
048                @NotNull SourceElement source
049        ) {
050            super(modality, visibility, correspondingProperty, annotations, Name.special("<set-" + correspondingProperty.getName() + ">"),
051                  hasBody, isDefault, isExternal, kind, source);
052            this.original = original != null ? original : this;
053        }
054    
055        public void initialize(@NotNull ValueParameterDescriptor parameter) {
056            assert this.parameter == null;
057            this.parameter = parameter;
058        }
059    
060        public void initializeDefault() {
061            assert parameter == null;
062            parameter = createSetterParameter(this, getCorrespondingProperty().getReturnType());
063        }
064    
065        public static ValueParameterDescriptorImpl createSetterParameter(
066                @NotNull PropertySetterDescriptor setterDescriptor,
067                @NotNull KotlinType type
068        ) {
069            return new ValueParameterDescriptorImpl(
070                    setterDescriptor, null, 0, Annotations.Companion.getEMPTY(), Name.special("<set-?>"), type,
071                    /* declaresDefaultValue = */ false,
072                    /* isCrossinline = */ false,
073                    /* isNoinline = */ false,
074                    null, SourceElement.NO_SOURCE
075            );
076        }
077    
078        @NotNull
079        @Override
080        @SuppressWarnings("unchecked")
081        public Collection<? extends PropertySetterDescriptor> getOverriddenDescriptors() {
082            return (Collection) super.getOverriddenDescriptors(false);
083        }
084    
085        @NotNull
086        @Override
087        public List<ValueParameterDescriptor> getValueParameters() {
088            if (parameter == null) {
089                throw new IllegalStateException();
090            }
091            return Collections.singletonList(parameter);
092        }
093    
094        @NotNull
095        @Override
096        public KotlinType getReturnType() {
097            return getBuiltIns(this).getUnitType();
098        }
099    
100        @Override
101        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
102            return visitor.visitPropertySetterDescriptor(this, data);
103        }
104    
105        @NotNull
106        @Override
107        public PropertySetterDescriptor getOriginal() {
108            return this.original;
109        }
110    }