Drop KFunctionFromReferenceImpl, make FunctionImpl an interface
23 invokes in KFunctionFromReferenceImpl (and consequently, in FunctionImpl) were needed before1576160390: a wrapped function reference must have had the necessary invoke to be called as an instance of a specific function type. After1576160390, this is not needed anymore because KFunctionFromReferenceImpl is now an internal implementation detail of reflection, and no invoke is ever called on it.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
// WITH_RUNTIME
|
||||
// FILE: J.java
|
||||
|
||||
import kotlin.Function;
|
||||
import kotlin.jvm.functions.Function0;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import kotlin.jvm.functions.Function2;
|
||||
|
||||
public class J {
|
||||
public static String test(Function<String> x) {
|
||||
if (x instanceof Function1) return "Fail 1";
|
||||
if (x instanceof Function2) return "Fail 2";
|
||||
if (!(x instanceof Function0)) return "Fail 3";
|
||||
|
||||
return ((Function0<String>) x).invoke();
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: K.kt
|
||||
|
||||
fun box(): String {
|
||||
return J.test({ "OK" })
|
||||
}
|
||||
@@ -7924,6 +7924,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/javaInterop"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("lambdaInstanceOf.kt")
|
||||
public void testLambdaInstanceOf() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/javaInterop/lambdaInstanceOf.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/javaInterop/generics")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package kotlin.reflect.jvm.internal
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import kotlin.jvm.internal.FunctionReference
|
||||
import kotlin.reflect.KCallable
|
||||
import kotlin.reflect.KotlinReflectionInternalError
|
||||
|
||||
internal class KFunctionFromReferenceImpl(
|
||||
val reference: FunctionReference
|
||||
): KFunctionImpl(
|
||||
reference.owner as? KDeclarationContainerImpl ?: EmptyContainerForLocal,
|
||||
reference.name,
|
||||
reference.signature
|
||||
) {
|
||||
override fun getArity() = reference.arity
|
||||
|
||||
override val name = reference.name
|
||||
|
||||
// The rest of the class is auto-generated. Use the following script:
|
||||
// (0..22).forEach { n -> println("override fun invoke(" + (1..n).joinToString { "p$it: Any?" } + "): Any? = reference(" + (1..n).joinToString { "p$it" } + ")") }
|
||||
override fun invoke(): Any? = reference()
|
||||
override fun invoke(p1: Any?): Any? = reference(p1)
|
||||
override fun invoke(p1: Any?, p2: Any?): Any? = reference(p1, p2)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?): Any? = reference(p1, p2, p3)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?): Any? = reference(p1, p2, p3, p4)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?): Any? = reference(p1, p2, p3, p4, p5)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?): Any? = reference(p1, p2, p3, p4, p5, p6)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?, p12: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?, p12: Any?, p13: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?, p12: Any?, p13: Any?, p14: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?, p12: Any?, p13: Any?, p14: Any?, p15: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?, p12: Any?, p13: Any?, p14: Any?, p15: Any?, p16: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?, p12: Any?, p13: Any?, p14: Any?, p15: Any?, p16: Any?, p17: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?, p12: Any?, p13: Any?, p14: Any?, p15: Any?, p16: Any?, p17: Any?, p18: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?, p12: Any?, p13: Any?, p14: Any?, p15: Any?, p16: Any?, p17: Any?, p18: Any?, p19: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?, p12: Any?, p13: Any?, p14: Any?, p15: Any?, p16: Any?, p17: Any?, p18: Any?, p19: Any?, p20: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?, p12: Any?, p13: Any?, p14: Any?, p15: Any?, p16: Any?, p17: Any?, p18: Any?, p19: Any?, p20: Any?, p21: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21)
|
||||
override fun invoke(p1: Any?, p2: Any?, p3: Any?, p4: Any?, p5: Any?, p6: Any?, p7: Any?, p8: Any?, p9: Any?, p10: Any?, p11: Any?, p12: Any?, p13: Any?, p14: Any?, p15: Any?, p16: Any?, p17: Any?, p18: Any?, p19: Any?, p20: Any?, p21: Any?, p22: Any?): Any? = reference(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22)
|
||||
}
|
||||
|
||||
@@ -28,12 +28,12 @@ import kotlin.reflect.KFunction
|
||||
import kotlin.reflect.KotlinReflectionInternalError
|
||||
import kotlin.reflect.jvm.internal.JvmFunctionSignature.*
|
||||
|
||||
internal open class KFunctionImpl protected constructor(
|
||||
internal class KFunctionImpl private constructor(
|
||||
override val container: KDeclarationContainerImpl,
|
||||
name: String,
|
||||
private val signature: String,
|
||||
descriptorInitialValue: FunctionDescriptor?
|
||||
) : KFunction<Any?>, KCallableImpl<Any?>, FunctionImpl() {
|
||||
) : KFunction<Any?>, KCallableImpl<Any?>, FunctionImpl {
|
||||
constructor(container: KDeclarationContainerImpl, name: String, signature: String) : this(container, name, signature, null)
|
||||
|
||||
constructor(container: KDeclarationContainerImpl, descriptor: FunctionDescriptor) : this(
|
||||
|
||||
@@ -66,7 +66,7 @@ public class ReflectionFactoryImpl extends ReflectionFactory {
|
||||
|
||||
@Override
|
||||
public KFunction function(FunctionReference f) {
|
||||
return new KFunctionFromReferenceImpl(f);
|
||||
return new KFunctionImpl(getOwner(f), f.getName(), f.getSignature());
|
||||
}
|
||||
|
||||
// Properties
|
||||
@@ -105,4 +105,9 @@ public class ReflectionFactoryImpl extends ReflectionFactory {
|
||||
KDeclarationContainer owner = reference.getOwner();
|
||||
return owner instanceof KDeclarationContainerImpl ? ((KDeclarationContainerImpl) owner) : EmptyContainerForLocal.INSTANCE;
|
||||
}
|
||||
|
||||
private static KDeclarationContainerImpl getOwner(FunctionReference reference) {
|
||||
KDeclarationContainer owner = reference.getOwner();
|
||||
return owner instanceof KDeclarationContainerImpl ? ((KDeclarationContainerImpl) owner) : EmptyContainerForLocal.INSTANCE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,172 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
// Auto-generated file. DO NOT EDIT!
|
||||
|
||||
package kotlin.jvm.internal;
|
||||
|
||||
import kotlin.Function;
|
||||
import kotlin.jvm.functions.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public abstract class FunctionImpl
|
||||
implements Function, Serializable,
|
||||
Function0, Function1, Function2, Function3, Function4, Function5, Function6, Function7, Function8, Function9,
|
||||
Function10, Function11, Function12, Function13, Function14, Function15, Function16, Function17, Function18, Function19,
|
||||
Function20, Function21, Function22 {
|
||||
|
||||
public abstract int getArity();
|
||||
|
||||
public Object invokeVararg(Object... p) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private void checkArity(int expected) {
|
||||
if (getArity() != expected) {
|
||||
throwWrongArity(expected);
|
||||
}
|
||||
}
|
||||
|
||||
private void throwWrongArity(int expected) {
|
||||
throw new IllegalStateException("Wrong function arity, expected: " + expected + ", actual: " + getArity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke() {
|
||||
checkArity(0);
|
||||
return invokeVararg();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1) {
|
||||
checkArity(1);
|
||||
return invokeVararg(p1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2) {
|
||||
checkArity(2);
|
||||
return invokeVararg(p1, p2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3) {
|
||||
checkArity(3);
|
||||
return invokeVararg(p1, p2, p3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4) {
|
||||
checkArity(4);
|
||||
return invokeVararg(p1, p2, p3, p4);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5) {
|
||||
checkArity(5);
|
||||
return invokeVararg(p1, p2, p3, p4, p5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6) {
|
||||
checkArity(6);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7) {
|
||||
checkArity(7);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8) {
|
||||
checkArity(8);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9) {
|
||||
checkArity(9);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10) {
|
||||
checkArity(10);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11) {
|
||||
checkArity(11);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11, Object p12) {
|
||||
checkArity(12);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11, Object p12, Object p13) {
|
||||
checkArity(13);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11, Object p12, Object p13, Object p14) {
|
||||
checkArity(14);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11, Object p12, Object p13, Object p14, Object p15) {
|
||||
checkArity(15);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11, Object p12, Object p13, Object p14, Object p15, Object p16) {
|
||||
checkArity(16);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11, Object p12, Object p13, Object p14, Object p15, Object p16, Object p17) {
|
||||
checkArity(17);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11, Object p12, Object p13, Object p14, Object p15, Object p16, Object p17, Object p18) {
|
||||
checkArity(18);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11, Object p12, Object p13, Object p14, Object p15, Object p16, Object p17, Object p18, Object p19) {
|
||||
checkArity(19);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11, Object p12, Object p13, Object p14, Object p15, Object p16, Object p17, Object p18, Object p19, Object p20) {
|
||||
checkArity(20);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11, Object p12, Object p13, Object p14, Object p15, Object p16, Object p17, Object p18, Object p19, Object p20, Object p21) {
|
||||
checkArity(21);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9, Object p10, Object p11, Object p12, Object p13, Object p14, Object p15, Object p16, Object p17, Object p18, Object p19, Object p20, Object p21, Object p22) {
|
||||
checkArity(22);
|
||||
return invokeVararg(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22);
|
||||
}
|
||||
public interface FunctionImpl extends Function, Serializable {
|
||||
int getArity();
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings({"unchecked", "NullableProblems"})
|
||||
public class FunctionReference extends FunctionImpl implements KFunction {
|
||||
public class FunctionReference implements FunctionImpl, KFunction {
|
||||
private final int arity;
|
||||
private KFunction reflected;
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package kotlin.jvm.internal
|
||||
|
||||
abstract class Lambda(private val arity: Int) : FunctionImpl() {
|
||||
abstract class Lambda(private val arity: Int) : FunctionImpl {
|
||||
override fun getArity() = arity
|
||||
|
||||
override fun toString() = Reflection.renderLambdaToString(this)
|
||||
|
||||
@@ -22,7 +22,7 @@ import kotlin.jvm.internal.markers.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@SuppressWarnings({"unused", "WeakerAccess"})
|
||||
public class TypeIntrinsics {
|
||||
private static <T extends Throwable> T sanitizeStackTrace(T throwable) {
|
||||
return Intrinsics.sanitizeStackTrace(throwable, TypeIntrinsics.class.getName());
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.generators.builtins.functionImpl
|
||||
|
||||
import org.jetbrains.kotlin.generators.builtins.functions.MAX_PARAM_COUNT
|
||||
import org.jetbrains.kotlin.generators.builtins.generateBuiltIns.BuiltInsSourceGenerator
|
||||
import org.jetbrains.kotlin.generators.builtins.generateBuiltIns.BuiltInsSourceGenerator.Language
|
||||
import java.io.PrintWriter
|
||||
|
||||
class GenerateFunctionImpl(out: PrintWriter) : BuiltInsSourceGenerator(out) {
|
||||
override fun getPackage() = "kotlin.jvm.internal"
|
||||
|
||||
override val language = Language.JAVA
|
||||
|
||||
override fun generateBody() {
|
||||
val n = MAX_PARAM_COUNT
|
||||
|
||||
out.print("""import kotlin.Function;
|
||||
import kotlin.jvm.functions.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public abstract class FunctionImpl
|
||||
implements Function, Serializable,""")
|
||||
|
||||
for (i in 0..n) {
|
||||
if (i % 10 == 0) {
|
||||
// Insert newline sometimes to avoid very long lines
|
||||
out.println()
|
||||
out.print(" ")
|
||||
}
|
||||
out.print("Function$i")
|
||||
if (i < n) out.print(", ")
|
||||
}
|
||||
|
||||
out.println(" {")
|
||||
|
||||
out.println("""
|
||||
public abstract int getArity();
|
||||
|
||||
public Object invokeVararg(Object... p) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private void checkArity(int expected) {
|
||||
if (getArity() != expected) {
|
||||
throwWrongArity(expected);
|
||||
}
|
||||
}
|
||||
|
||||
private void throwWrongArity(int expected) {
|
||||
throw new IllegalStateException("Wrong function arity, expected: " + expected + ", actual: " + getArity());
|
||||
}""")
|
||||
|
||||
for (i in 0..n) {
|
||||
out.println()
|
||||
out.println(" @Override")
|
||||
out.println(" public Object invoke(" + (1..i).joinToString { "Object p$it" } + ") {")
|
||||
out.println(" checkArity($i);")
|
||||
out.println(" return invokeVararg(" + (1..i).joinToString { "p$it" } + ");")
|
||||
out.println(" }")
|
||||
}
|
||||
|
||||
out.println("}")
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.generators.builtins.generateBuiltIns
|
||||
|
||||
import org.jetbrains.kotlin.generators.builtins.arrayIterators.GenerateArrayIterators
|
||||
import org.jetbrains.kotlin.generators.builtins.arrays.GenerateArrays
|
||||
import org.jetbrains.kotlin.generators.builtins.functionImpl.GenerateFunctionImpl
|
||||
import org.jetbrains.kotlin.generators.builtins.functions.GenerateFunctions
|
||||
import org.jetbrains.kotlin.generators.builtins.iterators.GenerateIterators
|
||||
import org.jetbrains.kotlin.generators.builtins.progressionIterators.GenerateProgressionIterators
|
||||
@@ -41,21 +40,19 @@ abstract class BuiltInsSourceGenerator(val out: PrintWriter) {
|
||||
|
||||
protected open fun getPackage(): String = "kotlin"
|
||||
|
||||
protected open val language: Language = Language.KOTLIN
|
||||
|
||||
enum class Language {
|
||||
KOTLIN,
|
||||
JAVA
|
||||
}
|
||||
|
||||
final fun generate() {
|
||||
fun generate() {
|
||||
out.println(File("license/LICENSE.txt").readText())
|
||||
// Don't include generator class name in the message: these are built-in sources,
|
||||
// and we don't want to scare users with any internal information about our project
|
||||
out.println("// Auto-generated file. DO NOT EDIT!")
|
||||
out.println()
|
||||
out.print("package ${getPackage()}")
|
||||
if (language == Language.KOTLIN) out.println() else out.println(";")
|
||||
out.println()
|
||||
out.println()
|
||||
|
||||
generateBody()
|
||||
@@ -68,7 +65,6 @@ fun generateBuiltIns(generate: (File, (PrintWriter) -> BuiltInsSourceGenerator)
|
||||
assertExists(RUNTIME_JVM_DIR)
|
||||
|
||||
generate(File(RUNTIME_JVM_DIR, "kotlin/jvm/functions/Functions.kt")) { GenerateFunctions(it) }
|
||||
generate(File(RUNTIME_JVM_DIR, "kotlin/jvm/internal/FunctionImpl.java")) { GenerateFunctionImpl(it) }
|
||||
generate(File(BUILT_INS_NATIVE_DIR, "kotlin/Arrays.kt")) { GenerateArrays(it) }
|
||||
generate(File(BUILT_INS_NATIVE_DIR, "kotlin/Primitives.kt")) { GeneratePrimitives(it) }
|
||||
generate(File(BUILT_INS_SRC_DIR, "kotlin/Iterators.kt")) { GenerateIterators(it) }
|
||||
|
||||
@@ -65,11 +65,11 @@ private fun defineClasses(
|
||||
}
|
||||
}
|
||||
|
||||
// The order is relevant here: if we load Lambda first instead, during the definition of Lambda the class loader will look for
|
||||
// its superclasses and will try to load FunctionImpl itself. It will succeed, probably with the help of some parent class loader,
|
||||
// and the subsequent attempt to define the patched version of FunctionImpl will fail with LinkageError (cannot redefine class)
|
||||
// This list should contain all superclasses of lambda classes.
|
||||
// The order is relevant here: if we load Lambda first instead, during the definition of Lambda the class loader will try
|
||||
// to load its superclass. It will succeed, probably with the help of some parent class loader, and the subsequent attempt to define
|
||||
// the patched version of that superclass will fail with LinkageError (cannot redefine class)
|
||||
private val LAMBDA_SUPERCLASSES = listOf(
|
||||
ClassBytes("kotlin.jvm.internal.FunctionImpl"),
|
||||
ClassBytes("kotlin.jvm.internal.Lambda")
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user