jeudi 9 avril 2020

Java reflection to initialize Kotlin object fails when object contains 'init'

I have some Java code that uses reflection to create a new object. The new object is an object I coded in Kotlin. The code works fine, unless in my Kotlin class I use init to try and initialize some variables.

eg:

private BaseClass constructViewHolderClass(View itemView, int viewType) {
    View itemView = ...

    Class<? extends BaseClass> baseClass = ChildBaseClass.class;

    Constructor<? extends BaseClass> ctor;

    ctor = baseClass.getConstructor(View.class);

    return (BaseClass) ctor.newInstance(new Object[]{itemView});
}

Kotlin class that works:

class ChildBaseClass(itemView: View) : BaseClass(itemView) {

    var imageView: ImageView = itemView.findViewById(R.id.image_view)
    var circularProgressDrawable = CircularProgressDrawable(itemView.context)
}

Kotlin class that does NOT work:

class ChildBaseClass(itemView: View) : BaseClass(itemView) {

    var imageView: ImageView = itemView.findViewById(R.id.image_view)
    var circularProgressDrawable = CircularProgressDrawable(itemView.context)

    init {
        circularProgressDrawable.strokeWidth = 5f
        circularProgressDrawable.centerRadius = 30f
        circularProgressDrawable.start()
    }
}

The exception thrown is java.lang.IllegalArgumentException: class java.lang.reflect.InvocationTargetException

Stacktrace:

Process: com.project.acme, PID: 12668
java.lang.IllegalArgumentException: class java.lang.reflect.InvocationTargetException
    at com.project.acme.extlibs.myrecyclerview.MYRecyclerViewAdapter.constructViewHolderClass(MYRecyclerViewAdapter.java:844)
    at com.project.acme.extlibs.myrecyclerview.MYRecyclerViewAdapter.onCreateViewHolder(MYRecyclerViewAdapter.java:830)
    at com.project.acme.extlibs.myrecyclerview.MYRecyclerViewAdapter.onCreateViewHolder(MYRecyclerViewAdapter.java:16)
    at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
    at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
    at android.view.View.layout(View.java:20822)
    at android.view.ViewGroup.layout(ViewGroup.java:6401)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20822)
    at android.view.ViewGroup.layout(ViewGroup.java:6401)
    at androidx.swiperefreshlayout.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:625)
    at android.view.View.layout(View.java:20822)
    at android.view.ViewGroup.layout(ViewGroup.java:6401)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20822)
    at android.view.ViewGroup.layout(ViewGroup.java:6401)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20822)
    at android.view.ViewGroup.layout(ViewGroup.java:6401)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:20822)
    at android.view.ViewGroup.layout(ViewGroup.java:6401)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20822)
    at android.view.ViewGroup.layout(ViewGroup.java:6401)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:20822)
    at android.view.ViewGroup.layout(ViewGroup.java:6401)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20822)
    at android.view.ViewGroup.layout(ViewGroup.java:6401)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:20822)
    at android.view.ViewGroup.layout(ViewGroup.java:6401)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)




Aucun commentaire:

Enregistrer un commentaire