Inner class create fail in Kotlin - kotlin

This code sample cannot be compiled and shows an internal error.
open class TestClass {
open inner class Back {
open fun dd() { }
}
}
class Manager: TestClass() {
private val test = object : Back() {
override fun dd() { }
}
}
Cause:
Error generating constructors of class null with kind IMPLEMENTATION
What does it mean?

The example provided refers to KT-11833 and now compiles. Checked it with Kotlin version 1.1.0-beta-22.

Related

Why does the following not work for Dagger/Anvil

I have the following interface
interface A {
fun doSomething1()
fun doSomething2()
}
abstract Aable : A {
override fun doSomething1() {}
}
The following doesnt work
#ContributesBinding(ApplicationScope::class, boundType = A::class)
class B : Aable() {
}
nor does
#ContributesMultiBinding(ApplicationScope::class, boundType = A::class)
class B : Aable(), A {
}
It throws the error contributes a binding, but does not specify the bound type even when a boundType is explicitly provided.. I am new to Anvil and Dagger, so sorry if this is simple enough but I haven't been able to figure out how to get this to work.
Thanks.

Parameterized Test in Kotlin usign mehtod source for nested test class

I may use case the class under test has many cases so it is divided into a structure of inner classes. I want to write parameterized test cases to reduce boiler plate and code duplication.
For this I wanted to go with the approach of method source.
Class under test
class UnderTest
{
testThisMethod(a:String,b:String?){
// Do something
externalInterface.call(a?:b)
}
}
Test Case structure
internal class A() {
private val externalService = mockk<ExternalService>
private val test: UnderTest(externalService)
// Some general tests
//---Position Outter
inner class A {
//--- Position A
inner class B {
//--- Position C
#ParameterizedTest
#MethodSource("provideArguments")
fun `with arguments external service create object`(
argument1: String,
argument2: String,
expected: String
) {
// Some code
verify {
externalService.call(expected)
//some more verification
}
}
}
}
}
To provide the argument provider method I tried placing it at positions and got following errors
Position outer: initialization error :Could not find factory method in class
Position A,B: compilation error: companion not allowed here
How can this be achieved?
Try using #TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class A() {
private val externalService = mockk
private val test: UnderTest(externalService)
// Some general tests
//---Position Outter
inner class A {
//--- Position A
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class B {
//--- Position C
#ParameterizedTest
#MethodSource("provideArguments")
fun `with arguments external service create object`(
argument1: String,
argument2: String,
expected: String
) {
// Some code
verify {
externalService.call(expected)
//some more verification
}
}
}
}
}
Check here for more details

Custom UiObject2Condition in Kotlin

Is it possible to extend public abstract class UiObject2Condition?
Doing in this way
class NoChildCondition: UiObject2Condition<Boolean>() {
override fun apply(obj: UiObject2?): Boolean {
return obj?.childCount == 0
}
}
causes an error: public open fun apply(obj: UiObject2?): Boolean defined in circlecomplete.ciom.uitests.NoChildCondition' has no access to 'public/*package*/ abstract fun apply(p0: UiObject2!): Boolean! defined in androidx.test.uiautomator.UiObject2Condition', so it cannot override it.
Note that UiObject2Condition is an empty subclass of androidx.test.uiautomator.Condition<UiObject2, R> and Condition is not public.
Implementing apply method causes a compilation error. Not implementing it compiles but causes a runtime error.
UIAutomator version: 2.2.0
UiObject2Condition decompilation:
package androidx.test.uiautomator;
public abstract class UiObject2Condition<R> extends Condition<UiObject2, R> {
public UiObject2Condition() {
}
}
Condition decompilation:
package androidx.test.uiautomator;
abstract class Condition<T, R> {
Condition() {
}
abstract R apply(T var1);
}
Creating androidx.test.uiautomator package and adding class into it works fine.
package androidx.test.uiautomator
class NoChildCondition: UiObject2Condition<Boolean>() {
override fun apply(obj: UiObject2?): Boolean {
return obj?.childCount == 0
}
}

Hiding base class constructor parameters in Kotlin

