Updated null checks in MetadataStoreBase. Fixes BUKKIT-1412

Previously, the method could be called with a null MetadataStore and stored.
In later execution null pointer exceptions would be generated when checking
for the plugin that the set Metadata belongs to.

Additionally, places where a plugin is referenced will now throw an
IllegalArgumentException if specified plugin is null. Using null would be an
obvious logical flaw, and in some cases produce additional exceptions later
in execution.

By: mbax <github@phozop.net>
This commit is contained in:
Bukkit/Spigot 2012-07-16 15:05:51 -04:00
parent 29a5295348
commit 977cc8a31c
4 changed files with 14 additions and 5 deletions

View File

@ -11,6 +11,7 @@ public interface MetadataStore<T> {
* @param subject The object receiving the metadata.
* @param metadataKey A unique key to identify this metadata.
* @param newMetadataValue The metadata value to apply.
* @throws IllegalArgumentException If value is null, or the owning plugin is null
*/
public void setMetadata(T subject, String metadataKey, MetadataValue newMetadataValue);
@ -39,6 +40,7 @@ public interface MetadataStore<T> {
* @param subject the object to remove the metadata from.
* @param metadataKey the unique metadata key identifying the metadata to remove.
* @param owningPlugin the plugin attempting to remove a metadata item.
* @throws IllegalArgumentException If plugin is null
*/
public void removeMetadata(T subject, String metadataKey, Plugin owningPlugin);
@ -47,6 +49,7 @@ public interface MetadataStore<T> {
* each invalidated metadata item to be recalculated the next time it is accessed.
*
* @param owningPlugin the plugin requesting the invalidation.
* @throws IllegalArgumentException If plugin is null
*/
public void invalidateAll(Plugin owningPlugin);
}

View File

@ -1,5 +1,6 @@
package org.bukkit.metadata;
import org.apache.commons.lang.Validate;
import org.bukkit.plugin.Plugin;
import java.util.*;
@ -22,8 +23,11 @@ public abstract class MetadataStoreBase<T> {
* @param metadataKey A unique key to identify this metadata.
* @param newMetadataValue The metadata value to apply.
* @see MetadataStore#setMetadata(Object, String, MetadataValue)
* @throws IllegalArgumentException If value is null, or the owning plugin is null
*/
public synchronized void setMetadata(T subject, String metadataKey, MetadataValue newMetadataValue) {
Validate.notNull(newMetadataValue, "Value cannot be null");
Validate.notNull(newMetadataValue.getOwningPlugin(), "Plugin cannot be null");
String key = cachedDisambiguate(subject, metadataKey);
if (!metadataMap.containsKey(key)) {
metadataMap.put(key, new ArrayList<MetadataValue>());
@ -78,8 +82,10 @@ public abstract class MetadataStoreBase<T> {
* @param metadataKey the unique metadata key identifying the metadata to remove.
* @param owningPlugin the plugin attempting to remove a metadata item.
* @see MetadataStore#removeMetadata(Object, String, org.bukkit.plugin.Plugin)
* @throws IllegalArgumentException If plugin is null
*/
public synchronized void removeMetadata(T subject, String metadataKey, Plugin owningPlugin) {
Validate.notNull(owningPlugin, "Plugin cannot be null");
String key = cachedDisambiguate(subject, metadataKey);
List<MetadataValue> metadataList = metadataMap.get(key);
if (metadataList == null) return;
@ -99,12 +105,10 @@ public abstract class MetadataStoreBase<T> {
*
* @param owningPlugin the plugin requesting the invalidation.
* @see MetadataStore#invalidateAll(org.bukkit.plugin.Plugin)
* @throws IllegalArgumentException If plugin is null
*/
public synchronized void invalidateAll(Plugin owningPlugin) {
if (owningPlugin == null) {
throw new IllegalArgumentException("owningPlugin cannot be null");
}
Validate.notNull(owningPlugin, "Plugin cannot be null");
for (List<MetadataValue> values : metadataMap.values()) {
for (MetadataValue value : values) {
if (value.getOwningPlugin().equals(owningPlugin)) {

View File

@ -62,7 +62,7 @@ public interface MetadataValue {
/**
* Returns the {@link Plugin} that created this metadata item.
*
* @return the plugin that owns this metadata value.
* @return the plugin that owns this metadata value. This should never be null.
*/
public Plugin getOwningPlugin();

View File

@ -13,6 +13,7 @@ public interface Metadatable {
*
* @param metadataKey A unique key to identify this metadata.
* @param newMetadataValue The metadata value to apply.
* @throws IllegalArgumentException If value is null, or the owning plugin is null
*/
public void setMetadata(String metadataKey, MetadataValue newMetadataValue);
@ -37,6 +38,7 @@ public interface Metadatable {
*
* @param metadataKey the unique metadata key identifying the metadata to remove.
* @param owningPlugin This plugin's metadata value will be removed. All other values will be left untouched.
* @throws IllegalArgumentException If plugin is null
*/
public void removeMetadata(String metadataKey, Plugin owningPlugin);
}