From dc99ead514964ab2b0247a2d2abd315f6e3212b6 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 18 Jun 2018 22:56:11 +0300 Subject: [PATCH] Fix compilation of Kotlin code when there's kotlin-reflect library in the classpath, but it's actually not used by Kotlin code. See #345 --- .../classlib/impl/ClassForNameTransformer.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/classlib/src/main/java/org/teavm/classlib/impl/ClassForNameTransformer.java b/classlib/src/main/java/org/teavm/classlib/impl/ClassForNameTransformer.java index eb317a27d..9dd68b055 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/ClassForNameTransformer.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/ClassForNameTransformer.java @@ -116,7 +116,7 @@ public class ClassForNameTransformer implements ClassHolderTransformer { if (nameIndex >= 0) { representative = program.variableAt(nameRepresentatives[nameIndex]); } else if (constant != null) { - if (classSource.get(constant) == null) { + if (classSource.get(constant) == null || !filterClassName(constant)) { continue; } ClassConstantInstruction classConstant = new ClassConstantInstruction(); @@ -148,4 +148,18 @@ public class ClassForNameTransformer implements ClassHolderTransformer { } } } + + private boolean filterClassName(String className) { + switch (className) { + // It's a hack for Kotlin. Kotlin full reflection library is too heavyweight for TeaVM. + // This optimization enables full reflection when there's Kotlin/JVM reflection library + // in the classpath, since Kotlin uses Class.forName() to check whether there's + // full reflection library presents. If program does not use full reflection, + // but build configuration includes kotlin-reflect artifact as a dependency, + // it gets into classpath, which allows this optimization to be applied. + case "kotlin.reflect.jvm.internal.ReflectionFactoryImpl": + return false; + } + return true; + } }