Kotlin KCallable illegalArgumentException












0















I have the following Kotlin function:



fun invokeSync(typedArguments : List<Any?>): Any?{
var returnedValue : Any?
try {
returnedValue = callable.call(this, typedArguments);
} catch (e:Exception) {
logInvocationError(e, typedArguments);
throw IllegalArgumentException(e);
}
}


It doesn't matter how much arguments are in this list, I will always get an IllegalArgumentException saying "Callable expects 3 arguments, but 1 were provided".



The function is a simple isGreater-function with 2 arguments of type Int.
I have checked the list of arguments and there are 2 arguments of type Int in there.



Here the function in context:



open class TypedJavaScriptFunction(name: String) : SelfRegisteringJavascriptFunction(MessageFormat.format(JS_NAME_CONVENTION, name)) {

val callable = getCallable(this::class)

override fun function(arguments: Array<Any?>): Any? {
try {
val typedArguments = getTypedArguments(arguments)
val annotations = callable.annotations

for (a in annotations) {
if (a is BrowserFunction) {
if (a.value == Policy.ASYNC) {
invokeAsync(typedArguments);
return null
} else {
return invokeSync(typedArguments)
}
}
}
} catch (e: IllegalArgumentException) {
// this Exception is only for signaling the error; it has already
// been logged before
JavaScriptAPI.showError(browser, "Internal Error (" + callable.name + ")");
}
return null
}

fun getTypedArguments(arguments: Array<Any?>): List<Any?> {
var typedArguments = mutableListOf<Any?>()
val argTypes = callable.valueParameters
if (arguments.size != argTypes.size) {
LOG.error(getName()
+ ": given arguments don't match signature. Given: "
+ arguments.size + ", expected: " + argTypes.size);
throw IllegalArgumentException()
}

for (i in 0 until arguments.size) {
typedArguments.add(TypeRefinery.refine(arguments[i], argTypes[i].type.classifier as KClass<Any>))
}

return typedArguments
}

// ...

fun invokeSync(typedArguments: List<Any?>): Any? {
var returnedValue: Any?
try {
returnedValue = callable.call(this, typedArguments);
} catch (e: Exception) {
logInvocationError(e, typedArguments);
throw IllegalArgumentException(e);
}

// ...
}
}


Did anyone can help me and tell me whats wrong or can give me a hint?










share|improve this question

























  • Can you show how you are defining callable in this context?

    – Todd
    Jan 20 at 15:47











  • .. and how this (surrounding class) is defined?

    – Michael Butscher
    Jan 20 at 15:50











  • @Lucky Ozzy pls reduce it to a minimal example reproducing your problem. This way it is easier to spot the problem an help you.

    – Willi Mentzel
    Jan 20 at 17:54
















0















I have the following Kotlin function:



fun invokeSync(typedArguments : List<Any?>): Any?{
var returnedValue : Any?
try {
returnedValue = callable.call(this, typedArguments);
} catch (e:Exception) {
logInvocationError(e, typedArguments);
throw IllegalArgumentException(e);
}
}


It doesn't matter how much arguments are in this list, I will always get an IllegalArgumentException saying "Callable expects 3 arguments, but 1 were provided".



The function is a simple isGreater-function with 2 arguments of type Int.
I have checked the list of arguments and there are 2 arguments of type Int in there.



Here the function in context:



open class TypedJavaScriptFunction(name: String) : SelfRegisteringJavascriptFunction(MessageFormat.format(JS_NAME_CONVENTION, name)) {

val callable = getCallable(this::class)

override fun function(arguments: Array<Any?>): Any? {
try {
val typedArguments = getTypedArguments(arguments)
val annotations = callable.annotations

for (a in annotations) {
if (a is BrowserFunction) {
if (a.value == Policy.ASYNC) {
invokeAsync(typedArguments);
return null
} else {
return invokeSync(typedArguments)
}
}
}
} catch (e: IllegalArgumentException) {
// this Exception is only for signaling the error; it has already
// been logged before
JavaScriptAPI.showError(browser, "Internal Error (" + callable.name + ")");
}
return null
}

fun getTypedArguments(arguments: Array<Any?>): List<Any?> {
var typedArguments = mutableListOf<Any?>()
val argTypes = callable.valueParameters
if (arguments.size != argTypes.size) {
LOG.error(getName()
+ ": given arguments don't match signature. Given: "
+ arguments.size + ", expected: " + argTypes.size);
throw IllegalArgumentException()
}

for (i in 0 until arguments.size) {
typedArguments.add(TypeRefinery.refine(arguments[i], argTypes[i].type.classifier as KClass<Any>))
}

return typedArguments
}

// ...

fun invokeSync(typedArguments: List<Any?>): Any? {
var returnedValue: Any?
try {
returnedValue = callable.call(this, typedArguments);
} catch (e: Exception) {
logInvocationError(e, typedArguments);
throw IllegalArgumentException(e);
}

// ...
}
}


Did anyone can help me and tell me whats wrong or can give me a hint?










share|improve this question

























  • Can you show how you are defining callable in this context?

    – Todd
    Jan 20 at 15:47











  • .. and how this (surrounding class) is defined?

    – Michael Butscher
    Jan 20 at 15:50











  • @Lucky Ozzy pls reduce it to a minimal example reproducing your problem. This way it is easier to spot the problem an help you.

    – Willi Mentzel
    Jan 20 at 17:54














0












0








0








I have the following Kotlin function:



fun invokeSync(typedArguments : List<Any?>): Any?{
var returnedValue : Any?
try {
returnedValue = callable.call(this, typedArguments);
} catch (e:Exception) {
logInvocationError(e, typedArguments);
throw IllegalArgumentException(e);
}
}


It doesn't matter how much arguments are in this list, I will always get an IllegalArgumentException saying "Callable expects 3 arguments, but 1 were provided".



The function is a simple isGreater-function with 2 arguments of type Int.
I have checked the list of arguments and there are 2 arguments of type Int in there.



Here the function in context:



open class TypedJavaScriptFunction(name: String) : SelfRegisteringJavascriptFunction(MessageFormat.format(JS_NAME_CONVENTION, name)) {

val callable = getCallable(this::class)

override fun function(arguments: Array<Any?>): Any? {
try {
val typedArguments = getTypedArguments(arguments)
val annotations = callable.annotations

for (a in annotations) {
if (a is BrowserFunction) {
if (a.value == Policy.ASYNC) {
invokeAsync(typedArguments);
return null
} else {
return invokeSync(typedArguments)
}
}
}
} catch (e: IllegalArgumentException) {
// this Exception is only for signaling the error; it has already
// been logged before
JavaScriptAPI.showError(browser, "Internal Error (" + callable.name + ")");
}
return null
}

fun getTypedArguments(arguments: Array<Any?>): List<Any?> {
var typedArguments = mutableListOf<Any?>()
val argTypes = callable.valueParameters
if (arguments.size != argTypes.size) {
LOG.error(getName()
+ ": given arguments don't match signature. Given: "
+ arguments.size + ", expected: " + argTypes.size);
throw IllegalArgumentException()
}

for (i in 0 until arguments.size) {
typedArguments.add(TypeRefinery.refine(arguments[i], argTypes[i].type.classifier as KClass<Any>))
}

return typedArguments
}

// ...

fun invokeSync(typedArguments: List<Any?>): Any? {
var returnedValue: Any?
try {
returnedValue = callable.call(this, typedArguments);
} catch (e: Exception) {
logInvocationError(e, typedArguments);
throw IllegalArgumentException(e);
}

// ...
}
}


Did anyone can help me and tell me whats wrong or can give me a hint?










share|improve this question
















I have the following Kotlin function:



fun invokeSync(typedArguments : List<Any?>): Any?{
var returnedValue : Any?
try {
returnedValue = callable.call(this, typedArguments);
} catch (e:Exception) {
logInvocationError(e, typedArguments);
throw IllegalArgumentException(e);
}
}


It doesn't matter how much arguments are in this list, I will always get an IllegalArgumentException saying "Callable expects 3 arguments, but 1 were provided".



The function is a simple isGreater-function with 2 arguments of type Int.
I have checked the list of arguments and there are 2 arguments of type Int in there.



Here the function in context:



