Kotlin类应具有单个no-arg构造函数

时间:2018-12-13 13:57:09

标签: kotlin kotlin-reflect

我使用以下方法实例化对象:

val payInCurrency = Currency::class.createInstance()
            .copy(code = "GBP")

但是我在那一行遇到了异常:

java.lang.IllegalArgumentException: Class should have a single no-arg constructor: class com.abc.Currency
    at kotlin.reflect.full.KClasses.createInstance(KClasses.kt:281)
    at com.abc.MyInteractorTest.setUp(MyInteractorTest.kt:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:106)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
    at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:66)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at sun.reflect.GeneratedMethodAccessor35.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:117)
    at sun.reflect.GeneratedMethodAccessor34.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:155)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:137)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:748)

3 个答案:

答案 0 :(得分:3)

请确保您的data class initializes所有值(因此可以使用默认构造函数)或自己提供适当的默认constructor

// setting default values on all properties:
data class Currency(val code : String = "GBP" / * all other properties need to have a default value assigned too */)

// or add a default constructor:
data class Currency(val code : String /* other properties */) {
  constructor() : this("GBP" /* other properties default values */)
}

请注意,createInstance在内部使用singleOrNull,并且如果存在多个满足仅具有可选参数标准的构造函数(或不存在),则抛出异常。因此,如果您有多个这样的构造函数,或者您不想更改Currency类,请遍历可用的构造函数,而使用适合的构造函数并填充参数。然后,您甚至可以跳过该copy通话。

您为什么还要使用这种方法来复制数据? copy仅在已经有一个对象只需要更改某些属性,而其余部分保持不变的情况下才有意义,否则您的示例甚至可能只写为:

val payInCurrency = Currency(code = "GBP")

您已经知道要使用Currency,并且还希望代码是"GBP"

答案 1 :(得分:3)

您正在呼叫createInstance(),而该方法的documentation说:

  

创建该类的新实例,调用一个没有参数或所有参数均为可选的构造函数(请参见KParameter.isOptional)。如果没有或有很多这样的构造函数,则会引发异常。

您在类上调用createInstance并抛出指定的异常,该类之一是:

  • 没有参数(或者所有参数都是可选的)的构造函数
  • 有多个符合该定义的构造函数

答案 2 :(得分:0)

很抱歉给您带来的困惑,感谢您的帮助。
确实,很明显KClasses.createInstance()要求类具有“无参数构造函数”。

因此,一种解决方案是更改我的类以引入“ no-arg构造函数”。

另一种解决方案是使用我自己的createInstance(),它接受​​没有“ no-arg构造函数”的类。原来我的团队已经拥有自己的createInstance(),而我使用了错误的方法(KClasses.createInstance()