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.k2js.translate.initializer;
018    
019    import com.google.dart.compiler.backend.js.ast.*;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.jet.lang.descriptors.*;
023    import org.jetbrains.jet.lang.psi.JetExpression;
024    import org.jetbrains.jet.lang.psi.JetObjectDeclaration;
025    import org.jetbrains.jet.lang.psi.JetProperty;
026    import org.jetbrains.k2js.translate.context.Namer;
027    import org.jetbrains.k2js.translate.context.TranslationContext;
028    import org.jetbrains.k2js.translate.declaration.ClassTranslator;
029    import org.jetbrains.k2js.translate.general.Translation;
030    import org.jetbrains.k2js.translate.utils.JsAstUtils;
031    
032    import java.util.List;
033    
034    import static org.jetbrains.k2js.translate.utils.BindingUtils.getClassDescriptor;
035    import static org.jetbrains.k2js.translate.utils.JsAstUtils.assignment;
036    import static org.jetbrains.k2js.translate.utils.TranslationUtils.assignmentToBackingField;
037    
038    public final class InitializerUtils {
039        private InitializerUtils() {
040        }
041    
042        @NotNull
043        public static JsStatement generateInitializerForProperty(@NotNull TranslationContext context,
044                @NotNull PropertyDescriptor descriptor,
045                @NotNull JsExpression value) {
046            return assignmentToBackingField(context, descriptor, value).makeStmt();
047        }
048    
049        @Nullable
050        public static JsStatement generateInitializerForDelegate(@NotNull TranslationContext context, @NotNull JetProperty property) {
051            JetExpression delegate = property.getDelegateExpression();
052            if (delegate != null) {
053                JsExpression value = Translation.translateAsExpression(delegate, context);
054                String name = property.getName();
055                assert name != null: "Delegate property must have name";
056                return JsAstUtils.defineSimpleProperty(Namer.getDelegateName(name), value);
057            }
058            return null;
059        }
060    
061        public static void generateObjectInitializer(
062                @NotNull JetObjectDeclaration declaration,
063                @NotNull List<JsStatement> initializers,
064                @NotNull TranslationContext context
065        ) {
066            JsExpression value = ClassTranslator.generateObjectLiteral(declaration, context);
067            ClassDescriptor descriptor = getClassDescriptor(context.bindingContext(), declaration);
068            JsExpression expression = assignment(new JsNameRef(descriptor.getName().asString(), JsLiteral.THIS), value);
069            initializers.add(expression.makeStmt());
070        }
071    
072        public static JsPropertyInitializer createClassObjectInitializer(JsExpression value, TranslationContext context) {
073            JsStringLiteral classObjectInitStr = context.program().getStringLiteral(Namer.getNameForClassObjectInitializer());
074            return new JsPropertyInitializer(classObjectInitStr, value);
075        }
076    }