J2K KotlinJvmBinaryClass & AnnotationAndConstantLoader: convert

This commit is contained in:
Alexander Udalov
2016-04-06 14:48:31 +03:00
parent 9c7e4f3dcc
commit 2ab68d53fa
9 changed files with 93 additions and 112 deletions
@@ -42,7 +42,8 @@ class LocalFileKotlinClass private constructor(
val className: JvmClassName by lazy { JvmClassName.byClassId(classId) } val className: JvmClassName by lazy { JvmClassName.byClassId(classId) }
override fun getLocation(): String = file.absolutePath override val location: String
get() = file.absolutePath
public override fun getFileContents(): ByteArray = fileContents public override fun getFileContents(): ByteArray = fileContents
@@ -33,7 +33,8 @@ class VirtualFileKotlinClass private constructor(
innerClasses: FileBasedKotlinClass.InnerClassesInfo innerClasses: FileBasedKotlinClass.InnerClassesInfo
) : FileBasedKotlinClass(className, classHeader, innerClasses) { ) : FileBasedKotlinClass(className, classHeader, innerClasses) {
override fun getLocation() = file.path override val location: String
get() = file.path
override fun getFileContents(): ByteArray { override fun getFileContents(): ByteArray {
try { try {
@@ -182,7 +182,8 @@ object InlineTestUtil {
val klass = FileBasedKotlinClass.create(file.asByteArray()) { val klass = FileBasedKotlinClass.create(file.asByteArray()) {
className, classHeader, innerClasses -> className, classHeader, innerClasses ->
object : FileBasedKotlinClass(className, classHeader, innerClasses) { object : FileBasedKotlinClass(className, classHeader, innerClasses) {
override fun getLocation(): String = throw UnsupportedOperationException() override val location: String
get() = throw UnsupportedOperationException()
override fun getFileContents(): ByteArray = throw UnsupportedOperationException() override fun getFileContents(): ByteArray = throw UnsupportedOperationException()
override fun hashCode(): Int = throw UnsupportedOperationException() override fun hashCode(): Int = throw UnsupportedOperationException()
override fun equals(other: Any?): Boolean = throw UnsupportedOperationException() override fun equals(other: Any?): Boolean = throw UnsupportedOperationException()
@@ -146,14 +146,14 @@ abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C : Any,
override fun loadValueParameterAnnotations( override fun loadValueParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
message: MessageLite, callableProto: MessageLite,
kind: AnnotatedCallableKind, kind: AnnotatedCallableKind,
parameterIndex: Int, parameterIndex: Int,
proto: ProtoBuf.ValueParameter proto: ProtoBuf.ValueParameter
): List<A> { ): List<A> {
val methodSignature = getCallableSignature(message, container.nameResolver, container.typeTable, kind) val methodSignature = getCallableSignature(callableProto, container.nameResolver, container.typeTable, kind)
if (methodSignature != null) { if (methodSignature != null) {
val index = parameterIndex + computeJvmParameterIndexShift(container, message) val index = parameterIndex + computeJvmParameterIndexShift(container, callableProto)
val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, index) val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, index)
return findClassAndLoadMemberAnnotations(container, paramSignature) return findClassAndLoadMemberAnnotations(container, paramSignature)
} }
@@ -176,10 +176,10 @@ abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C : Any,
override fun loadExtensionReceiverParameterAnnotations( override fun loadExtensionReceiverParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
message: MessageLite, proto: MessageLite,
kind: AnnotatedCallableKind kind: AnnotatedCallableKind
): List<A> { ): List<A> {
val methodSignature = getCallableSignature(message, container.nameResolver, container.typeTable, kind) val methodSignature = getCallableSignature(proto, container.nameResolver, container.typeTable, kind)
if (methodSignature != null) { if (methodSignature != null) {
val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, 0) val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, 0)
return findClassAndLoadMemberAnnotations(container, paramSignature) return findClassAndLoadMemberAnnotations(container, paramSignature)
@@ -188,12 +188,12 @@ abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C : Any,
return emptyList() return emptyList()
} }
override fun loadTypeAnnotations(type: ProtoBuf.Type, nameResolver: NameResolver): List<A> { override fun loadTypeAnnotations(proto: ProtoBuf.Type, nameResolver: NameResolver): List<A> {
return type.getExtension(JvmProtoBuf.typeAnnotation).map { loadTypeAnnotation(it, nameResolver) } return proto.getExtension(JvmProtoBuf.typeAnnotation).map { loadTypeAnnotation(it, nameResolver) }
} }
override fun loadTypeParameterAnnotations(typeParameter: ProtoBuf.TypeParameter, nameResolver: NameResolver): List<A> { override fun loadTypeParameterAnnotations(proto: ProtoBuf.TypeParameter, nameResolver: NameResolver): List<A> {
return typeParameter.getExtension(JvmProtoBuf.typeParameterAnnotation).map { loadTypeAnnotation(it, nameResolver) } return proto.getExtension(JvmProtoBuf.typeParameterAnnotation).map { loadTypeAnnotation(it, nameResolver) }
} }
override fun loadPropertyConstant(container: ProtoContainer, proto: ProtoBuf.Property, expectedType: KotlinType): C? { override fun loadPropertyConstant(container: ProtoContainer, proto: ProtoBuf.Property, expectedType: KotlinType): C? {
@@ -14,74 +14,63 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.load.kotlin; package org.jetbrains.kotlin.load.kotlin
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
import org.jetbrains.kotlin.descriptors.SourceElement; import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader; import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.ClassId;
import org.jetbrains.kotlin.name.Name;
public interface KotlinJvmBinaryClass { interface KotlinJvmBinaryClass {
@NotNull val classId: ClassId
ClassId getClassId();
/** /**
* @return path to the class file (to be reported to the user upon error) * @return path to the class file (to be reported to the user upon error)
*/ */
@NotNull val location: String
String getLocation();
void loadClassAnnotations(@NotNull AnnotationVisitor visitor); fun loadClassAnnotations(visitor: AnnotationVisitor)
void visitMembers(@NotNull MemberVisitor visitor); fun visitMembers(visitor: MemberVisitor)
@NotNull val classHeader: KotlinClassHeader
KotlinClassHeader getClassHeader();
interface MemberVisitor { interface MemberVisitor {
// TODO: abstract signatures for methods and fields instead of ASM 'desc' strings? // TODO: abstract signatures for methods and fields instead of ASM 'desc' strings?
@Nullable fun visitMethod(name: Name, desc: String): MethodAnnotationVisitor?
MethodAnnotationVisitor visitMethod(@NotNull Name name, @NotNull String desc);
@Nullable fun visitField(name: Name, desc: String, initializer: Any?): AnnotationVisitor?
AnnotationVisitor visitField(@NotNull Name name, @NotNull String desc, @Nullable Object initializer);
} }
interface AnnotationVisitor { interface AnnotationVisitor {
@Nullable fun visitAnnotation(classId: ClassId, source: SourceElement): AnnotationArgumentVisitor?
AnnotationArgumentVisitor visitAnnotation(@NotNull ClassId classId, @NotNull SourceElement source);
void visitEnd(); fun visitEnd()
} }
interface MethodAnnotationVisitor extends AnnotationVisitor { interface MethodAnnotationVisitor : AnnotationVisitor {
@Nullable fun visitParameterAnnotation(index: Int, classId: ClassId, source: SourceElement): AnnotationArgumentVisitor?
AnnotationArgumentVisitor visitParameterAnnotation(int index, @NotNull ClassId classId, @NotNull SourceElement source);
} }
interface AnnotationArgumentVisitor { interface AnnotationArgumentVisitor {
// TODO: class literals // TODO: class literals
void visit(@Nullable Name name, @Nullable Object value); fun visit(name: Name?, value: Any?)
void visitEnum(@NotNull Name name, @NotNull ClassId enumClassId, @NotNull Name enumEntryName); fun visitEnum(name: Name, enumClassId: ClassId, enumEntryName: Name)
@Nullable fun visitAnnotation(name: Name, classId: ClassId): AnnotationArgumentVisitor?
AnnotationArgumentVisitor visitAnnotation(@NotNull Name name, @NotNull ClassId classId);
@Nullable fun visitArray(name: Name): AnnotationArrayArgumentVisitor?
AnnotationArrayArgumentVisitor visitArray(@NotNull Name name);
void visitEnd(); fun visitEnd()
} }
interface AnnotationArrayArgumentVisitor { interface AnnotationArrayArgumentVisitor {
void visit(@Nullable Object value); fun visit(value: Any?)
void visitEnum(@NotNull ClassId enumClassId, @NotNull Name enumEntryName); fun visitEnum(enumClassId: ClassId, enumEntryName: Name)
void visitEnd(); fun visitEnd()
} }
} }
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.load.java.structure.reflect.isEnumClassOrSpecialized
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinaryClass import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinaryClass
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
import org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor import org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import java.lang.reflect.Constructor import java.lang.reflect.Constructor
import java.lang.reflect.Field import java.lang.reflect.Field
@@ -41,7 +42,7 @@ private val TYPES_ELIGIBLE_FOR_SIMPLE_VISIT = setOf<Class<*>>(
class ReflectKotlinClass private constructor( class ReflectKotlinClass private constructor(
val klass: Class<*>, val klass: Class<*>,
private val classHeader: KotlinClassHeader override val classHeader: KotlinClassHeader
) : KotlinJvmBinaryClass { ) : KotlinJvmBinaryClass {
companion object Factory { companion object Factory {
@@ -53,11 +54,11 @@ class ReflectKotlinClass private constructor(
} }
} }
override fun getLocation() = klass.name.replace('.', '/') + ".class" override val location: String
get() = klass.name.replace('.', '/') + ".class"
override fun getClassId() = klass.classId override val classId: ClassId
get() = klass.classId
override fun getClassHeader() = classHeader
override fun loadClassAnnotations(visitor: KotlinJvmBinaryClass.AnnotationVisitor) { override fun loadClassAnnotations(visitor: KotlinJvmBinaryClass.AnnotationVisitor) {
ReflectClassStructure.loadClassAnnotations(klass, visitor) ReflectClassStructure.loadClassAnnotations(klass, visitor)
@@ -14,70 +14,58 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.serialization.deserialization; package org.jetbrains.kotlin.serialization.deserialization
import com.google.protobuf.MessageLite; import com.google.protobuf.MessageLite
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.serialization.ProtoBuf;
import org.jetbrains.kotlin.types.KotlinType;
import java.util.List;
// The MessageLite instance everywhere should be Constructor, Function or Property // The MessageLite instance everywhere should be Constructor, Function or Property
// TODO: simplify this interface // TODO: simplify this interface
public interface AnnotationAndConstantLoader<A, C, T> { interface AnnotationAndConstantLoader<A : Any, C : Any, T : Any> {
@NotNull fun loadClassAnnotations(
List<A> loadClassAnnotations( classProto: ProtoBuf.Class,
@NotNull ProtoBuf.Class classProto, nameResolver: NameResolver
@NotNull NameResolver nameResolver ): List<A>
);
@NotNull fun loadCallableAnnotations(
List<T> loadCallableAnnotations( container: ProtoContainer,
@NotNull ProtoContainer container, proto: MessageLite,
@NotNull MessageLite message, kind: AnnotatedCallableKind
@NotNull AnnotatedCallableKind kind ): List<T>
);
@NotNull fun loadEnumEntryAnnotations(
List<A> loadEnumEntryAnnotations( container: ProtoContainer,
@NotNull ProtoContainer container, proto: ProtoBuf.EnumEntry
@NotNull ProtoBuf.EnumEntry proto ): List<A>
);
@NotNull fun loadValueParameterAnnotations(
List<A> loadValueParameterAnnotations( container: ProtoContainer,
@NotNull ProtoContainer container, callableProto: MessageLite,
@NotNull MessageLite message, kind: AnnotatedCallableKind,
@NotNull AnnotatedCallableKind kind, parameterIndex: Int,
int parameterIndex, proto: ProtoBuf.ValueParameter
@NotNull ProtoBuf.ValueParameter proto ): List<A>
);
@NotNull fun loadExtensionReceiverParameterAnnotations(
List<A> loadExtensionReceiverParameterAnnotations( container: ProtoContainer,
@NotNull ProtoContainer container, proto: MessageLite,
@NotNull MessageLite message, kind: AnnotatedCallableKind
@NotNull AnnotatedCallableKind kind ): List<A>
);
@NotNull fun loadTypeAnnotations(
List<A> loadTypeAnnotations( proto: ProtoBuf.Type,
@NotNull ProtoBuf.Type type, nameResolver: NameResolver
@NotNull NameResolver nameResolver ): List<A>
);
@NotNull fun loadTypeParameterAnnotations(
List<A> loadTypeParameterAnnotations( proto: ProtoBuf.TypeParameter,
@NotNull ProtoBuf.TypeParameter typeParameter, nameResolver: NameResolver
@NotNull NameResolver nameResolver ): List<A>
);
@Nullable fun loadPropertyConstant(
C loadPropertyConstant( container: ProtoContainer,
@NotNull ProtoContainer container, proto: ProtoBuf.Property,
@NotNull ProtoBuf.Property proto, expectedType: KotlinType
@NotNull KotlinType expectedType ): C?
);
} }
@@ -64,7 +64,7 @@ class AnnotationAndConstantLoaderImpl(
override fun loadValueParameterAnnotations( override fun loadValueParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
message: MessageLite, callableProto: MessageLite,
kind: AnnotatedCallableKind, kind: AnnotatedCallableKind,
parameterIndex: Int, parameterIndex: Int,
proto: ProtoBuf.ValueParameter proto: ProtoBuf.ValueParameter
@@ -75,7 +75,7 @@ class AnnotationAndConstantLoaderImpl(
override fun loadExtensionReceiverParameterAnnotations( override fun loadExtensionReceiverParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
message: MessageLite, proto: MessageLite,
kind: AnnotatedCallableKind kind: AnnotatedCallableKind
): List<AnnotationDescriptor> = emptyList() ): List<AnnotationDescriptor> = emptyList()
@@ -57,7 +57,7 @@ class AnnotationLoaderForStubBuilderImpl(
override fun loadValueParameterAnnotations( override fun loadValueParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
message: MessageLite, callableProto: MessageLite,
kind: AnnotatedCallableKind, kind: AnnotatedCallableKind,
parameterIndex: Int, parameterIndex: Int,
proto: ProtoBuf.ValueParameter proto: ProtoBuf.ValueParameter
@@ -66,7 +66,7 @@ class AnnotationLoaderForStubBuilderImpl(
override fun loadExtensionReceiverParameterAnnotations( override fun loadExtensionReceiverParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
message: MessageLite, proto: MessageLite,
kind: AnnotatedCallableKind kind: AnnotatedCallableKind
): List<ClassId> = emptyList() ): List<ClassId> = emptyList()