diff options
author | Wesley Wolfe <weswolf@aol.com> | 2012-02-29 08:32:25 -0600 |
---|---|---|
committer | EvilSeph <evilseph@gmail.com> | 2012-02-29 10:23:16 -0500 |
commit | ca7aab6f4ffab1a025e7385cafef45b664346d6c (patch) | |
tree | 1801be1c63a89ce92bd49331b8134c737bff13f9 /src/main | |
parent | eb3c1611807e4461a5fc1a2880f1533dbdbe7167 (diff) | |
download | bukkit-ca7aab6f4ffab1a025e7385cafef45b664346d6c.tar bukkit-ca7aab6f4ffab1a025e7385cafef45b664346d6c.tar.gz bukkit-ca7aab6f4ffab1a025e7385cafef45b664346d6c.tar.lz bukkit-ca7aab6f4ffab1a025e7385cafef45b664346d6c.tar.xz bukkit-ca7aab6f4ffab1a025e7385cafef45b664346d6c.zip |
[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.
Diffstat (limited to 'src/main')
13 files changed, 165 insertions, 174 deletions
diff --git a/src/main/java/org/bukkit/command/PluginCommandYamlParser.java b/src/main/java/org/bukkit/command/PluginCommandYamlParser.java index e7feea49..2e6fedf1 100644 --- a/src/main/java/org/bukkit/command/PluginCommandYamlParser.java +++ b/src/main/java/org/bukkit/command/PluginCommandYamlParser.java @@ -9,58 +9,54 @@ import org.bukkit.plugin.Plugin; public class PluginCommandYamlParser { - @SuppressWarnings("unchecked") public static List<Command> parse(Plugin plugin) { List<Command> pluginCmds = new ArrayList<Command>(); - Object object = plugin.getDescription().getCommands(); - if (object == null) { + Map<String, Map<String, Object>> map = plugin.getDescription().getCommands(); + + if (map == null) { return pluginCmds; } - Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) object; - - if (map != null) { - for (Entry<String, Map<String, Object>> entry : map.entrySet()) { - Command newCmd = new PluginCommand(entry.getKey(), plugin); - Object description = entry.getValue().get("description"); - Object usage = entry.getValue().get("usage"); - Object aliases = entry.getValue().get("aliases"); - Object permission = entry.getValue().get("permission"); - Object permissionMessage = entry.getValue().get("permission-message"); + for (Entry<String, Map<String, Object>> entry : map.entrySet()) { + Command newCmd = new PluginCommand(entry.getKey(), plugin); + Object description = entry.getValue().get("description"); + Object usage = entry.getValue().get("usage"); + Object aliases = entry.getValue().get("aliases"); + Object permission = entry.getValue().get("permission"); + Object permissionMessage = entry.getValue().get("permission-message"); - if (description != null) { - newCmd.setDescription(description.toString()); - } + if (description != null) { + newCmd.setDescription(description.toString()); + } - if (usage != null) { - newCmd.setUsage(usage.toString()); - } + if (usage != null) { + newCmd.setUsage(usage.toString()); + } - if (aliases != null) { - List<String> aliasList = new ArrayList<String>(); + if (aliases != null) { + List<String> aliasList = new ArrayList<String>(); - if (aliases instanceof List) { - for (Object o : (List<Object>) aliases) { - aliasList.add(o.toString()); - } - } else { - aliasList.add(aliases.toString()); + if (aliases instanceof List) { + for (Object o : (List<?>) aliases) { + aliasList.add(o.toString()); } - - newCmd.setAliases(aliasList); + } else { + aliasList.add(aliases.toString()); } - if (permission != null) { - newCmd.setPermission(permission.toString()); - } + newCmd.setAliases(aliasList); + } - if (permissionMessage != null) { - newCmd.setPermissionMessage(permissionMessage.toString()); - } + if (permission != null) { + newCmd.setPermission(permission.toString()); + } - pluginCmds.add(newCmd); + if (permissionMessage != null) { + newCmd.setPermissionMessage(permissionMessage.toString()); } + + pluginCmds.add(newCmd); } return pluginCmds; } diff --git a/src/main/java/org/bukkit/command/defaults/VersionCommand.java b/src/main/java/org/bukkit/command/defaults/VersionCommand.java index 3a582359..0bd5c5af 100644 --- a/src/main/java/org/bukkit/command/defaults/VersionCommand.java +++ b/src/main/java/org/bukkit/command/defaults/VersionCommand.java @@ -1,7 +1,8 @@ package org.bukkit.command.defaults; -import java.util.ArrayList; import java.util.Arrays; +import java.util.List; + import org.bukkit.ChatColor; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -67,7 +68,7 @@ public class VersionCommand extends Command { private String getAuthors(final PluginDescriptionFile desc) { StringBuilder result = new StringBuilder(); - ArrayList<String> authors = desc.getAuthors(); + List<String> authors = desc.getAuthors(); for (int i = 0; i < authors.size(); i++) { if (result.length() > 0) { diff --git a/src/main/java/org/bukkit/configuration/ConfigurationSection.java b/src/main/java/org/bukkit/configuration/ConfigurationSection.java index 7083ccc2..90d43849 100644 --- a/src/main/java/org/bukkit/configuration/ConfigurationSection.java +++ b/src/main/java/org/bukkit/configuration/ConfigurationSection.java @@ -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 /** diff --git a/src/main/java/org/bukkit/configuration/MemorySection.java b/src/main/java/org/bukkit/configuration/MemorySection.java index 6d6c3f9d..549d4a13 100644 --- a/src/main/java/org/bukkit/configuration/MemorySection.java +++ b/src/main/java/org/bukkit/configuration/MemorySection.java @@ -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); } } diff --git a/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java b/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java index 80842a70..99bfa1f8 100644 --- a/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java +++ b/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java @@ -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); } diff --git a/src/main/java/org/bukkit/configuration/file/YamlConstructor.java b/src/main/java/org/bukkit/configuration/file/YamlConstructor.java index 7df59fb2..73ad722a 100644 --- a/src/main/java/org/bukkit/configuration/file/YamlConstructor.java +++ b/src/main/java/org/bukkit/configuration/file/YamlConstructor.java @@ -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()); } diff --git a/src/main/java/org/bukkit/event/HandlerList.java b/src/main/java/org/bukkit/event/HandlerList.java index f25b26fb..f48d942f 100644 --- a/src/main/java/org/bukkit/event/HandlerList.java +++ b/src/main/java/org/bukkit/event/HandlerList.java @@ -9,7 +9,6 @@ import java.util.Map.Entry; /** * A list of event handlers, stored per-event. Based on lahwran's fevents. */ -@SuppressWarnings("unchecked") public class HandlerList { /** * Handler array. This field being an array is the key to this system's speed. @@ -203,6 +202,7 @@ public class HandlerList { * * @return the list of all handler lists */ + @SuppressWarnings("unchecked") public static ArrayList<HandlerList> getHandlerLists() { return (ArrayList<HandlerList>) allLists.clone(); } diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java index 9118194b..b814fbec 100644 --- a/src/main/java/org/bukkit/inventory/ItemStack.java +++ b/src/main/java/org/bukkit/inventory/ItemStack.java @@ -391,10 +391,9 @@ public class ItemStack implements Cloneable, ConfigurationSerializable { Object raw = args.get("enchantments"); if (raw instanceof Map) { - @SuppressWarnings("unchecked") - Map<Object, Object> map = (Map<Object, Object>) raw; + Map<?, ?> map = (Map<?, ?>) raw; - for (Map.Entry<Object, Object> entry : map.entrySet()) { + for (Map.Entry<?, ?> entry : map.entrySet()) { Enchantment enchantment = Enchantment.getByName(entry.getKey().toString()); if ((enchantment != null) && (entry.getValue() instanceof Integer)) { diff --git a/src/main/java/org/bukkit/permissions/Permission.java b/src/main/java/org/bukkit/permissions/Permission.java index bfac37ab..abac6a9a 100644 --- a/src/main/java/org/bukkit/permissions/Permission.java +++ b/src/main/java/org/bukkit/permissions/Permission.java @@ -6,6 +6,8 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Level; + +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.plugin.PluginManager; @@ -204,12 +206,12 @@ public class Permission { * @param def Default permission value to use if missing * @return Permission object */ - public static List<Permission> loadPermissions(Map<String, Map<String, Object>> data, String error, PermissionDefault def) { + public static List<Permission> loadPermissions(Map<?, ?> data, String error, PermissionDefault def) { List<Permission> result = new ArrayList<Permission>(); - for (Map.Entry<String, Map<String, Object>> entry : data.entrySet()) { + for (Map.Entry<?, ?> entry : data.entrySet()) { try { - result.add(Permission.loadPermission(entry.getKey(), entry.getValue(), def, result)); + result.add(Permission.loadPermission(entry.getKey().toString(), (Map<?, ?>) entry.getValue(), def, result)); } catch (Throwable ex) { Bukkit.getServer().getLogger().log(Level.SEVERE, String.format(error, entry.getKey()), ex); } @@ -248,13 +250,10 @@ public class Permission { * @param output A list to append any created child-Permissions to, may be null * @return Permission object */ - public static Permission loadPermission(String name, Map<String, Object> data, PermissionDefault def, List<Permission> output) { - if (name == null) { - throw new IllegalArgumentException("Name cannot be null"); - } - if (data == null) { - throw new IllegalArgumentException("Data cannot be null"); - } + public static Permission loadPermission(String name, Map<?, ?> data, PermissionDefault def, List<Permission> output) { + Validate.notNull(name, "Name cannot be null"); + Validate.notNull(data, "Data cannot be null"); + String desc = null; Map<String, Boolean> children = null; @@ -280,11 +279,7 @@ public class Permission { } if (data.containsKey("description")) { - try { - desc = (String) data.get("description"); - } catch (ClassCastException ex) { - throw new IllegalArgumentException("'description' key is of wrong type", ex); - } + desc = data.get("description").toString(); } Permission result = new Permission(name, desc, def, children); @@ -304,31 +299,30 @@ public class Permission { return result; } - @SuppressWarnings("unchecked") - private static Map<String, Boolean> extractChildren(Map<String, Object> data, String name, PermissionDefault def, List<Permission> output) { - Map<String, Object> input = (Map<String, Object>) data.get("children"); + private static Map<String, Boolean> extractChildren(Map<?, ?> data, String name, PermissionDefault def, List<Permission> output) { + Map<?, ?> input = (Map<?, ?>) data.get("children"); Map<String, Boolean> children = new LinkedHashMap<String, Boolean>(); - for (Map.Entry<String, Object> entry : input.entrySet()) { + for (Map.Entry<?, ?> entry : input.entrySet()) { if ((entry.getValue() instanceof Boolean)) { - children.put(entry.getKey(), (Boolean) entry.getValue()); + children.put(entry.getKey().toString(), (Boolean) entry.getValue()); } else if ((entry.getValue() instanceof Map)) { try { try { - Permission perm = loadPermission((String) entry.getKey(), (Map<String, Object>) entry.getValue(), def, output); + Permission perm = loadPermission(entry.getKey().toString(), (Map<?, ?>) entry.getValue(), def, output); children.put(perm.getName(), Boolean.valueOf(true)); if (output != null) { output.add(perm); } } catch (Throwable ex) { - Bukkit.getServer().getLogger().log(Level.SEVERE, "Permission node '" + (String) entry.getKey() + "' in child of " + name + " is invalid", ex); + Bukkit.getServer().getLogger().log(Level.SEVERE, "Permission node '" + entry.getKey().toString() + "' in child of " + name + " is invalid", ex); } } catch (ClassCastException ex) { - throw new IllegalArgumentException("Child '" + (String) entry.getKey() + "' contains invalid map type"); + throw new IllegalArgumentException("Child '" + entry.getKey().toString() + "' contains invalid map type"); } } else { - throw new IllegalArgumentException("Child '" + (String) entry.getKey() + "' contains invalid value"); + throw new IllegalArgumentException("Child '" + entry.getKey().toString() + "' contains invalid value"); } } diff --git a/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java b/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java index 69f40968..34095d52 100644 --- a/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java +++ b/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java @@ -3,15 +3,18 @@ package org.bukkit.plugin; import java.io.InputStream; import java.io.Reader; import java.io.Writer; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; + import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + /** * Provides access to a Plugins description file, plugin.yaml */ @@ -20,21 +23,20 @@ public final class PluginDescriptionFile { private String name = null; private String main = null; private String classLoaderOf = null; - private ArrayList<String> depend = null; - private ArrayList<String> softDepend = null; + private List<String> depend = null; + private List<String> softDepend = null; private String version = null; - private Object commands = null; + private Map<String, Map<String, Object>> commands = null; private String description = null; - private ArrayList<String> authors = new ArrayList<String>(); + private List<String> authors = null; private String website = null; private boolean database = false; private PluginLoadOrder order = PluginLoadOrder.POSTWORLD; - private List<Permission> permissions = new ArrayList<Permission>(); + private List<Permission> permissions = null; private PermissionDefault defaultPerm = PermissionDefault.OP; - @SuppressWarnings("unchecked") public PluginDescriptionFile(final InputStream stream) throws InvalidDescriptionException { - loadMap((Map<String, Object>) yaml.load(stream)); + loadMap((Map<?, ?>) yaml.load(stream)); } /** @@ -43,9 +45,8 @@ public final class PluginDescriptionFile { * @param reader The reader * @throws InvalidDescriptionException If the PluginDescriptionFile is invalid */ - @SuppressWarnings("unchecked") public PluginDescriptionFile(final Reader reader) throws InvalidDescriptionException { - loadMap((Map<String, Object>) yaml.load(reader)); + loadMap((Map<?, ?>) yaml.load(reader)); } /** @@ -106,15 +107,15 @@ public final class PluginDescriptionFile { return main; } - public Object getCommands() { + public Map<String, Map<String, Object>> getCommands() { return commands; } - public Object getDepend() { + public List<String> getDepend() { return depend; } - public Object getSoftDepend() { + public List<String> getSoftDepend() { return softDepend; } @@ -131,7 +132,7 @@ public final class PluginDescriptionFile { return description; } - public ArrayList<String> getAuthors() { + public List<String> getAuthors() { return authors; } @@ -159,8 +160,7 @@ public final class PluginDescriptionFile { return classLoaderOf; } - @SuppressWarnings("unchecked") - private void loadMap(Map<String, Object> map) throws InvalidDescriptionException { + private void loadMap(Map<?, ?> map) throws InvalidDescriptionException { try { name = map.get("name").toString(); @@ -193,11 +193,28 @@ public final class PluginDescriptionFile { } if (map.containsKey("commands")) { + ImmutableMap.Builder<String, Map<String, Object>> commandsBuilder = ImmutableMap.<String, Map<String, Object>>builder(); try { - commands = map.get("commands"); + for (Map.Entry<?, ?> command : ((Map<?, ?>) map.get("commands")).entrySet()) { + ImmutableMap.Builder<String, Object> commandBuilder = ImmutableMap.<String, Object>builder(); + for (Map.Entry<?, ?> commandEntry : ((Map<?, ?>) command.getValue()).entrySet()) { + if (commandEntry.getValue() instanceof Iterable) { + // This prevents internal alias list changes + ImmutableList.Builder<Object> commandSubList = ImmutableList.<Object>builder(); + for (Object commandSubListItem : (Iterable<?>) commandEntry.getValue()) { + commandSubList.add(commandSubListItem); + } + commandBuilder.put(commandEntry.getKey().toString(), commandSubList.build()); + } else { + commandBuilder.put(commandEntry.getKey().toString(), commandEntry.getValue()); + } + } + commandsBuilder.put(command.getKey().toString(), commandBuilder.build()); + } } catch (ClassCastException ex) { throw new InvalidDescriptionException(ex, "commands are of wrong type"); } + commands = commandsBuilder.build(); } if (map.containsKey("class-loader-of")) { @@ -205,19 +222,27 @@ public final class PluginDescriptionFile { } if (map.containsKey("depend")) { + ImmutableList.Builder<String> dependBuilder = ImmutableList.<String>builder(); try { - depend = (ArrayList<String>) map.get("depend"); + for (Object dependency : (Iterable<?>) map.get("depend")) { + dependBuilder.add(dependency.toString()); + } } catch (ClassCastException ex) { throw new InvalidDescriptionException(ex, "depend is of wrong type"); } + depend = dependBuilder.build(); } if (map.containsKey("softdepend")) { + ImmutableList.Builder<String> softDependBuilder = ImmutableList.<String>builder(); try { - softDepend = (ArrayList<String>) map.get("softdepend"); + for (Object dependency : (Iterable<?>) map.get("softdepend")) { + softDependBuilder.add(dependency.toString()); + } } catch (ClassCastException ex) { throw new InvalidDescriptionException(ex, "softdepend is of wrong type"); } + softDepend = softDependBuilder.build(); } if (map.containsKey("database")) { @@ -229,19 +254,11 @@ public final class PluginDescriptionFile { } if (map.containsKey("website")) { - try { - website = (String) map.get("website"); - } catch (ClassCastException ex) { - throw new InvalidDescriptionException(ex, "website is of wrong type"); - } + website = map.get("website").toString(); } if (map.containsKey("description")) { - try { - description = (String) map.get("description"); - } catch (ClassCastException ex) { - throw new InvalidDescriptionException(ex, "description is of wrong type"); - } + description = map.get("description").toString(); } if (map.containsKey("load")) { @@ -254,29 +271,28 @@ public final class PluginDescriptionFile { } } - if (map.containsKey("author")) { - try { - String extra = (String) map.get("author"); - - authors.add(extra); - } catch (ClassCastException ex) { - throw new InvalidDescriptionException(ex, "author is of wrong type"); - } - } - if (map.containsKey("authors")) { + ImmutableList.Builder<String> authorsBuilder = ImmutableList.<String>builder(); + if (map.containsKey("author")) { + authorsBuilder.add(map.get("author").toString()); + } try { - ArrayList<String> extra = (ArrayList<String>) map.get("authors"); - - authors.addAll(extra); + for (Object o : (Iterable<?>) map.get("authors")) { + authorsBuilder.add(o.toString()); + } } catch (ClassCastException ex) { throw new InvalidDescriptionException(ex, "authors are of wrong type"); } + authors = authorsBuilder.build(); + } else if (map.containsKey("author")) { + authors = ImmutableList.of(map.get("author").toString()); + } else { + authors = ImmutableList.<String>of(); } if (map.containsKey("default-permission")) { try { - defaultPerm = PermissionDefault.getByName((String) map.get("default-permission")); + defaultPerm = PermissionDefault.getByName(map.get("default-permission").toString()); } catch (ClassCastException ex) { throw new InvalidDescriptionException(ex, "default-permission is of wrong type"); } catch (IllegalArgumentException ex) { @@ -286,12 +302,14 @@ public final class PluginDescriptionFile { if (map.containsKey("permissions")) { try { - Map<String, Map<String, Object>> perms = (Map<String, Map<String, Object>>) map.get("permissions"); + Map<?, ?> perms = (Map<?, ?>) map.get("permissions"); - permissions = Permission.loadPermissions(perms, "Permission node '%s' in plugin description file for " + getFullName() + " is invalid", defaultPerm); + permissions = ImmutableList.copyOf(Permission.loadPermissions(perms, "Permission node '%s' in plugin description file for " + getFullName() + " is invalid", defaultPerm)); } catch (ClassCastException ex) { throw new InvalidDescriptionException(ex, "permissions are of wrong type"); } + } else { + permissions = ImmutableList.<Permission>of(); } } diff --git a/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/src/main/java/org/bukkit/plugin/SimplePluginManager.java index 1febcb8c..f23a59e2 100644 --- a/src/main/java/org/bukkit/plugin/SimplePluginManager.java +++ b/src/main/java/org/bukkit/plugin/SimplePluginManager.java @@ -99,7 +99,6 @@ public final class SimplePluginManager implements PluginManager { * @param directory Directory to check for plugins * @return A list of all plugins loaded */ - @SuppressWarnings("unchecked") public Plugin[] loadPlugins(File directory) { Validate.notNull(directory, "Directory cannot be null"); Validate.isTrue(directory.isDirectory(), "Directory must be a directory"); @@ -138,12 +137,12 @@ public final class SimplePluginManager implements PluginManager { plugins.put(description.getName(), file); - Collection<? extends String> softDependencySet = (Collection<? extends String>) description.getSoftDepend(); + Collection<String> softDependencySet = description.getSoftDepend(); if (softDependencySet != null) { softDependencies.put(description.getName(), new LinkedList<String>(softDependencySet)); } - Collection<? extends String> dependencySet = (Collection<? extends String>) description.getDepend(); + Collection<String> dependencySet = description.getDepend(); if (dependencySet != null) { dependencies.put(description.getName(), new LinkedList<String>(dependencySet)); } diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java index 2766330d..cb0055b4 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java @@ -8,9 +8,9 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.jar.JarEntry; @@ -40,6 +40,8 @@ import org.bukkit.plugin.TimedRegisteredListener; import org.bukkit.plugin.UnknownDependencyException; import org.yaml.snakeyaml.error.YAMLException; +import com.google.common.collect.ImmutableList; + /** * Represents a Java plugin loader, allowing plugins in the form of .jar */ @@ -53,7 +55,6 @@ public class JavaPluginLoader implements PluginLoader { server = instance; } - @SuppressWarnings("unchecked") public Plugin loadPlugin(File file) throws InvalidPluginException { Validate.notNull(file, "File cannot be null"); @@ -104,15 +105,9 @@ public class JavaPluginLoader implements PluginLoader { )); } - ArrayList<String> depend; - - try { - depend = (ArrayList<String>) description.getDepend(); - if (depend == null) { - depend = new ArrayList<String>(); - } - } catch (ClassCastException ex) { - throw new InvalidPluginException(ex); + List<String> depend = description.getDepend(); + if (depend == null) { + depend = ImmutableList.<String>of(); } for (String pluginName : depend) { @@ -251,8 +246,7 @@ public class JavaPluginLoader implements PluginLoader { classes.put(name, clazz); if (ConfigurationSerializable.class.isAssignableFrom(clazz)) { - @SuppressWarnings("unchecked") - Class<? extends ConfigurationSerializable> serializable = (Class<? extends ConfigurationSerializable>) clazz; + Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class); ConfigurationSerialization.registerClass(serializable); } } @@ -263,8 +257,7 @@ public class JavaPluginLoader implements PluginLoader { try { if ((clazz != null) && (ConfigurationSerializable.class.isAssignableFrom(clazz))) { - @SuppressWarnings("unchecked") - Class<? extends ConfigurationSerializable> serializable = (Class<? extends ConfigurationSerializable>) clazz; + Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class); ConfigurationSerialization.unregisterClass(serializable); } } catch (NullPointerException ex) { diff --git a/src/main/java/org/bukkit/potion/Potion.java b/src/main/java/org/bukkit/potion/Potion.java index a66d1f91..e9c8e041 100644 --- a/src/main/java/org/bukkit/potion/Potion.java +++ b/src/main/java/org/bukkit/potion/Potion.java @@ -1,13 +1,14 @@ package org.bukkit.potion; import java.util.Collection; -import java.util.Collections; import org.apache.commons.lang.Validate; import org.bukkit.Material; import org.bukkit.entity.LivingEntity; import org.bukkit.inventory.ItemStack; +import com.google.common.collect.ImmutableList; + /** * Represents a minecraft potion */ @@ -170,9 +171,8 @@ public class Potion { * @see Potion#toDamageValue() * @return The effects that this potion applies */ - @SuppressWarnings("unchecked") public Collection<PotionEffect> getEffects() { - if(type == null) return Collections.EMPTY_SET; + if (type == null) return ImmutableList.<PotionEffect>of(); return getBrewer().getEffectsFromDamage(toDamageValue()); } |