[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

@@ -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();
}
}

View File

@@ -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));
}

View File

@@ -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) {