diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2016-05-13 19:19:32 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2016-05-13 19:28:56 +0200 |
commit | 5b78551dcf54158adffe3236a45946942af5f354 (patch) | |
tree | d7786aea5864674e68ad5a277b9d0d5aafbb7992 /ridljar | |
parent | 86ab6471520a5300df2b8bc577987550362f4921 (diff) |
Related tdf#99272: Prevent bogus com.sun.star.uno.Type(Short[].class) etc.
...so that the JNI UNO bridge will not accidentally reinterpret a Short[] as a
short[]. <https://wiki.openoffice.org/wiki/Uno/Java/Specifications/Type_Mapping>
makes it clear that the former is not a valid Java representation for UNO type
"sequence of short".
Needed one fix in bogus test code. Also fixed the two bogus places in odk/examples
mentioned in f53e427291321eabe8d060a737e750a94739f911 "Resolves: tdf#99272 new
Short[] used instead of new short[]".
Change-Id: I8321eb1294ec77b3a9bf73cafb6e7fe337157bb7
Diffstat (limited to 'ridljar')
-rw-r--r-- | ridljar/com/sun/star/uno/Type.java | 93 | ||||
-rw-r--r-- | ridljar/test/com/sun/star/uno/Type_Test.java | 6 |
2 files changed, 65 insertions, 34 deletions
diff --git a/ridljar/com/sun/star/uno/Type.java b/ridljar/com/sun/star/uno/Type.java index c1b0e3e7ec20..018a6069d56d 100644 --- a/ridljar/com/sun/star/uno/Type.java +++ b/ridljar/com/sun/star/uno/Type.java @@ -78,65 +78,81 @@ public class Type { TYPE_NAME_ANY }; - private static final HashMap<Class<?>, TypeClass[]> __javaClassToTypeClass = new HashMap<Class<?>, TypeClass[]>(); + private static final class TypeInfo { + TypeInfo( + TypeClass thePrimary, TypeClass theAlternative, + boolean theSequenceComponentType) + { + primary = thePrimary; + alternative = theAlternative; + sequenceComponentType = theSequenceComponentType; + } + + final TypeClass primary; + final TypeClass alternative; + final boolean sequenceComponentType; + } + + private static final HashMap<Class<?>, TypeInfo> __javaClassToTypeClass = + new HashMap<Class<?>, TypeInfo>(); static { __javaClassToTypeClass.put( - void.class, new TypeClass[] { TypeClass.VOID, TypeClass.VOID }); + void.class, new TypeInfo(TypeClass.VOID, TypeClass.VOID, false)); __javaClassToTypeClass.put( - Void.class, new TypeClass[] { TypeClass.VOID, TypeClass.VOID }); + Void.class, new TypeInfo(TypeClass.VOID, TypeClass.VOID, false)); __javaClassToTypeClass.put( boolean.class, - new TypeClass[] { TypeClass.BOOLEAN, TypeClass.BOOLEAN }); + new TypeInfo(TypeClass.BOOLEAN, TypeClass.BOOLEAN, true)); __javaClassToTypeClass.put( Boolean.class, - new TypeClass[] { TypeClass.BOOLEAN, TypeClass.BOOLEAN }); + new TypeInfo(TypeClass.BOOLEAN, TypeClass.BOOLEAN, false)); __javaClassToTypeClass.put( - byte.class, new TypeClass[] { TypeClass.BYTE, TypeClass.BYTE }); + byte.class, new TypeInfo(TypeClass.BYTE, TypeClass.BYTE, true)); __javaClassToTypeClass.put( - Byte.class, new TypeClass[] { TypeClass.BYTE, TypeClass.BYTE }); + Byte.class, new TypeInfo(TypeClass.BYTE, TypeClass.BYTE, false)); __javaClassToTypeClass.put( short.class, - new TypeClass[] { TypeClass.SHORT, TypeClass.UNSIGNED_SHORT }); + new TypeInfo(TypeClass.SHORT, TypeClass.UNSIGNED_SHORT, true)); __javaClassToTypeClass.put( Short.class, - new TypeClass[] { TypeClass.SHORT, TypeClass.UNSIGNED_SHORT }); + new TypeInfo(TypeClass.SHORT, TypeClass.UNSIGNED_SHORT, false)); __javaClassToTypeClass.put( int.class, - new TypeClass[] { TypeClass.LONG, TypeClass.UNSIGNED_LONG }); + new TypeInfo(TypeClass.LONG, TypeClass.UNSIGNED_LONG, true)); __javaClassToTypeClass.put( Integer.class, - new TypeClass[] { TypeClass.LONG, TypeClass.UNSIGNED_LONG }); + new TypeInfo(TypeClass.LONG, TypeClass.UNSIGNED_LONG, false)); __javaClassToTypeClass.put( long.class, - new TypeClass[] { TypeClass.HYPER, TypeClass.UNSIGNED_HYPER }); + new TypeInfo(TypeClass.HYPER, TypeClass.UNSIGNED_HYPER, true)); __javaClassToTypeClass.put( Long.class, - new TypeClass[] { TypeClass.HYPER, TypeClass.UNSIGNED_HYPER }); + new TypeInfo(TypeClass.HYPER, TypeClass.UNSIGNED_HYPER, false)); __javaClassToTypeClass.put( - float.class, new TypeClass[] { TypeClass.FLOAT, TypeClass.FLOAT }); + float.class, new TypeInfo(TypeClass.FLOAT, TypeClass.FLOAT, true)); __javaClassToTypeClass.put( - Float.class, new TypeClass[] { TypeClass.FLOAT, TypeClass.FLOAT }); + Float.class, new TypeInfo(TypeClass.FLOAT, TypeClass.FLOAT, false)); __javaClassToTypeClass.put( double.class, - new TypeClass[] { TypeClass.DOUBLE, TypeClass.DOUBLE }); + new TypeInfo(TypeClass.DOUBLE, TypeClass.DOUBLE, true)); __javaClassToTypeClass.put( Double.class, - new TypeClass[] { TypeClass.DOUBLE, TypeClass.DOUBLE }); + new TypeInfo(TypeClass.DOUBLE, TypeClass.DOUBLE, false)); __javaClassToTypeClass.put( - char.class, new TypeClass[] { TypeClass.CHAR, TypeClass.CHAR }); + char.class, new TypeInfo(TypeClass.CHAR, TypeClass.CHAR, true)); __javaClassToTypeClass.put( Character.class, - new TypeClass[] { TypeClass.CHAR, TypeClass.CHAR }); + new TypeInfo(TypeClass.CHAR, TypeClass.CHAR, false)); __javaClassToTypeClass.put( String.class, - new TypeClass[] { TypeClass.STRING, TypeClass.STRING }); + new TypeInfo(TypeClass.STRING, TypeClass.STRING, true)); __javaClassToTypeClass.put( - Type.class, new TypeClass[] { TypeClass.TYPE, TypeClass.TYPE }); + Type.class, new TypeInfo(TypeClass.TYPE, TypeClass.TYPE, true)); __javaClassToTypeClass.put( - Any.class, new TypeClass[] { TypeClass.ANY, TypeClass.ANY }); + Any.class, new TypeInfo(TypeClass.ANY, TypeClass.ANY, true)); __javaClassToTypeClass.put( Object.class, - new TypeClass[] { TypeClass.ANY, TypeClass.INTERFACE }); + new TypeInfo(TypeClass.ANY, TypeClass.INTERFACE, true)); } public static final Type VOID = new Type(void.class); @@ -162,7 +178,7 @@ public class Type { * Constructs a new <code>Type</code> which defaults to <code>VOID</code>. */ public Type() { - init(null, void.class, false, false); + init(null, void.class, false, false, false); } /** @@ -190,7 +206,7 @@ public class Type { * <code>null</code>. */ public Type(Class<?> zClass) { - init(null, zClass, false, false); + init(null, zClass, false, false, false); } /** @@ -244,7 +260,13 @@ public class Type { * @since UDK 3.2.0 */ public Type(Class<?> zClass, boolean alternative) { - init(null, zClass, alternative, false); + init(null, zClass, alternative, false, false); + } + + private Type( + Class<?> zClass, boolean alternative, boolean sequenceComponentType) + { + init(null, zClass, alternative, false, sequenceComponentType); } /** @@ -282,7 +304,7 @@ public class Type { init( typeName, Class.forName(i < 0 ? typeName : typeName.substring(0, i)), - false, i >= 0); + false, i >= 0, false); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } @@ -449,14 +471,19 @@ public class Type { } private void init( - String name, Class<?> zClass, boolean alternative, boolean arguments) + String name, Class<?> zClass, boolean alternative, boolean arguments, + boolean sequenceComponentType) { - TypeClass[] tc = __javaClassToTypeClass.get(zClass); - if (tc != null) { - // tc only contains primitive type classes, except for + TypeInfo info = __javaClassToTypeClass.get(zClass); + if (info != null) { + if (sequenceComponentType && !info.sequenceComponentType) { + throw new IllegalArgumentException( + zClass + " cannot be sequence component type"); + } + // info only contains primitive type classes, except for // TypeClass.INTERFACE, which stands for XInterface (the alternative // interpretation of java.lang.Object): - _typeClass = tc[alternative ? 1 : 0]; + _typeClass = alternative ? info.alternative : info.primary; _typeName = _typeClass == TypeClass.INTERFACE ? XInterface.class.getName() : __typeClassToTypeName[_typeClass.getValue()]; @@ -465,7 +492,7 @@ public class Type { // java.lang.Boolean.class); getZClass will later calculate the // correct class when needed } else if (zClass.isArray()) { - Type t = new Type(zClass.getComponentType(), alternative); + Type t = new Type(zClass.getComponentType(), alternative, true); _typeClass = t.getTypeClass() != TypeClass.UNKNOWN ? TypeClass.SEQUENCE : TypeClass.UNKNOWN; _typeName = "[]" + t.getTypeName(); diff --git a/ridljar/test/com/sun/star/uno/Type_Test.java b/ridljar/test/com/sun/star/uno/Type_Test.java index e81142c64ac0..1499b8230e4b 100644 --- a/ridljar/test/com/sun/star/uno/Type_Test.java +++ b/ridljar/test/com/sun/star/uno/Type_Test.java @@ -76,7 +76,11 @@ public final class Type_Test { assertSame(boolean.class, new Type(boolean.class).getZClass()); assertSame(boolean.class, new Type(Boolean.class).getZClass()); assertSame(boolean[].class, new Type(boolean[].class).getZClass()); - assertSame(boolean[].class, new Type(Boolean[].class).getZClass()); + + try { + new Type(Boolean[].class); + fail(); + } catch (java.lang.RuntimeException e) {} } @Test public void testIsSupertypeOf() { |