0

I have a json with class name, something like this:

{
   "view_class" : "com.view.MyClass"
}

And I have this hierarchy:

    public class Config {
         public String viewClass;
         ...
    }

    public interface ViewHolderInterface {
         void bindValue(...);
    }

    public class SuperMyClass extends RecyclerView.ViewHolder 
implements ViewHolderInterface {
       ... 
    }

    public class MyClass extends SuperMyClass {
       ... 
    }

I read a json file and I generate a "Config" object, so the idea is this:

public void bindViewHolder(SuperMyClass holder, Config config) {

    Class viewHolderClass;

    try {
        //row.viewHolderClass = "com.view.MyClass"
        viewHolderClass = Class.forName(config.viewClass);

    } catch (final Exception exception) {

    }
    // Here I need to "cast" a holder to MyClass and execute the method bindValue.
    final ViewHolderInterface viewHolderInstance =
        (ViewHolderInterface) viewHolderClass.cast(holder);
    viewHolderInstance.bindObjectValue(...);
}

I have tried to do this cast using reflection but I this throws an exception:

java.lang.ClassCastException: SuperMyClass cannot be cast to MyClass

I think that my error is using reflection and the same time try to up-cast, but I have not found another way to do this.

Consider that there may be more than one "view_class" type (remember that this class is an implementation of a ViewHolder).

Any idea?

Gaston Flores
  • 2,457
  • 3
  • 23
  • 42
  • 1
    you are not actually performing reflection, but only a downcasting that is breaking in runtime. Do you really need to perform this downcast? i ask because this statement `final ViewHolderInterface viewHolderInstance = (ViewHolderInterface) viewHolderClass.cast(holder);` is strange, for it performs two type castings. Before trying to solve this issue with reflection, maybe it would be better to rearchitect the inheritance of these classes – nandsito Apr 29 '17 at 21:55

1 Answers1

0

I think that my error is using reflection and the same time try to up-cast, but I have not found another way to do this.

Your reflection is ok, but the downcasting is not. Take a look at this question: explicit casting from super class to subclass

Consider that there may be more than one "view_class" type (remember that this class is an implementation of a ViewHolder).

You can inspect the object instance in runtime to find out its class. You need not tell the class by outside means.

You can try a safer approach like:

// mind the comparison order from subclass upward
if (MyClass.class.isInstance(holder)) {
    MyClass myClassHolder = (MyClass) holder;
} else if (SuperMyClass.class.isInstance(holder)) {
    SuperMyClass superMyClassHolder = (SuperMyClass) holder;
} else if (ViewHolder.class.isInstance(holder)) {
    ViewHolder viewHolder = (ViewHolder) holder;
}
Community
  • 1
  • 1
nandsito
  • 3,782
  • 2
  • 19
  • 26
  • Thanks for the response, is there a way for avoid "IFs"?...and use reflection for do this? – Gaston Flores Apr 29 '17 at 21:32
  • @GastonFlores i think the ifs are unavoidable, but maybe your problem may be solved in a whole different way. I'll make some questions – nandsito Apr 29 '17 at 21:46