I am trying to understand how to hide a base constructor parameter in a subclass in kotlin. How do you put a facade over a base constructor? This doesn't work:
import com.android.volley.Request
import com.android.volley.Response
class MyCustomRequest(url: String)
: Request<String>(Request.Method.POST, url, hiddenListener) {
private fun hiddenListener() = Response.ErrorListener {
/* super secret listener */
}
...
}
I think I understand the problem:
During construction of a new instance of a derived class, the base
class initialization is done as the first step (preceded only by
evaluation of the arguments for the base class constructor) and thus
happens before the initialization logic of the derived class is run.
I'm trying to solve this problem for Volley, where I need my custom request to be be a Request so that it can be passed into a RequestQueue. It would be easier of RequestQueue took in some kind of interface but since it doesn't I have to subclass. There are other ways I can hide these complexities from the caller, but this limitation has come up for me other times in Kotlin and I'm not sure how to solve it.
I am not familiar with volley but I tried to come up with an example that should give you some insight how to solve your problem. What you can do is use a companion object:
interface MyListener {
fun handleEvent()
}
open class Base<T>(anything: Any, val listener: MyListener) { // this would be your Request class
fun onSomeEvent() {
listener.handleEvent()
}
}
class Derived(anything: Any) : Base<Any>(anything, hiddenListener) { // this would be your MyCustomRequest class
private companion object {
private val hiddenListener = object : MyListener {
override fun handleEvent() {
// do secret stuff here
}
}
}
}
So if you apply this to your problem, the result should look something like this:
class MyCustomRequest(url: String)
: Request<String>(Request.Method.POST, url, hiddenListener) {
private companion object {
private val hiddenListener = Response.ErrorListener {
/* super secret listener */
}
}
...
}
A different way would be to use a decorator, create your Request withing that decorator and just delegate the calls to it:
class Decorator(anything: Any) {
private var inner: Base<Any>
private val hiddenListener: MyListener = object : MyListener {
override fun handleEvent() { }
}
init {
inner = Base(anything, hiddenListener)
}
}
And once again for your example that would look like this:
class MyCustomRequest(url: String) {
private var inner: Request<String>
private val hiddenListener = Response.ErrorListener {
/* super secret listener */
}
init {
inner = Request<String>(Request.Method.POST, url, hiddenListener)
}
...
}

Get companion class in companion object

Is there a way to get the javaClass of the companion class inside a companion object without knowing it's name?
I suppose I could get it by doing something like this:
open class TestClass {
companion object {
init {
val clazz = Class.forName(this::class.java.canonicalName.removeSuffix(".Companion"))
}
}
}
However, this does not work for class InheritingClass : TestClass(). It would still give me TestClass, not InheritingClass.
I was hoping for something more straightforward like this::class.companionClass.
Getting the class of the companion object of a given class will look like this:
TestClass::class.companionObject
Here's an example:
class TestClass {
companion object {
fun sayHello() = "Hello world"
}
}
If you want to get the class that contains the companion, since the latter is always an inner class of the former,
class TestClass {
companion object {
fun whichIsMyParentClass() = this::class.java.declaringClass // It'll return TestClass
}
}
And to further simplify, you'll also want to create an extension property:
import kotlin.reflect.KClass
val <T : Any> KClass<T>.companionClass get() =
if (isCompanion)
this.java.declaringClass
else
null
So, whenever you want to get the parent class of the companion object,
class TestClass {
companion object {
fun whichIsMyParentClass() = this::class.companionClass // It'll return TestClass
}
}
The companion class itself has no reference to the actual class as you can see in this bytecode
public final class TestClass$Companion {
private TestClass$Companion() { // <init> //()V
<localVar:index=0 , name=this , desc=LTestClass$Companion;, sig=null, start=L1, end=L2>
L1 {
aload0 // reference to self
invokespecial java/lang/Object <init>(()V);
return
}
L2 {
}
}
public TestClass$Companion(kotlin.jvm.internal.DefaultConstructorMarker arg0) { // <init> //(Lkotlin/jvm/internal/DefaultConstructorMarker;)V
<localVar:index=0 , name=this , desc=LTestClass$Companion;, sig=null, start=L1, end=L2>
<localVar:index=1 , name=$constructor_marker , desc=Lkotlin/jvm/internal/DefaultConstructorMarker;, sig=null, start=L1, end=L2>
L1 {
aload0 // reference to self
invokespecial TestClass$Companion <init>(()V);
return
}
L2 {
}
}
}
The reference is only the other way around (see decompiled kotlin class)
public final class TestClass {
public static final Companion companion = ...
}
So you can either do it as you just did by cutting off the .Companion part of the class name or you reference it by hard with TestClass::class.java (what is in my opinion no problem and the best solution)
If you need to print the class name, you can add simpleName, such as
this::class.java.declaringClass.simpleName