Kaydet (Commit) 4418ad03 authored tarafından Noel Grandin's avatar Noel Grandin Kaydeden (comit) Michael Stahl

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
üst 30d4c3c7
......@@ -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<MethodDescription> superList = new ArrayList<MethodDescription>();
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<MethodDescription> directList = new ArrayList<MethodDescription>();
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<TypeDescription> args = new java.util.ArrayList<TypeDescription>();
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<TypeDescription> list = new ArrayList<TypeDescription>();
}
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<TypeDescription> {
public Entry(TypeDescription desc, ReferenceQueue<TypeDescription> 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<String, Entry> map = new HashMap<String, Entry>();
private final ReferenceQueue<TypeDescription> queue = new ReferenceQueue<TypeDescription>();
}
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;
......
......@@ -58,7 +58,15 @@ import java.util.Set;
* <code>WeakReference</code> wrappers are automatically cleared as soon as the
* values are disposed.</p>
*/
public final class WeakMap implements Map {
public final class WeakMap<K,V> {
/**
* Declare the map as WeakReference instead of Entry because it makes the return
* type signatures of values() and keySet() cleaner.
*/
private final HashMap<K, WeakReference<V>> map = new HashMap<K, WeakReference<V>>();
private final ReferenceQueue<V> queue = new ReferenceQueue<V>();
/**
* Constructs an empty <code>WeakMap</code>.
*/
......@@ -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<K,V> m) {
putAll(m);
}
......@@ -108,7 +116,7 @@ public final class WeakMap implements Map {
* @return <code>true</code> 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 <code>true</code> if this map maps one or more keys to the
* specified value
*/
public boolean containsValue(Object value) {
public boolean containsValue(WeakReference<V> 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
* <code>null</code> if the map contains no mapping for this key
*/
public Object get(Object key) {
public WeakReference<V> 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
* <code>null</code> if there was no mapping for the key
*/
public Object put(Object key, Object value) {
public WeakReference<V> put(K key, V value) {
cleanUp();
return map.put(key, new Entry(key, value, queue));
return map.put(key, new Entry<K,V>(key, value, queue));
}
/**
......@@ -167,7 +175,7 @@ public final class WeakMap implements Map {
* @return previous value associated with the specified key, or
* <code>null</code> if there was no mapping for the key
*/
public Object remove(Object key) {
public WeakReference<V> 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
* <code>WeakReference</code>.
*/
public void putAll(Map t) {
public void putAll(Map<K,V> 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<Map.Entry<K,V>> i = m.entrySet().iterator(); i.hasNext();) {
Map.Entry<K,V> e = i.next();
K k = e.getKey();
map.put(k, new Entry<K,V>(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<K> 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<WeakReference<V>> 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<Map.Entry<K,WeakReference<V>>> entrySet() {
return map.entrySet();
}
......@@ -253,17 +261,20 @@ public final class WeakMap implements Map {
* @return the referent of the specified <code>WeakReference</code>, or
* <code>null</code> if <code>ref</code> is <code>null</code>
*/
public static Object getValue(Object ref) {
return ref == null ? null : ((WeakReference) ref).get();
public static <T> T getValue(WeakReference<T> 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<K,V> e = (Entry<K,V>) 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<K,V> extends WeakReference<V>
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<V> 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();
}
......@@ -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);
}
......
......@@ -175,5 +175,5 @@ public interface ITypeDescription {
* <p>
* @return the corresponding java class.
*/
Class getZClass();
Class<?> getZClass();
}
......@@ -78,7 +78,7 @@ public class Type {
TYPE_NAME_ANY
};
private static final HashMap __javaClassToTypeClass = new HashMap();
private static final HashMap<Class<?>, TypeClass[]> __javaClassToTypeClass = new HashMap<Class<?>, 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
* <code>null</code>.
*/
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 <code>null</code> in extreme situations
* (inconsistent <code>TypeClass</code>, 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;
}
......@@ -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<Object> l = new ArrayList<Object>();
synchronized (bridges) {
for (Iterator i = bridges.values().iterator(); i.hasNext();) {
Object o = WeakMap.getValue(i.next());
for (Iterator<java.lang.ref.WeakReference<IBridge>> 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<java.lang.ref.WeakReference<IBridge>> 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<XCurrentContext> currentContext = new ThreadLocal<XCurrentContext>();
private static final WeakMap environments = new WeakMap();
private static final WeakMap bridges = new WeakMap();
private static final WeakMap<String,IEnvironment> environments = new WeakMap<String,IEnvironment>();
private static final WeakMap<String,IBridge> bridges = new WeakMap<String,IBridge>();
}
......@@ -69,7 +69,7 @@ public final class UnoLoader {
UnoClassLoader cl;
try {
cl = (UnoClassLoader) AccessController.doPrivileged(
new PrivilegedExceptionAction() {
new PrivilegedExceptionAction<Object>() {
public Object run() throws MalformedURLException {
return new UnoClassLoader(
base, null, UnoLoader.class.getClassLoader());
......
......@@ -24,7 +24,7 @@ import static org.junit.Assert.*;
public final class WeakMap_Test {
@Test public void test() {
WeakMap m = new WeakMap();
WeakMap<String,Object> m = new WeakMap<String,Object>();
assertEquals(0, m.size());
assertTrue(m.isEmpty());
assertFalse(m.containsKey("key1"));
......
......@@ -71,10 +71,10 @@ public final class UnoRuntime_Test {
UnoRuntime.areSame(
new Any(
new Type("com.sun.star.beans.Optional<unsigned long>"),
new Optional()),
new Optional<Integer>()),
new Any(
new Type("com.sun.star.beans.Optional<unsigned long>"),
new Optional(false, new Integer(0)))));
new Optional<Integer>(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));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment