From 4418ad0388b58cab6a129908f78baec5bc73906e Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Thu, 28 Jun 2012 11:01:03 +0200 Subject: Java5 update - convert code to use generics Note that WeakMap no longer extends Map, because it does not conform to the generic Map interface. Change-Id: I72ea681528c932d84c95f095434e1dc95b0a3d16 --- .../sun/star/lib/uno/typedesc/TypeDescription.java | 44 ++++++------- ridljar/com/sun/star/lib/util/WeakMap.java | 76 +++++++++++++--------- ridljar/com/sun/star/uno/Any.java | 2 +- ridljar/com/sun/star/uno/ITypeDescription.java | 2 +- ridljar/com/sun/star/uno/Type.java | 26 ++++---- ridljar/com/sun/star/uno/UnoRuntime.java | 36 +++++----- .../com/sun/star/lib/unoloader/UnoLoader.java | 2 +- .../test/com/sun/star/lib/util/WeakMap_Test.java | 2 +- ridljar/test/com/sun/star/uno/UnoRuntime_Test.java | 5 +- 9 files changed, 104 insertions(+), 91 deletions(-) diff --git a/ridljar/com/sun/star/lib/uno/typedesc/TypeDescription.java b/ridljar/com/sun/star/lib/uno/typedesc/TypeDescription.java index f97666a03e86..f532133299d2 100644 --- a/ridljar/com/sun/star/lib/uno/typedesc/TypeDescription.java +++ b/ridljar/com/sun/star/lib/uno/typedesc/TypeDescription.java @@ -55,7 +55,7 @@ public final class TypeDescription implements ITypeDescription { return get(t); } - public static TypeDescription getTypeDescription(Class zClass) { + public static TypeDescription getTypeDescription(Class zClass) { return getDefinitely(new Type(zClass)); } @@ -206,7 +206,7 @@ public final class TypeDescription implements ITypeDescription { } // @see ITypeDescription#getZClass - public Class getZClass() { + public Class getZClass() { return zClass; } @@ -244,7 +244,7 @@ public final class TypeDescription implements ITypeDescription { { TypeClass typeClass = type.getTypeClass(); String typeName = type.getTypeName(); - Class zClass = type.getZClass(); + Class zClass = type.getZClass(); if (zClass == null) { throw new ClassNotFoundException("UNO type " + type); } @@ -322,7 +322,7 @@ public final class TypeDescription implements ITypeDescription { { // This code exploits the fact that an instantiated polymorphic // struct type may not be the direct base of a struct type: - Class superClass = zClass.getSuperclass(); + Class superClass = zClass.getSuperclass(); TypeDescription[] superTypes = superClass != Object.class ? new TypeDescription[] { get(new Type(superClass)) } : null; @@ -349,7 +349,7 @@ public final class TypeDescription implements ITypeDescription { case TypeClass.INTERFACE_value: { List superTypes = new List(); - Class[] interfaces = zClass.getInterfaces(); + Class[] interfaces = zClass.getInterfaces(); for (int i = 0; i < interfaces.length; ++i) { Type t = new Type(interfaces[i]); if (t.getTypeClass() == TypeClass.INTERFACE) { @@ -374,7 +374,7 @@ public final class TypeDescription implements ITypeDescription { private TypeDescription( TypeClass typeClass, String typeName, String arrayTypeName, - Class zClass, TypeDescription[] superTypes, + Class zClass, TypeDescription[] superTypes, ITypeDescription componentType) { this.typeClass = typeClass; @@ -414,16 +414,16 @@ public final class TypeDescription implements ITypeDescription { getDefinitely(Type.VOID), null) }; } else { int methodOffset = 0; - ArrayList superList = new ArrayList(); + ArrayList superList = new ArrayList(); for (int i = 0; i < superTypes.length; ++i) { IMethodDescription[] ds = superTypes[i].getMethodDescriptions(); for (int j = 0; j < ds.length; ++j) { superList.add(new MethodDescription(ds[j], methodOffset++)); } } - superMethodDescriptions = (IMethodDescription[]) superList.toArray( + superMethodDescriptions = superList.toArray( new IMethodDescription[superList.size()]); - ArrayList directList = new ArrayList(); + ArrayList directList = new ArrayList(); TypeInfo[] infos = getTypeInfo(); int infoCount = infos == null ? 0 : infos.length; int index = 0; @@ -465,7 +465,7 @@ public final class TypeDescription implements ITypeDescription { + ": entries not ordererd"); } Method method = findMethod(methods, info.getName()); - Class[] params = method.getParameterTypes(); + Class[] params = method.getParameterTypes(); ITypeDescription[] in = new ITypeDescription[params.length]; ITypeDescription[] out = new ITypeDescription[params.length]; @@ -499,7 +499,7 @@ public final class TypeDescription implements ITypeDescription { method)); } } - methodDescriptions = (IMethodDescription[]) directList.toArray( + methodDescriptions = directList.toArray( new IMethodDescription[directList.size()]); } } @@ -512,7 +512,7 @@ public final class TypeDescription implements ITypeDescription { if (i < 0) { return null; } - java.util.List args = new java.util.ArrayList(); + java.util.List args = new java.util.ArrayList(); do { ++i; // skip '<' or ',' int j = i; @@ -555,7 +555,7 @@ public final class TypeDescription implements ITypeDescription { throw new IllegalArgumentException( "UNO type name \"" + typeName + "\" is syntactically invalid"); } - return (TypeDescription[]) args.toArray( + return args.toArray( new TypeDescription[args.size()]); } @@ -626,7 +626,7 @@ public final class TypeDescription implements ITypeDescription { } private static ITypeDescription getTypeDescription( - Class zClass, TypeInfo typeInfo) + Class zClass, TypeInfo typeInfo) { return getDefinitely( new Type( @@ -649,11 +649,11 @@ public final class TypeDescription implements ITypeDescription { } public TypeDescription[] toArray() { - return (TypeDescription[]) list.toArray( + return list.toArray( new TypeDescription[list.size()]); } - private final ArrayList list = new ArrayList(); + private final ArrayList list = new ArrayList(); } private static final class Cache { @@ -662,7 +662,7 @@ public final class TypeDescription implements ITypeDescription { public TypeDescription get(String typeName) { synchronized (map) { cleanUp(); - Entry e = (Entry) map.get(typeName); + Entry e = map.get(typeName); return e == null ? null : (TypeDescription) e.get(); } } @@ -684,8 +684,8 @@ public final class TypeDescription implements ITypeDescription { } } - private static final class Entry extends SoftReference { - public Entry(TypeDescription desc, ReferenceQueue queue) { + private static final class Entry extends SoftReference { + public Entry(TypeDescription desc, ReferenceQueue queue) { super(desc, queue); typeName = desc.getTypeName(); } @@ -693,8 +693,8 @@ public final class TypeDescription implements ITypeDescription { public final String typeName; } - private final HashMap map = new HashMap(); - private final ReferenceQueue queue = new ReferenceQueue(); + private final HashMap map = new HashMap(); + private final ReferenceQueue queue = new ReferenceQueue(); } private static final Cache cache = new Cache(); @@ -702,7 +702,7 @@ public final class TypeDescription implements ITypeDescription { private final TypeClass typeClass; private final String typeName; private final String arrayTypeName; - private final Class zClass; + private final Class zClass; private final TypeDescription[] superTypes; private final ITypeDescription componentType; private final boolean hasTypeArguments; diff --git a/ridljar/com/sun/star/lib/util/WeakMap.java b/ridljar/com/sun/star/lib/util/WeakMap.java index a8b72233cfb3..c9ed21d159de 100644 --- a/ridljar/com/sun/star/lib/util/WeakMap.java +++ b/ridljar/com/sun/star/lib/util/WeakMap.java @@ -58,7 +58,15 @@ import java.util.Set; * WeakReference wrappers are automatically cleared as soon as the * values are disposed.

*/ -public final class WeakMap implements Map { +public final class WeakMap { + + /** + * Declare the map as WeakReference instead of Entry because it makes the return + * type signatures of values() and keySet() cleaner. + */ + private final HashMap> map = new HashMap>(); + private final ReferenceQueue queue = new ReferenceQueue(); + /** * Constructs an empty WeakMap. */ @@ -70,7 +78,7 @@ public final class WeakMap implements Map { * * @param m the map whose mappings are to be placed in this map */ - public WeakMap(Map m) { + public WeakMap(Map m) { putAll(m); } @@ -108,7 +116,7 @@ public final class WeakMap implements Map { * @return true if this map contains a mapping for the * specified key */ - public boolean containsKey(Object key) { + public boolean containsKey(K key) { return map.containsKey(key); } @@ -122,7 +130,7 @@ public final class WeakMap implements Map { * @return true if this map maps one or more keys to the * specified value */ - public boolean containsValue(Object value) { + public boolean containsValue(WeakReference value) { return map.containsValue(value); } @@ -137,7 +145,7 @@ public final class WeakMap implements Map { * @return the value to which this map maps the specified key, or * null if the map contains no mapping for this key */ - public Object get(Object key) { + public WeakReference get(K key) { return map.get(key); } @@ -153,9 +161,9 @@ public final class WeakMap implements Map { * @return previous value associated with the specified key, or * null if there was no mapping for the key */ - public Object put(Object key, Object value) { + public WeakReference put(K key, V value) { cleanUp(); - return map.put(key, new Entry(key, value, queue)); + return map.put(key, new Entry(key, value, queue)); } /** @@ -167,7 +175,7 @@ public final class WeakMap implements Map { * @return previous value associated with the specified key, or * null if there was no mapping for the key */ - public Object remove(Object key) { + public WeakReference remove(K key) { cleanUp(); return map.remove(key); } @@ -181,12 +189,12 @@ public final class WeakMap implements Map { * must be plain objects, which are then wrapped in instances of * WeakReference. */ - public void putAll(Map t) { + public void putAll(Map m) { cleanUp(); - for (Iterator i = t.entrySet().iterator(); i.hasNext();) { - Map.Entry e = (Map.Entry) i.next(); - Object k = e.getKey(); - map.put(k, new Entry(k, e.getValue(), queue)); + for (Iterator> i = m.entrySet().iterator(); i.hasNext();) { + Map.Entry e = i.next(); + K k = e.getKey(); + map.put(k, new Entry(k, e.getValue(), queue)); } } @@ -207,7 +215,7 @@ public final class WeakMap implements Map { * * @return a set view of the keys contained in this map */ - public Set keySet() { + public Set keySet() { return map.keySet(); } @@ -218,7 +226,7 @@ public final class WeakMap implements Map { * * @return a collection view of the values contained in this map */ - public Collection values() { + public Collection> values() { return map.values(); } @@ -229,7 +237,7 @@ public final class WeakMap implements Map { * * @return a collection view of the mappings contained in this map */ - public Set entrySet() { + public Set>> entrySet() { return map.entrySet(); } @@ -253,17 +261,20 @@ public final class WeakMap implements Map { * @return the referent of the specified WeakReference, or * null if ref is null */ - public static Object getValue(Object ref) { - return ref == null ? null : ((WeakReference) ref).get(); + public static T getValue(WeakReference ref) { + return ref == null ? null : ref.get(); } - // cleanUp must only be called from within modifying methods. Otherwise, - // the implementations of entrySet, keySet and values would break - // (specificially, iterating over the collections returned by those - // methods), as non-modifying methods might modify the underlying map. + /** + * cleanUp() must only be called from within modifying methods. Otherwise, + * the implementations of entrySet, keySet and values would break + * (Specifically, iterating over the collections returned by those + * methods), as non-modifying methods might modify the underlying map. + **/ + @SuppressWarnings("unchecked") private void cleanUp() { for (;;) { - Entry e = (Entry) queue.poll(); + Entry e = (Entry) queue.poll(); if (e == null) { break; } @@ -278,15 +289,12 @@ public final class WeakMap implements Map { } } - private static final class Entry extends WeakReference + private static final class Entry extends WeakReference implements DisposeListener { - public void notifyDispose(DisposeNotifier source) { - Entry.this.clear(); // qualification needed for Java 1.3 - enqueue(); - } + private final K key; - private Entry(Object key, Object value, ReferenceQueue queue) { + private Entry(K key, V value, ReferenceQueue queue) { super(value, queue); this.key = key; if (value instanceof DisposeNotifier) { @@ -294,9 +302,13 @@ public final class WeakMap implements Map { } } - private final Object key; + /** + * @see DisposeListener#notifyDispose(DisposeNotifier) + */ + public void notifyDispose(DisposeNotifier source) { + clear(); + enqueue(); + } } - private final HashMap map = new HashMap(); - private final ReferenceQueue queue = new ReferenceQueue(); } diff --git a/ridljar/com/sun/star/uno/Any.java b/ridljar/com/sun/star/uno/Any.java index ec068f744335..ba1d9f643b56 100644 --- a/ridljar/com/sun/star/uno/Any.java +++ b/ridljar/com/sun/star/uno/Any.java @@ -57,7 +57,7 @@ public class Any { * @param object the data of the any. * @deprecated as of UDK 2.0 */ - public Any(Class zInterface, Object object) { + public Any(Class zInterface, Object object) { this(new Type(zInterface), object); } diff --git a/ridljar/com/sun/star/uno/ITypeDescription.java b/ridljar/com/sun/star/uno/ITypeDescription.java index 910368a7bb9f..85014454e253 100644 --- a/ridljar/com/sun/star/uno/ITypeDescription.java +++ b/ridljar/com/sun/star/uno/ITypeDescription.java @@ -175,5 +175,5 @@ public interface ITypeDescription { *

* @return the corresponding java class. */ - Class getZClass(); + Class getZClass(); } diff --git a/ridljar/com/sun/star/uno/Type.java b/ridljar/com/sun/star/uno/Type.java index 31766441b59c..539c6ebd6a77 100644 --- a/ridljar/com/sun/star/uno/Type.java +++ b/ridljar/com/sun/star/uno/Type.java @@ -78,7 +78,7 @@ public class Type { TYPE_NAME_ANY }; - private static final HashMap __javaClassToTypeClass = new HashMap(); + private static final HashMap, TypeClass[]> __javaClassToTypeClass = new HashMap, TypeClass[]>(); static { __javaClassToTypeClass.put( void.class, new TypeClass[] { TypeClass.VOID, TypeClass.VOID }); @@ -189,7 +189,7 @@ public class Type { * @param zClass the Java class of this type. Must not be * null. */ - public Type(Class zClass) { + public Type(Class zClass) { init(null, zClass, false, false); } @@ -243,7 +243,7 @@ public class Type { * * @since UDK 3.2.0 */ - public Type(Class zClass, boolean alternative) { + public Type(Class zClass, boolean alternative) { init(null, zClass, alternative, false); } @@ -333,7 +333,7 @@ public class Type { * @return the type name; may be null in extreme situations * (inconsistent TypeClass, error loading a class) */ - public Class getZClass() { + public Class getZClass() { synchronized (this) { if (_class == null) { _class = determineClass(); @@ -401,8 +401,8 @@ public class Type { } case TypeClass.EXCEPTION_value: case TypeClass.INTERFACE_value: - Class c1 = getZClass(); - Class c2 = type.getZClass(); + Class c1 = getZClass(); + Class c2 = type.getZClass(); return c1 != null && c2 != null && c1.isAssignableFrom(c2); default: @@ -428,9 +428,9 @@ public class Type { } private void init( - String name, Class zClass, boolean alternative, boolean arguments) + String name, Class zClass, boolean alternative, boolean arguments) { - TypeClass[] tc = (TypeClass[]) __javaClassToTypeClass.get(zClass); + TypeClass[] tc = __javaClassToTypeClass.get(zClass); if (tc != null) { // tc only contains primitive type classes, except for // TypeClass.INTERFACE, which stands for XInterface (the alternative @@ -488,7 +488,7 @@ public class Type { } } - private Class determineClass() { + private Class determineClass() { switch (_typeClass.getValue()) { case TypeClass.VOID_value: return _typeName.equals(TYPE_NAME_VOID) ? void.class : null; @@ -580,7 +580,7 @@ public class Type { if (args >= 0) { base = base.substring(0, args); } - Class c; + Class c; try { c = Class.forName(base); } catch (ClassNotFoundException e) { @@ -604,7 +604,7 @@ public class Type { case TypeClass.EXCEPTION_value: case TypeClass.INTERFACE_value: { - Class c; + Class c; try { c = Class.forName(_typeName); } catch (ClassNotFoundException e) { @@ -616,7 +616,7 @@ public class Type { case TypeClass.STRUCT_value: { int args = _typeName.indexOf('<'); - Class c; + Class c; try { c = Class.forName( args < 0 ? _typeName : _typeName.substring(0, args)); @@ -638,6 +638,6 @@ public class Type { protected TypeClass _typeClass; // TODO should be final protected String _typeName; // TODO should be final - protected Class _class; + protected Class _class; protected ITypeDescription _iTypeDescription; } diff --git a/ridljar/com/sun/star/uno/UnoRuntime.java b/ridljar/com/sun/star/uno/UnoRuntime.java index bac97b57e021..e20ba72e492d 100644 --- a/ridljar/com/sun/star/uno/UnoRuntime.java +++ b/ridljar/com/sun/star/uno/UnoRuntime.java @@ -149,7 +149,7 @@ public class UnoRuntime { } } // Ensure that the object implements the requested interface type: - Class c = type.getZClass(); + Class c = type.getZClass(); if (c == null || !c.isInstance(object)) { object = null; } @@ -367,7 +367,7 @@ public class UnoRuntime { * if no context has been set for the current thread */ public static XCurrentContext getCurrentContext() { - return (XCurrentContext) currentContext.get(); + return currentContext.get(); } /** @@ -407,13 +407,13 @@ public class UnoRuntime { throws java.lang.Exception { synchronized (environments) { - IEnvironment env = (IEnvironment) WeakMap.getValue( + IEnvironment env = WeakMap.getValue( environments.get(name + context)); if (env == null) { - Class c = Class.forName( + Class c = Class.forName( "com.sun.star.lib.uno.environments." + name + "." + name + "_environment"); - Constructor ctor = c.getConstructor( + Constructor ctor = c.getConstructor( new Class[] { Object.class }); env = (IEnvironment) ctor.newInstance(new Object[] { context }); environments.put(name + context, env); @@ -453,9 +453,9 @@ public class UnoRuntime { String name = from.getName() + "_" + to.getName(); String hashName = from.getName() + from.getContext() + "_" + to.getName() + to.getContext(); - IBridge bridge = (IBridge) WeakMap.getValue(bridges.get(hashName)); + IBridge bridge = WeakMap.getValue(bridges.get(hashName)); if(bridge == null) { - Class zClass = null; + Class zClass = null; String className = name + "_bridge"; try { zClass = Class.forName(className); @@ -464,9 +464,9 @@ public class UnoRuntime { + className; zClass = Class.forName(className); } - Class[] signature = { + Class[] signature = { IEnvironment.class, IEnvironment.class, args.getClass() }; - Constructor constructor = zClass.getConstructor(signature); + Constructor constructor = zClass.getConstructor(signature); Object[] iargs = { from, to, args }; bridge = (IBridge) constructor.newInstance(iargs); bridges.put(hashName, bridge); @@ -521,16 +521,16 @@ public class UnoRuntime { * offering a replacement. */ public static IBridge[] getBridges() { - ArrayList l = new ArrayList(); + ArrayList l = new ArrayList(); synchronized (bridges) { - for (Iterator i = bridges.values().iterator(); i.hasNext();) { - Object o = WeakMap.getValue(i.next()); + for (Iterator> i = bridges.values().iterator(); i.hasNext();) { + IBridge o = WeakMap.getValue(i.next()); if (o != null) { l.add(o); } } } - return (IBridge[]) l.toArray(new IBridge[l.size()]); + return l.toArray(new IBridge[l.size()]); } /** @@ -596,8 +596,8 @@ public class UnoRuntime { */ static public boolean reset() { synchronized (bridges) { - for (Iterator i = bridges.values().iterator(); i.hasNext();) { - IBridge b = (IBridge) WeakMap.getValue(i.next()); + for (Iterator> i = bridges.values().iterator(); i.hasNext();) { + IBridge b = WeakMap.getValue(i.next()); if (b != null) { // The following call to dispose was originally made to // com.sun.star.lib.sandbox.Disposable.dispose, which cannot @@ -681,8 +681,8 @@ public class UnoRuntime { private static final String oidSuffix = ";java[];" + getUniqueKey(); - private static final ThreadLocal currentContext = new ThreadLocal(); + private static final ThreadLocal currentContext = new ThreadLocal(); - private static final WeakMap environments = new WeakMap(); - private static final WeakMap bridges = new WeakMap(); + private static final WeakMap environments = new WeakMap(); + private static final WeakMap bridges = new WeakMap(); } diff --git a/ridljar/source/unoloader/com/sun/star/lib/unoloader/UnoLoader.java b/ridljar/source/unoloader/com/sun/star/lib/unoloader/UnoLoader.java index 1f443e038035..76f392cefd13 100644 --- a/ridljar/source/unoloader/com/sun/star/lib/unoloader/UnoLoader.java +++ b/ridljar/source/unoloader/com/sun/star/lib/unoloader/UnoLoader.java @@ -69,7 +69,7 @@ public final class UnoLoader { UnoClassLoader cl; try { cl = (UnoClassLoader) AccessController.doPrivileged( - new PrivilegedExceptionAction() { + new PrivilegedExceptionAction() { public Object run() throws MalformedURLException { return new UnoClassLoader( base, null, UnoLoader.class.getClassLoader()); diff --git a/ridljar/test/com/sun/star/lib/util/WeakMap_Test.java b/ridljar/test/com/sun/star/lib/util/WeakMap_Test.java index 00e03a6c22ff..378a1ac78777 100644 --- a/ridljar/test/com/sun/star/lib/util/WeakMap_Test.java +++ b/ridljar/test/com/sun/star/lib/util/WeakMap_Test.java @@ -24,7 +24,7 @@ import static org.junit.Assert.*; public final class WeakMap_Test { @Test public void test() { - WeakMap m = new WeakMap(); + WeakMap m = new WeakMap(); assertEquals(0, m.size()); assertTrue(m.isEmpty()); assertFalse(m.containsKey("key1")); diff --git a/ridljar/test/com/sun/star/uno/UnoRuntime_Test.java b/ridljar/test/com/sun/star/uno/UnoRuntime_Test.java index 3bc375af0a96..cfc773f5296f 100644 --- a/ridljar/test/com/sun/star/uno/UnoRuntime_Test.java +++ b/ridljar/test/com/sun/star/uno/UnoRuntime_Test.java @@ -71,10 +71,10 @@ public final class UnoRuntime_Test { UnoRuntime.areSame( new Any( new Type("com.sun.star.beans.Optional"), - new Optional()), + new Optional()), new Any( new Type("com.sun.star.beans.Optional"), - new Optional(false, new Integer(0))))); + new Optional(false, new Integer(0))))); assertFalse(UnoRuntime.areSame(new Test1(), new Test2())); Test2 test2 = new Test2(); assertTrue( @@ -83,6 +83,7 @@ public final class UnoRuntime_Test { UnoRuntime.queryInterface(Ifc.class, test2), test2)); } + @SuppressWarnings("rawtypes") @Test public void test_completeValue() { assertEquals( new Integer(0), UnoRuntime.completeValue(Type.UNSIGNED_LONG, null)); -- cgit