VirtualBox

Changeset 48005 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Aug 22, 2013 6:53:11 PM (11 years ago)
Author:
vboxsync
Message:

Main/glue/glue-java.xsl: fix the heavily broken "in enum safearray" conversion helper code, both the webservice one (got confused by mixing up the enum names generated by jax-ws and the ones by our wrappers) and the local API one (tried to invoke a private constructor with a totally wrong signature) were totally wrong and probably never worked

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/glue/glue-java.xsl

    r47850 r48005  
    26702670import java.lang.reflect.Array;
    26712671import java.lang.reflect.Constructor;
     2672import java.lang.reflect.Method;
    26722673import java.lang.reflect.InvocationTargetException;
    26732674
     
    27652766    }
    27662767
     2768    @SuppressWarnings( "unchecked")
    27672769    public static <T> List<T> wrapEnum(Class<T> wrapperClass, long values[])
    27682770    {
     
    27712773            if (values == null)
    27722774                return null;
    2773             Constructor<T> c = wrapperClass.getConstructor(int.class);
     2775            //// This code is questionable, as it invokes a private constructor
     2776            //// (all enums only have default constructors), and we don't really
     2777            //// know what to pass as the name, and the ordinal may or may not
     2778            //// be sensible, especially if the long was abused as a bitset.
     2779            //Constructor<T> c = wrapperClass.getDeclaredConstructor(String.class, int.class, int.class);
     2780            //c.setAccessible(true); // make it callable
     2781            //List<T> ret = new ArrayList<T>(values.length);
     2782            //for (long v : values)
     2783            //{
     2784            //    T convEnum = c.newInstance("unknown", (int)v, (int)v);
     2785            //    ret.add(convEnum);
     2786            //}
     2787
     2788            // Alternative implementation: use the fromValue method, which is
     2789            // what the code handling single enums will do. I see no reason to
     2790            // use the above very ugly hack if there are better alternatives,
     2791            // which as a bonus complain about unknown values. This variant is
     2792            // slower, but also orders of magnitude safer.
     2793            java.lang.reflect.Method fromValue = wrapperClass.getMethod("fromValue", long.class);
    27742794            List<T> ret = new ArrayList<T>(values.length);
    27752795            for (long v : values)
    27762796            {
    2777                 ret.add(c.newInstance(v));
     2797                T convEnum = (T)fromValue.invoke(null, v);
     2798                ret.add(convEnum);
    27782799            }
    27792800            return ret;
     
    27832804            throw new AssertionError(e);
    27842805        }
    2785         catch (InstantiationException e)
    2786         {
    2787             throw new AssertionError(e);
    2788         }
     2806        //catch (InstantiationException e)
     2807        //{
     2808        //    throw new AssertionError(e);
     2809        //}
    27892810        catch (IllegalAccessException e)
    27902811        {
     
    39984019            if (values == null)
    39994020                return null;
    4000             java.lang.reflect.Method fromValue = toClass.getMethod("fromValue", String.class);
    40014021            List<T2> ret = new ArrayList<T2>(values.size());
    40024022            for (T1 v : values)
    40034023            {
    4004                 // static method is called with null this
    4005                 ret.add((T2)fromValue.invoke(null, v.name()));
     4024                // Ordinal based enum conversion, as JAX-WS "invents" its own
     4025                // enum names and has string values with the expected content.
     4026                int enumOrdinal = v.ordinal();
     4027                T2 convEnum = toClass.getEnumConstants()[enumOrdinal];
     4028                ret.add(convEnum);
    40064029            }
    40074030            return ret;
    40084031        }
    4009         catch (NoSuchMethodException e)
    4010         {
    4011             throw new AssertionError(e);
    4012         }
    4013         catch (IllegalAccessException e)
    4014         {
    4015             throw new AssertionError(e);
    4016         }
    4017         catch (InvocationTargetException e)
     4032        catch (ArrayIndexOutOfBoundsException e)
    40184033        {
    40194034            throw new AssertionError(e);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette