Changeset 48005 in vbox for trunk/src/VBox
- Timestamp:
- Aug 22, 2013 6:53:11 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/glue/glue-java.xsl
r47850 r48005 2670 2670 import java.lang.reflect.Array; 2671 2671 import java.lang.reflect.Constructor; 2672 import java.lang.reflect.Method; 2672 2673 import java.lang.reflect.InvocationTargetException; 2673 2674 … … 2765 2766 } 2766 2767 2768 @SuppressWarnings( "unchecked") 2767 2769 public static <T> List<T> wrapEnum(Class<T> wrapperClass, long values[]) 2768 2770 { … … 2771 2773 if (values == null) 2772 2774 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); 2774 2794 List<T> ret = new ArrayList<T>(values.length); 2775 2795 for (long v : values) 2776 2796 { 2777 ret.add(c.newInstance(v)); 2797 T convEnum = (T)fromValue.invoke(null, v); 2798 ret.add(convEnum); 2778 2799 } 2779 2800 return ret; … … 2783 2804 throw new AssertionError(e); 2784 2805 } 2785 catch (InstantiationException e)2786 {2787 throw new AssertionError(e);2788 }2806 //catch (InstantiationException e) 2807 //{ 2808 // throw new AssertionError(e); 2809 //} 2789 2810 catch (IllegalAccessException e) 2790 2811 { … … 3998 4019 if (values == null) 3999 4020 return null; 4000 java.lang.reflect.Method fromValue = toClass.getMethod("fromValue", String.class);4001 4021 List<T2> ret = new ArrayList<T2>(values.size()); 4002 4022 for (T1 v : values) 4003 4023 { 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); 4006 4029 } 4007 4030 return ret; 4008 4031 } 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) 4018 4033 { 4019 4034 throw new AssertionError(e);
Note:
See TracChangeset
for help on using the changeset viewer.