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