[Bleeding] Cleaned up unsafe casts. Addresses BUKKIT-844

Removed internal collection leaks from PluginDescriptionFile
BREAKING: PluginDescriptionFile.getAuthors() now returns List instead of
ArrayList

Various places with unsafe generics, notably List<Object> getList() in
Configurations are now referenced as <?>. This is nonbreaking, but
sourcecode will need to be revised when compiled.

By: Wesley Wolfe <weswolf@aol.com>
This commit is contained in:
Bukkit/Spigot
2012-02-29 08:32:25 -06:00
parent e7c9a43100
commit 5906da7948
14 changed files with 174 additions and 183 deletions

View File

@@ -174,7 +174,7 @@ public interface ConfigurationSection {
* @param path Path to create the section at.
* @return Newly created section
*/
public ConfigurationSection createSection(String path, Map<String, Object> map);
public ConfigurationSection createSection(String path, Map<?, ?> map);
// Primitives
/**
@@ -368,8 +368,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List.
*/
@SuppressWarnings("rawtypes")
public List getList(String path);
public List<?> getList(String path);
/**
* Gets the requested List by path, returning a default value if not found.
@@ -380,8 +379,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List.
*/
@SuppressWarnings("rawtypes")
public List getList(String path, List<?> def);
public List<?> getList(String path, List<?> def);
/**
* Checks if the specified path is a List.
@@ -544,7 +542,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List of Maps.
*/
public List<Map<String, Object>> getMapList(String path);
public List<Map<?, ?>> getMapList(String path);
// Bukkit
/**

View File

@@ -261,15 +261,14 @@ public class MemorySection implements ConfigurationSection {
}
}
@SuppressWarnings("unchecked")
public ConfigurationSection createSection(String path, Map<String, Object> map) {
public ConfigurationSection createSection(String path, Map<?, ?> map) {
ConfigurationSection section = createSection(path);
for (Map.Entry<String, Object> entry : map.entrySet()) {
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (entry.getValue() instanceof Map) {
section.createSection(entry.getKey(), (Map<String, Object>) entry.getValue());
section.createSection(entry.getKey().toString(), (Map<?, ?>) entry.getValue());
} else {
section.set(entry.getKey(), entry.getValue());
section.set(entry.getKey().toString(), entry.getValue());
}
}
@@ -413,24 +412,22 @@ public class MemorySection implements ConfigurationSection {
}
// Java
@SuppressWarnings("unchecked")
public List<Object> getList(String path) {
public List<?> getList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path);
return getList(path, (def instanceof List) ? (List<Object>) def : null);
return getList(path, (def instanceof List) ? (List<?>) def : null);
}
@SuppressWarnings("unchecked")
public List<Object> getList(String path, List<?> def) {
public List<?> getList(String path, List<?> def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def);
return (List<Object>) ((val instanceof List) ? val : def);
return (List<?>) ((val instanceof List) ? val : def);
}
public boolean isList(String path) {
@@ -447,7 +444,7 @@ public class MemorySection implements ConfigurationSection {
throw new IllegalArgumentException("Path cannot be null");
}
List<Object> list = getList(path);
List<?> list = getList(path);
if (list == null) {
return new ArrayList<String>(0);
@@ -469,7 +466,7 @@ public class MemorySection implements ConfigurationSection {
throw new IllegalArgumentException("Path cannot be null");
}
List<Object> list = getList(path);
List<?> list = getList(path);
if (list == null) {
return new ArrayList<Integer>(0);
@@ -510,7 +507,7 @@ public class MemorySection implements ConfigurationSection {
throw new IllegalArgumentException("Path cannot be null");
}
List<Object> list = getList(path);
List<?> list = getList(path);
if (list == null) {
return new ArrayList<Boolean>(0);
@@ -538,7 +535,7 @@ public class MemorySection implements ConfigurationSection {
throw new IllegalArgumentException("Path cannot be null");
}
List<Object> list = getList(path);
List<?> list = getList(path);
if (list == null) {
return new ArrayList<Double>(0);
@@ -579,7 +576,7 @@ public class MemorySection implements ConfigurationSection {
throw new IllegalArgumentException("Path cannot be null");
}
List<Object> list = getList(path);
List<?> list = getList(path);
if (list == null) {
return new ArrayList<Float>(0);
@@ -620,7 +617,7 @@ public class MemorySection implements ConfigurationSection {
throw new IllegalArgumentException("Path cannot be null");
}
List<Object> list = getList(path);
List<?> list = getList(path);
if (list == null) {
return new ArrayList<Long>(0);
@@ -661,7 +658,7 @@ public class MemorySection implements ConfigurationSection {
throw new IllegalArgumentException("Path cannot be null");
}
List<Object> list = getList(path);
List<?> list = getList(path);
if (list == null) {
return new ArrayList<Byte>(0);
@@ -702,7 +699,7 @@ public class MemorySection implements ConfigurationSection {
throw new IllegalArgumentException("Path cannot be null");
}
List<Object> list = getList(path);
List<?> list = getList(path);
if (list == null) {
return new ArrayList<Character>(0);
@@ -744,7 +741,7 @@ public class MemorySection implements ConfigurationSection {
throw new IllegalArgumentException("Path cannot be null");
}
List<Object> list = getList(path);
List<?> list = getList(path);
if (list == null) {
return new ArrayList<Short>(0);
@@ -780,18 +777,17 @@ public class MemorySection implements ConfigurationSection {
return result;
}
@SuppressWarnings("unchecked")
public List<Map<String, Object>> getMapList(String path) {
public List<Map<?, ?>> getMapList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<Object> list = getList(path);
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
List<?> list = getList(path);
List<Map<?, ?>> result = new ArrayList<Map<?, ?>>();
for (Object object : list) {
if (object instanceof Map) {
result.add((Map<String, Object>) object);
result.add((Map<?, ?>) object);
}
}

View File

@@ -42,16 +42,15 @@ public class YamlConfiguration extends FileConfiguration {
return header + dump;
}
@SuppressWarnings("unchecked")
@Override
public void loadFromString(String contents) throws InvalidConfigurationException {
if (contents == null) {
throw new IllegalArgumentException("Contents cannot be null");
}
Map<Object, Object> input;
Map<?, ?> input;
try {
input = (Map<Object, Object>) yaml.load(contents);
input = (Map<?, ?>) yaml.load(contents);
} catch (YAMLException e) {
throw new InvalidConfigurationException(e);
} catch (ClassCastException e) {
@@ -68,14 +67,13 @@ public class YamlConfiguration extends FileConfiguration {
}
}
@SuppressWarnings("unchecked")
protected void convertMapsToSections(Map<Object, Object> input, ConfigurationSection section) {
for (Map.Entry<Object, Object> entry : input.entrySet()) {
protected void convertMapsToSections(Map<?, ?> input, ConfigurationSection section) {
for (Map.Entry<?, ?> entry : input.entrySet()) {
String key = entry.getKey().toString();
Object value = entry.getValue();
if (value instanceof Map<?, ?>) {
convertMapsToSections((Map<Object, Object>) value, section.createSection(key));
if (value instanceof Map) {
convertMapsToSections((Map<?, ?>) value, section.createSection(key));
} else {
section.set(key, value);
}

View File

@@ -23,12 +23,11 @@ public class YamlConstructor extends SafeConstructor {
throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
}
@SuppressWarnings("unchecked")
Map<Object, Object> raw = (Map<Object, Object>) super.construct(node);
Map<?, ?> raw = (Map<?, ?>) super.construct(node);
if (raw.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
Map<String, Object> typed = new LinkedHashMap<String, Object>(raw.size());
for (Map.Entry<Object, Object> entry : raw.entrySet()) {
for (Map.Entry<?, ?> entry : raw.entrySet()) {
typed.put(entry.getKey().toString(), entry.getValue());
}