提交 0215fd9d 编写于 作者: K khazra

7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE

Summary: Insert null argument check in AbstractPreferences.remove()
Reviewed-by: dholmes, chegar, alanb
上级 a7aa8c5c
...@@ -305,8 +305,10 @@ public abstract class AbstractPreferences extends Preferences { ...@@ -305,8 +305,10 @@ public abstract class AbstractPreferences extends Preferences {
* @param key key whose mapping is to be removed from the preference node. * @param key key whose mapping is to be removed from the preference node.
* @throws IllegalStateException if this node (or an ancestor) has been * @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method. * removed with the {@link #removeNode()} method.
* @throws NullPointerException {@inheritDoc}.
*/ */
public void remove(String key) { public void remove(String key) {
Objects.requireNonNull(key, "Specified key cannot be null");
synchronized(lock) { synchronized(lock) {
if (removed) if (removed)
throw new IllegalStateException("Node has been removed."); throw new IllegalStateException("Node has been removed.");
......
...@@ -22,23 +22,77 @@ ...@@ -22,23 +22,77 @@
*/ */
/* @test /* @test
* @bug 7160242 * @bug 7160242 7165118
* @summary Check if NullPointerException is thrown if the key passed * @summary Check if NullPointerException is thrown if the key passed
* to remove() is null. * to remove() is null.
*/ */
import java.util.prefs.Preferences; import java.util.prefs.Preferences;
import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;
public class RemoveNullKeyCheck { public class RemoveNullKeyCheck {
private static boolean failed = false;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
try { checkPreferencesRemove();
Preferences node = Preferences.userRoot().node("N1"); checkAbstractPreferencesRemove();
node.remove(null); if (failed) {
throw new RuntimeException("Expected NullPointerException " + throw new RuntimeException("Expected NullPointerException " +
"not thrown"); "not thrown");
} catch (NullPointerException npe) { }
System.out.println("NullPointerException thrown"); }
}
public static void checkPreferencesRemove() {
try {
Preferences node = Preferences.userRoot().node("N1");
node.remove(null);
failed = true;
} catch (NullPointerException npe) {
}
}
public static void checkAbstractPreferencesRemove() {
Preferences abstrPrefs = new AbstractPreferences(null, "") {
@Override
protected void putSpi(String key, String value) {
}
@Override
protected String getSpi(String key) {
return null;
}
@Override
protected void removeSpi(String key) {
}
@Override
protected void removeNodeSpi() throws BackingStoreException {
}
@Override
protected String[] keysSpi() throws BackingStoreException {
return new String[0];
}
@Override
protected String[] childrenNamesSpi() throws BackingStoreException {
return new String[0];
}
@Override
protected AbstractPreferences childSpi(String name) {
return null;
}
@Override
protected void syncSpi() throws BackingStoreException {
}
@Override
protected void flushSpi() throws BackingStoreException {
}
};
try {
abstrPrefs.remove(null);
failed = true;
} catch(NullPointerException npe) {
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册