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 org.jetbrains.annotations.NotNull; 020 import org.jetbrains.asm4.Type; 021 import org.jetbrains.jet.codegen.state.JetTypeMapper; 022 import org.jetbrains.jet.lang.descriptors.ClassDescriptor; 023 import org.jetbrains.jet.lang.descriptors.ClassKind; 024 import org.jetbrains.jet.lang.resolve.DescriptorUtils; 025 import org.jetbrains.jet.lang.resolve.java.JvmAbi; 026 027 public class FieldInfo { 028 029 @NotNull 030 public static FieldInfo createForSingleton(@NotNull ClassDescriptor fieldClassDescriptor, @NotNull JetTypeMapper typeMapper) { 031 ClassKind kind = fieldClassDescriptor.getKind(); 032 if (kind != ClassKind.OBJECT && kind != ClassKind.CLASS_OBJECT && kind != ClassKind.ENUM_ENTRY) { 033 throw new UnsupportedOperationException(); 034 } 035 036 Type fieldType = typeMapper.mapType(fieldClassDescriptor.getDefaultType()); 037 038 ClassDescriptor ownerDescriptor = kind == ClassKind.OBJECT 039 ? fieldClassDescriptor: DescriptorUtils.getParentOfType(fieldClassDescriptor, ClassDescriptor.class); 040 assert ownerDescriptor != null; 041 Type ownerType = typeMapper.mapType(ownerDescriptor.getDefaultType()); 042 043 String fieldName = kind == ClassKind.ENUM_ENTRY 044 ? fieldClassDescriptor.getName().asString() 045 : fieldClassDescriptor.getKind() == ClassKind.CLASS_OBJECT ? JvmAbi.CLASS_OBJECT_FIELD : JvmAbi.INSTANCE_FIELD; 046 return new FieldInfo(ownerType, fieldType, fieldName, true); 047 } 048 049 050 public static FieldInfo createForHiddenField(@NotNull Type owner, Type fieldType, String fieldName) { 051 return new FieldInfo(owner, fieldType, fieldName, false); 052 } 053 054 private final Type fieldType; 055 056 private final Type ownerType; 057 058 private final String fieldName; 059 060 private final boolean isStatic; 061 062 private FieldInfo(@NotNull Type ownerType, @NotNull Type fieldType, @NotNull String fieldName, @NotNull boolean isStatic) { 063 this.ownerType = ownerType; 064 this.fieldType = fieldType; 065 this.fieldName = fieldName; 066 this.isStatic = isStatic; 067 } 068 069 @NotNull 070 public Type getFieldType() { 071 return fieldType; 072 } 073 074 @NotNull 075 public Type getOwnerType() { 076 return ownerType; 077 } 078 079 @NotNull 080 public String getOwnerInternalName() { 081 return ownerType.getInternalName(); 082 } 083 084 @NotNull 085 public String getFieldName() { 086 return fieldName; 087 } 088 089 @NotNull 090 public boolean isStatic() { 091 return isStatic; 092 } 093 }