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