open class TypedJavaScriptFunction(name: String) : SelfRegisteringJavascriptFunction(MessageFormat.format(JS_NAME_CONVENTION, name)) {

val callable = getCallable(this::class)

override fun function(arguments: Array<Any?>): Any? {
try {
val typedArguments = getTypedArguments(arguments)
val annotations = callable.annotations

for (a in annotations) {
if (a is BrowserFunction) {
if (a.value == Policy.ASYNC) {
invokeAsync(typedArguments);
return null
} else {
return invokeSync(typedArguments)
}
}
}
} catch (e: IllegalArgumentException) {
// this Exception is only for signaling the error; it has already
// been logged before
JavaScriptAPI.showError(browser, "Internal Error (" + callable.name + ")");
}
return null
}

fun getTypedArguments(arguments: Array<Any?>): List<Any?> {
var typedArguments = mutableListOf<Any?>()
val argTypes = callable.valueParameters
if (arguments.size != argTypes.size) {
LOG.error(getName()
+ ": given arguments don't match signature. Given: "
+ arguments.size + ", expected: " + argTypes.size);
throw IllegalArgumentException()
}

for (i in 0 until arguments.size) {
typedArguments.add(TypeRefinery.refine(arguments[i], argTypes[i].type.classifier as KClass<Any>))
}

return typedArguments
}

// ...

fun invokeSync(typedArguments: List<Any?>): Any? {
var returnedValue: Any?
try {
returnedValue = callable.call(this, typedArguments);
} catch (e: Exception) {
logInvocationError(e, typedArguments);
throw IllegalArgumentException(e);
}

// ...
}
}


Did anyone can help me and tell me whats wrong or can give me a hint?







kotlin illegalargumentexception






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 21 at 22:54









Willi Mentzel

9,862114770




9,862114770










asked Jan 20 at 15:22









Lucky OzzyLucky Ozzy

32




32













  • Can you show how you are defining callable in this context?

    – Todd
    Jan 20 at 15:47











  • .. and how this (surrounding class) is defined?

    – Michael Butscher
    Jan 20 at 15:50











  • @Lucky Ozzy pls reduce it to a minimal example reproducing your problem. This way it is easier to spot the problem an help you.

    – Willi Mentzel
    Jan 20 at 17:54



















  • Can you show how you are defining callable in this context?

    – Todd
    Jan 20 at 15:47











  • .. and how this (surrounding class) is defined?

    – Michael Butscher
    Jan 20 at 15:50











  • @Lucky Ozzy pls reduce it to a minimal example reproducing your problem. This way it is easier to spot the problem an help you.

    – Willi Mentzel
    Jan 20 at 17:54

















Can you show how you are defining callable in this context?

– Todd
Jan 20 at 15:47





Can you show how you are defining callable in this context?

– Todd
Jan 20 at 15:47













.. and how this (surrounding class) is defined?

– Michael Butscher
Jan 20 at 15:50





.. and how this (surrounding class) is defined?

– Michael Butscher
Jan 20 at 15:50













@Lucky Ozzy pls reduce it to a minimal example reproducing your problem. This way it is easier to spot the problem an help you.

– Willi Mentzel
Jan 20 at 17:54





@Lucky Ozzy pls reduce it to a minimal example reproducing your problem. This way it is easier to spot the problem an help you.

– Willi Mentzel
Jan 20 at 17:54












1 Answer
1






active

oldest

votes


















2














Since call takes a vararg you need to use the spread operator * and toTypedArray() to pass in the List like that:



returnedValue = callable.call(this, *typedArguments.toTypedArray());


The first argument is the instance you are calling the function on and the other two parameters come from the spreaded List, under the condition that List has exactly two elements.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277921%2fkotlin-kcallable-illegalargumentexception%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Since call takes a vararg you need to use the spread operator * and toTypedArray() to pass in the List like that:



    returnedValue = callable.call(this, *typedArguments.toTypedArray());


    The first argument is the instance you are calling the function on and the other two parameters come from the spreaded List, under the condition that List has exactly two elements.






    share|improve this answer






























      2














      Since call takes a vararg you need to use the spread operator * and toTypedArray() to pass in the List like that:



      returnedValue = callable.call(this, *typedArguments.toTypedArray());


      The first argument is the instance you are calling the function on and the other two parameters come from the spreaded List, under the condition that List has exactly two elements.






      share|improve this answer




























        2












        2








        2







        Since call takes a vararg you need to use the spread operator * and toTypedArray() to pass in the List like that:



        returnedValue = callable.call(this, *typedArguments.toTypedArray());


        The first argument is the instance you are calling the function on and the other two parameters come from the spreaded List, under the condition that List has exactly two elements.






        share|improve this answer















        Since call takes a vararg you need to use the spread operator * and toTypedArray() to pass in the List like that:



        returnedValue = callable.call(this, *typedArguments.toTypedArray());


        The first argument is the instance you are calling the function on and the other two parameters come from the spreaded List, under the condition that List has exactly two elements.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 20 at 18:09

























        answered Jan 20 at 18:03









        Willi MentzelWilli Mentzel

        9,862114770




        9,862114770
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277921%2fkotlin-kcallable-illegalargumentexception%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Homophylophilia

            Updating UILabel text programmatically using a function

            Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage