diff options
author | mbax <github@phozop.net> | 2012-07-16 15:05:51 -0400 |
---|---|---|
committer | Wesley Wolfe <weswolf@aol.com> | 2012-08-26 22:09:21 -0500 |
commit | bc6b976dac34838f0f31d1286e65442690fd6acf (patch) | |
tree | fcbaaf776c027e010ce518f16f728bfefb643149 /src/main | |
parent | 555c40e387cd3b5496e3b8581a68db4d1e10515d (diff) | |
download | bukkit-bc6b976dac34838f0f31d1286e65442690fd6acf.tar bukkit-bc6b976dac34838f0f31d1286e65442690fd6acf.tar.gz bukkit-bc6b976dac34838f0f31d1286e65442690fd6acf.tar.lz bukkit-bc6b976dac34838f0f31d1286e65442690fd6acf.tar.xz bukkit-bc6b976dac34838f0f31d1286e65442690fd6acf.zip |
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.
Diffstat (limited to 'src/main')
4 files changed, 14 insertions, 5 deletions
diff --git a/src/main/java/org/bukkit/metadata/MetadataStore.java b/src/main/java/org/bukkit/metadata/MetadataStore.java index 0adfa7a0..273a273f 100644 --- a/src/main/java/org/bukkit/metadata/MetadataStore.java +++ b/src/main/java/org/bukkit/metadata/MetadataStore.java @@ -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); } diff --git a/src/main/java/org/bukkit/metadata/MetadataStoreBase.java b/src/main/java/org/bukkit/metadata/MetadataStoreBase.java index 9bbe3647..7febbd43 100644 --- a/src/main/java/org/bukkit/metadata/MetadataStoreBase.java +++ b/src/main/java/org/bukkit/metadata/MetadataStoreBase.java @@ -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)) { diff --git a/src/main/java/org/bukkit/metadata/MetadataValue.java b/src/main/java/org/bukkit/metadata/MetadataValue.java index 761d1ac9..deec40fb 100644 --- a/src/main/java/org/bukkit/metadata/MetadataValue.java +++ b/src/main/java/org/bukkit/metadata/MetadataValue.java @@ -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(); diff --git a/src/main/java/org/bukkit/metadata/Metadatable.java b/src/main/java/org/bukkit/metadata/Metadatable.java index 956a1960..41711be8 100644 --- a/src/main/java/org/bukkit/metadata/Metadatable.java +++ b/src/main/java/org/bukkit/metadata/Metadatable.java @@ -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); } |