From 27c40edeb4f80dcaf3cdc1fafab53886392ae371 Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Fri, 10 Apr 2015 15:42:11 +0300 Subject: [PATCH] InlineTestUtil convertion --- .../kotlin/codegen/InlineTestUtil.kt | 196 ++++++------------ 1 file changed, 61 insertions(+), 135 deletions(-) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/InlineTestUtil.kt b/compiler/tests/org/jetbrains/kotlin/codegen/InlineTestUtil.kt index 322a29e83a3..27f858af72f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/InlineTestUtil.kt +++ b/compiler/tests/org/jetbrains/kotlin/codegen/InlineTestUtil.kt @@ -16,174 +16,100 @@ package org.jetbrains.kotlin.codegen; -import com.intellij.openapi.util.Ref; -import com.intellij.openapi.util.text.StringUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.load.java.JvmAbi; -import org.jetbrains.kotlin.resolve.jvm.JvmClassName; -import org.jetbrains.kotlin.load.kotlin.PackageClassUtils; -import org.jetbrains.kotlin.name.FqName; -import org.jetbrains.kotlin.backend.common.output.OutputFile; -import org.jetbrains.org.objectweb.asm.*; -import org.jetbrains.org.objectweb.asm.tree.MethodNode; +import com.intellij.openapi.util.Ref +import com.intellij.openapi.util.text.StringUtil +import org.jetbrains.kotlin.backend.common.output.OutputFile +import org.jetbrains.kotlin.load.java.JvmAbi +import org.jetbrains.kotlin.load.kotlin.PackageClassUtils +import org.jetbrains.kotlin.resolve.jvm.JvmClassName +import org.jetbrains.org.objectweb.asm.* +import org.jetbrains.org.objectweb.asm.tree.MethodNode +import java.util.ArrayList +import java.util.HashSet -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +public object InlineTestUtil { -public class InlineTestUtil { + public val INLINE_ANNOTATION_CLASS: String = "kotlin/inline" - public static final String INLINE_ANNOTATION_CLASS = "kotlin/inline"; + public fun checkNoCallsToInline(files: List) { + val inlinedMethods = collectInlineMethods(files) + assert(!inlinedMethods.isEmpty(), "There are no inline methods") - public static void checkNoCallsToInline(List files) { - Set inlinedMethods = collectInlineMethods(files); - assert !inlinedMethods.isEmpty() : "There are no inline methods"; - - List notInlinedCalls = checkInlineNotInvoked(files, inlinedMethods); - assert notInlinedCalls.isEmpty() : "All inline methods should be inlined but " + StringUtil.join(notInlinedCalls, "\n"); + val notInlinedCalls = checkInlineNotInvoked(files, inlinedMethods) + assert(notInlinedCalls.isEmpty()) { "All inline methods should be inlined but " + StringUtil.join(notInlinedCalls, "\n") } } - private static Set collectInlineMethods(List files) { - final Set inlineMethods = new HashSet(); + private fun collectInlineMethods(files: List): Set { + val inlineMethods = HashSet() - for (OutputFile file : files) { - ClassReader cr = new ClassReader(file.asByteArray()); - final String[] className = {null}; + for (file in files) { + val cr = ClassReader(file.asByteArray()) + var className: String? = null - cr.accept(new ClassVisitor(Opcodes.ASM4) { + cr.accept(object : ClassVisitor(Opcodes.ASM4) { - @Override - public void visit(int version, int access, @NotNull String name, String signature, String superName, String[] interfaces) { - className[0] = name; - super.visit(version, access, name, signature, superName, interfaces); + override fun visit(version: Int, access: Int, name: String, signature: String?, superName: String?, interfaces: Array?) { + className = name + super.visit(version, access, name, signature, superName, interfaces) } - @Override - public MethodVisitor visitMethod( - int access, @NotNull String name, @NotNull String desc, String signature, String[] exceptions - ) { - return new MethodNode(Opcodes.ASM4, access, name, desc, signature, exceptions) { - @NotNull - @Override - public AnnotationVisitor visitAnnotation(@NotNull String desc, boolean visible) { - Type type = Type.getType(desc); - String annotationClass = type.getInternalName(); - if (INLINE_ANNOTATION_CLASS.equals(annotationClass)) { - inlineMethods.add(new MethodInfo(className[0], name, this.desc)); + override fun visitMethod(access: Int, name: String, desc: String, signature: String?, exceptions: Array?): MethodVisitor { + return object : MethodNode(Opcodes.ASM4, access, name, desc, signature, exceptions) { + public override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor { + val type = Type.getType(desc) + val annotationClass = type.getInternalName() + if (INLINE_ANNOTATION_CLASS == annotationClass) { + inlineMethods.add(MethodInfo(className!!, name, this.desc)) } - return super.visitAnnotation(desc, visible); + return super.visitAnnotation(desc, visible) } - }; + } } - }, 0); + }, 0) } - return inlineMethods; + return inlineMethods } - private static List checkInlineNotInvoked(List files, final Set inlinedMethods) { - final List notInlined = new ArrayList(); - for (OutputFile file : files) { - ClassReader cr = new ClassReader(file.asByteArray()); - - final Ref className = Ref.create(); - cr.accept(new ClassVisitor(Opcodes.ASM4) { - @Override - public void visit(int version, int access, @NotNull String name, String signature, String superName, String[] interfaces) { - className.set(name); - super.visit(version, access, name, signature, superName, interfaces); + private fun checkInlineNotInvoked(files: List, inlinedMethods: Set): List { + val notInlined = ArrayList() + files.forEach { file -> + val cr = ClassReader(file.asByteArray()) + var className: String? = null + cr.accept(object : ClassVisitor(Opcodes.ASM4) { + override fun visit(version: Int, access: Int, name: String, signature: String, superName: String, interfaces: Array) { + className = name + super.visit(version, access, name, signature, superName, interfaces) } - @Override - public MethodVisitor visitMethod( - int access, @NotNull String name, @NotNull String desc, String signature, String[] exceptions - ) { - FqName classFqName = JvmClassName.byInternalName(className.get()).getFqNameForClassNameWithoutDollars(); + override fun visitMethod(access: Int, name: String, desc: String, signature: String, exceptions: Array): MethodVisitor? { + val classFqName = JvmClassName.byInternalName(className!!).getFqNameForClassNameWithoutDollars() if (PackageClassUtils.isPackageClassFqName(classFqName)) { - return super.visitMethod(access, name, desc, signature, exceptions); + return null } - return new MethodNode(Opcodes.ASM4, access, name, desc, signature, exceptions) { - @Override - public void visitMethodInsn(int opcode, @NotNull String owner, String name, @NotNull String desc, boolean itf) { - MethodInfo methodCall = new MethodInfo(owner, name, desc); + return object : MethodNode(Opcodes.ASM4, access, name, desc, signature, exceptions) { + public override fun visitMethodInsn(opcode: Int, owner: String, name: String, desc: String, itf: Boolean) { + val methodCall = MethodInfo(owner, name, desc) if (inlinedMethods.contains(methodCall)) { - MethodInfo fromCall = new MethodInfo(className.get(), this.name, this.desc); + val fromCall = MethodInfo(className!!, this.name, this.desc) //skip delegation to trait impl from child class - if (methodCall.owner.endsWith(JvmAbi.TRAIT_IMPL_SUFFIX) && !fromCall.owner.equals(methodCall.owner)) { - return; + if (methodCall.owner.endsWith(JvmAbi.TRAIT_IMPL_SUFFIX) && fromCall.owner != methodCall.owner) { + return } - notInlined.add(new NotInlinedCall(fromCall, methodCall)); + notInlined.add(NotInlinedCall(fromCall, methodCall)) } } - }; + } } - }, 0); + }, 0) } - return notInlined; + return notInlined } - private static class NotInlinedCall { - public final MethodInfo fromCall; - public final MethodInfo inlineMethod; + private data class NotInlinedCall(public val fromCall: MethodInfo, public val inlineMethod: MethodInfo) - public NotInlinedCall(MethodInfo call, MethodInfo method) { - fromCall = call; - inlineMethod = method; - } - - @Override - public String toString() { - return "NotInlinedCall{" + - "fromCall=" + fromCall + - ", inlineMethod=" + inlineMethod + - '}'; - } - } - - private static class MethodInfo { - private final String owner; - private final String name; - private final String desc; - - public MethodInfo(@NotNull String owner, @NotNull String name, @NotNull String desc) { - this.owner = owner; - this.name = name; - this.desc = desc; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - MethodInfo method = (MethodInfo) o; - - if (!desc.equals(method.desc)) return false; - if (!name.equals(method.name)) return false; - if (!owner.equals(method.owner)) return false; - - return true; - } - - @Override - public int hashCode() { - int result = owner.hashCode(); - result = 31 * result + name.hashCode(); - result = 31 * result + desc.hashCode(); - return result; - } - - @Override - public String toString() { - return "MethodInfo{" + - "owner='" + owner + '\'' + - ", name='" + name + '\'' + - ", desc='" + desc + '\'' + - '}'; - } - } + private data class MethodInfo(val owner: String, val name: String, val desc: String) }