InlineTestUtil convertion

This commit is contained in:
Michael Bogdanov
2015-04-10 15:42:11 +03:00
parent 9c74b22262
commit 27c40edeb4
@@ -16,174 +16,100 @@
package org.jetbrains.kotlin.codegen; package org.jetbrains.kotlin.codegen;
import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.Ref
import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.backend.common.output.OutputFile
import org.jetbrains.kotlin.load.java.JvmAbi; 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.load.kotlin.PackageClassUtils; import org.jetbrains.kotlin.resolve.jvm.JvmClassName
import org.jetbrains.kotlin.name.FqName; import org.jetbrains.org.objectweb.asm.*
import org.jetbrains.kotlin.backend.common.output.OutputFile; import org.jetbrains.org.objectweb.asm.tree.MethodNode
import org.jetbrains.org.objectweb.asm.*; import java.util.ArrayList
import org.jetbrains.org.objectweb.asm.tree.MethodNode; import java.util.HashSet
import java.util.ArrayList; public object InlineTestUtil {
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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<OutputFile>) {
val inlinedMethods = collectInlineMethods(files)
assert(!inlinedMethods.isEmpty(), "There are no inline methods")
public static void checkNoCallsToInline(List<OutputFile> files) { val notInlinedCalls = checkInlineNotInvoked(files, inlinedMethods)
Set<MethodInfo> inlinedMethods = collectInlineMethods(files); assert(notInlinedCalls.isEmpty()) { "All inline methods should be inlined but " + StringUtil.join(notInlinedCalls, "\n") }
assert !inlinedMethods.isEmpty() : "There are no inline methods";
List<NotInlinedCall> notInlinedCalls = checkInlineNotInvoked(files, inlinedMethods);
assert notInlinedCalls.isEmpty() : "All inline methods should be inlined but " + StringUtil.join(notInlinedCalls, "\n");
} }
private static Set<MethodInfo> collectInlineMethods(List<OutputFile> files) { private fun collectInlineMethods(files: List<OutputFile>): Set<MethodInfo> {
final Set<MethodInfo> inlineMethods = new HashSet<MethodInfo>(); val inlineMethods = HashSet<MethodInfo>()
for (OutputFile file : files) { for (file in files) {
ClassReader cr = new ClassReader(file.asByteArray()); val cr = ClassReader(file.asByteArray())
final String[] className = {null}; var className: String? = null
cr.accept(new ClassVisitor(Opcodes.ASM4) { cr.accept(object : ClassVisitor(Opcodes.ASM4) {
@Override override fun visit(version: Int, access: Int, name: String, signature: String?, superName: String?, interfaces: Array<String>?) {
public void visit(int version, int access, @NotNull String name, String signature, String superName, String[] interfaces) { className = name
className[0] = name; super.visit(version, access, name, signature, superName, interfaces)
super.visit(version, access, name, signature, superName, interfaces);
} }
@Override override fun visitMethod(access: Int, name: String, desc: String, signature: String?, exceptions: Array<String>?): MethodVisitor {
public MethodVisitor visitMethod( return object : MethodNode(Opcodes.ASM4, access, name, desc, signature, exceptions) {
int access, @NotNull String name, @NotNull String desc, String signature, String[] exceptions public override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor {
) { val type = Type.getType(desc)
return new MethodNode(Opcodes.ASM4, access, name, desc, signature, exceptions) { val annotationClass = type.getInternalName()
@NotNull if (INLINE_ANNOTATION_CLASS == annotationClass) {
@Override inlineMethods.add(MethodInfo(className!!, name, this.desc))
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));
} }
return super.visitAnnotation(desc, visible); return super.visitAnnotation(desc, visible)
} }
}; }
} }
}, 0); }, 0)
} }
return inlineMethods; return inlineMethods
} }
private static List<NotInlinedCall> checkInlineNotInvoked(List<OutputFile> files, final Set<MethodInfo> inlinedMethods) { private fun checkInlineNotInvoked(files: List<OutputFile>, inlinedMethods: Set<MethodInfo>): List<NotInlinedCall> {
final List<NotInlinedCall> notInlined = new ArrayList<NotInlinedCall>(); val notInlined = ArrayList<NotInlinedCall>()
for (OutputFile file : files) { files.forEach { file ->
ClassReader cr = new ClassReader(file.asByteArray()); val cr = ClassReader(file.asByteArray())
var className: String? = null
final Ref<String> className = Ref.create(); cr.accept(object : ClassVisitor(Opcodes.ASM4) {
cr.accept(new ClassVisitor(Opcodes.ASM4) { override fun visit(version: Int, access: Int, name: String, signature: String, superName: String, interfaces: Array<String>) {
@Override className = name
public void visit(int version, int access, @NotNull String name, String signature, String superName, String[] interfaces) { super.visit(version, access, name, signature, superName, interfaces)
className.set(name);
super.visit(version, access, name, signature, superName, interfaces);
} }
@Override override fun visitMethod(access: Int, name: String, desc: String, signature: String, exceptions: Array<String>): MethodVisitor? {
public MethodVisitor visitMethod( val classFqName = JvmClassName.byInternalName(className!!).getFqNameForClassNameWithoutDollars()
int access, @NotNull String name, @NotNull String desc, String signature, String[] exceptions
) {
FqName classFqName = JvmClassName.byInternalName(className.get()).getFqNameForClassNameWithoutDollars();
if (PackageClassUtils.isPackageClassFqName(classFqName)) { if (PackageClassUtils.isPackageClassFqName(classFqName)) {
return super.visitMethod(access, name, desc, signature, exceptions); return null
} }
return new MethodNode(Opcodes.ASM4, access, name, desc, signature, exceptions) { return object : MethodNode(Opcodes.ASM4, access, name, desc, signature, exceptions) {
@Override public override fun visitMethodInsn(opcode: Int, owner: String, name: String, desc: String, itf: Boolean) {
public void visitMethodInsn(int opcode, @NotNull String owner, String name, @NotNull String desc, boolean itf) { val methodCall = MethodInfo(owner, name, desc)
MethodInfo methodCall = new MethodInfo(owner, name, desc);
if (inlinedMethods.contains(methodCall)) { 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 //skip delegation to trait impl from child class
if (methodCall.owner.endsWith(JvmAbi.TRAIT_IMPL_SUFFIX) && !fromCall.owner.equals(methodCall.owner)) { if (methodCall.owner.endsWith(JvmAbi.TRAIT_IMPL_SUFFIX) && fromCall.owner != methodCall.owner) {
return; return
} }
notInlined.add(new NotInlinedCall(fromCall, methodCall)); notInlined.add(NotInlinedCall(fromCall, methodCall))
} }
} }
}; }
} }
}, 0); }, 0)
} }
return notInlined; return notInlined
} }
private static class NotInlinedCall { private data class NotInlinedCall(public val fromCall: MethodInfo, public val inlineMethod: MethodInfo)
public final MethodInfo fromCall;
public final MethodInfo inlineMethod;
public NotInlinedCall(MethodInfo call, MethodInfo method) { private data class MethodInfo(val owner: String, val name: String, val desc: String)
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 + '\'' +
'}';
}
}
} }