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                @Nullable PropertySetterDescriptor original
047        ) {
048            super(modality, visibility, correspondingProperty, annotations, Name.special("<set-" + correspondingProperty.getName() + ">"), hasBody, isDefault, kind);
049            this.original = original != null ? original : this;
050        }
051    
052        public void initialize(@NotNull ValueParameterDescriptor parameter) {
053            assert this.parameter == null;
054            this.parameter = parameter;
055        }
056    
057        public void initializeDefault() {
058            assert parameter == null;
059            parameter = createSetterParameter(this, getCorrespondingProperty().getReturnType());
060        }
061    
062        public static ValueParameterDescriptorImpl createSetterParameter(
063                @NotNull PropertySetterDescriptor setterDescriptor,
064                @NotNull JetType type
065        ) {
066            return new ValueParameterDescriptorImpl(
067                    setterDescriptor, null, 0, Annotations.EMPTY, Name.special("<set-?>"), type, false, null
068            );
069        }
070    
071        @NotNull
072        @Override
073        public Set<? extends PropertyAccessorDescriptor> getOverriddenDescriptors() {
074            return super.getOverriddenDescriptors(false);
075        }
076    
077        @NotNull
078        @Override
079        public List<ValueParameterDescriptor> getValueParameters() {
080            if (parameter == null) {
081                throw new IllegalStateException();
082            }
083            return Collections.singletonList(parameter);
084        }
085    
086        @NotNull
087        @Override
088        public JetType getReturnType() {
089            return KotlinBuiltIns.getInstance().getUnitType();
090        }
091    
092        @Override
093        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
094            return visitor.visitPropertySetterDescriptor(this, data);
095        }
096    
097        @NotNull
098        @Override
099        public PropertySetterDescriptor getOriginal() {
100            return this.original;
101        }
102    }