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.codegen;
018    
019    import com.intellij.util.ArrayUtil;
020    import kotlin.Function0;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.jet.codegen.context.FieldOwnerContext;
023    import org.jetbrains.jet.codegen.state.GenerationState;
024    import org.jetbrains.jet.lang.psi.*;
025    import org.jetbrains.org.objectweb.asm.Type;
026    
027    import static org.jetbrains.jet.codegen.AsmUtil.writeKotlinSyntheticClassAnnotation;
028    import static org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass;
029    import static org.jetbrains.org.objectweb.asm.Opcodes.*;
030    
031    public class PackagePartCodegen extends MemberCodegen<JetFile> {
032        private final Type packagePartType;
033    
034        public PackagePartCodegen(
035                @NotNull ClassBuilder v,
036                @NotNull JetFile file,
037                @NotNull Type packagePartType,
038                @NotNull FieldOwnerContext context,
039                @NotNull GenerationState state
040        ) {
041            super(state, null, context, file, v);
042            this.packagePartType = packagePartType;
043        }
044    
045        @Override
046        protected void generateDeclaration() {
047            v.defineClass(element, V1_6,
048                          ACC_PUBLIC | ACC_FINAL,
049                          packagePartType.getInternalName(),
050                          null,
051                          "java/lang/Object",
052                          ArrayUtil.EMPTY_STRING_ARRAY
053            );
054            v.visitSource(element.getName(), null);
055        }
056    
057        @Override
058        protected void generateBody() {
059            for (JetDeclaration declaration : element.getDeclarations()) {
060                if (declaration instanceof JetNamedFunction || declaration instanceof JetProperty) {
061                    genFunctionOrProperty((JetTypeParameterListOwner) declaration, v);
062                }
063            }
064    
065            if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
066                generateInitializers(new Function0<ExpressionCodegen>() {
067                    @Override
068                    public ExpressionCodegen invoke() {
069                        return createOrGetClInitCodegen();
070                    }
071                });
072            }
073        }
074    
075        @Override
076        protected void generateSyntheticParts() {
077            if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
078                generatePropertyMetadataArrayFieldIfNeeded(packagePartType);
079            }
080        }
081    
082        @Override
083        protected void generateKotlinAnnotation() {
084            writeKotlinSyntheticClassAnnotation(v, KotlinSyntheticClass.Kind.PACKAGE_PART);
085        }
086    }