Menu

InstantiationException

Help
2008-04-24
2013-04-25
  • Bruno Marchesson

    Hi,

    We are facing an issue with the latest beta12 release : it looks like beanlib tries to instantiate the abstract superclass rather than the concrete child.
    After investigation, it looks like the problem comes from the (new) 'chooseClass' method :

    Hibernate3JavaBeanReplicator)
    protected <T> T createToInstance(Object from, Class<T> toClass)
    throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException 
    {
    // figure out the pre-enhanced class
    Class<T> actualClass = UnEnhancer.getActualClass(from);
    Class<T> targetClass = chooseClass(actualClass, toClass);
    return newInstanceAsPrivileged(targetClass);
    }

    (ReplicatorTemplate)
    protected final <T> Class<T> chooseClass(Class<?> fromClass, Class<T> toClass) {
    return (Class<T>)(toClass.isAssignableFrom(fromClass) ? fromClass : toClass);
    }

    Maybe we can complete it with another test, such as :

    protected final <T> Class<T> chooseClass(Class<?> fromClass, Class<T> toClass) {
    if (toClass.isAssignableFrom(fromClass) == false)
    {
    return toClass;
    }
    else
    {
    // is the fromClass abstract ?
    int modifiers = fromClass.getModifiers();
    if (Modifier.isAbstract(modifiers))
    {
    // Do not choose abstract class
    return toClass;
    }
    else
    {
    return fromClass;
    }
    }
    }

    Do you think I am right ?

    Regards
    Bruno

     
    • Hanson Char

      Hanson Char - 2008-04-24

      Do you have a test case that can demonstrate this problem ?

       
    • Hanson Char

      Hanson Char - 2008-04-24

      Anyhow, your proposed solution looks reasonable.  Given an abstract class cannot be instantiated, returning the other class is probably the best it can do.  I've made the change and will prepare a release shortly.

       
    • Hanson Char

      Hanson Char - 2008-04-24

      You can now download beta13 for the fix.  Thanks for pointing this out :)

       
      • Bruno Marchesson

        Great :) !
        I try it ASAP and keep you informed.

        Thanks a lot for your quick answer !
        Bruno

         

Log in to post a comment.