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.js.translate.reference;
018    
019    import org.jetbrains.kotlin.js.backend.ast.JsExpression;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.kotlin.descriptors.PropertyDescriptor;
022    import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptorKt;
023    import org.jetbrains.kotlin.js.translate.context.TranslationContext;
024    import org.jetbrains.kotlin.js.translate.general.AbstractTranslator;
025    import org.jetbrains.kotlin.psi.KtSimpleNameExpression;
026    
027    import static org.jetbrains.kotlin.js.translate.utils.BindingUtils.getDescriptorForReferenceExpression;
028    import static org.jetbrains.kotlin.js.translate.utils.TranslationUtils.assignmentToBackingField;
029    import static org.jetbrains.kotlin.js.translate.utils.TranslationUtils.backingFieldReference;
030    
031    public final class BackingFieldAccessTranslator extends AbstractTranslator implements AccessTranslator {
032    
033        @NotNull
034        private final PropertyDescriptor descriptor;
035    
036        /*package*/
037        public static BackingFieldAccessTranslator newInstance(@NotNull KtSimpleNameExpression expression,
038                                                        @NotNull TranslationContext context) {
039            PropertyDescriptor referencedProperty = SyntheticFieldDescriptorKt.getReferencedProperty(
040                    getDescriptorForReferenceExpression(context.bindingContext(), expression)
041            );
042            assert referencedProperty != null;
043            return new BackingFieldAccessTranslator(referencedProperty, context);
044        }
045    
046        private BackingFieldAccessTranslator(@NotNull PropertyDescriptor descriptor, @NotNull TranslationContext context) {
047            super(context);
048            this.descriptor = descriptor;
049        }
050    
051        @NotNull
052        @Override
053        public JsExpression translateAsGet() {
054            return backingFieldReference(context(), descriptor);
055        }
056    
057        @NotNull
058        @Override
059        public JsExpression translateAsSet(@NotNull JsExpression setTo) {
060            return assignmentToBackingField(context(), descriptor, setTo);
061        }
062    
063        @NotNull
064        @Override
065        public AccessTranslator getCached() {
066            return this;
067        }
068    }