Title: Using null key or an uninitialized key when put a object into map.
Today, I encountered the problem shown as below when running a module.
Caused by: java.lang.IllegalArgumentException: No name specified
at org.apache.commons.beanutils.PropertyUtilsBean.isWriteable(PropertyUtilsBean.java:1226)
at org.apache.commons.beanutils.BeanUtilsBean.copyProperties(BeanUtilsBean.java:243)
at org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:114)
...
BeanUtilsBean.java
/**
* <p>Copy property values from the origin bean to the destination bean
* for all cases where the property names are the same.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @see BeanUtilsBean#copyProperties
*/
public static void copyProperties(Object dest, Object orig)
throws IllegalAccessException, InvocationTargetException {
BeanUtilsBean.getInstance().copyProperties(dest, orig);
}
BeanUtilsBean.java
/**
* <p>Copy property values from the origin bean to the destination bean
* for all cases where the property names are the same. For each
* property, a conversion is attempted as necessary. All combinations of
* standard JavaBeans and DynaBeans as origin and destination are
* supported. Properties that exist in the origin bean, but do not exist
* in the destination bean (or are read-only in the destination bean) are
* silently ignored.</p>
*
* <p>If the origin "bean" is actually a <code>Map</code>, it is assumed
* to contain String-valued <strong>simple</strong> property names as the keys, pointing at
* the corresponding property values that will be converted (if necessary)
* and set in the destination bean. <strong>Note</strong> that this method
* is intended to perform a "shallow copy" of the properties and so complex
* properties (for example, nested ones) will not be copied.</p>
*
* <p>This method differs from <code>populate()</code>, which
* was primarily designed for populating JavaBeans from the map of request
* parameters retrieved on an HTTP request, is that no scalar->indexed
* or indexed->scalar manipulations are performed. If the origin property
* is indexed, the destination property must be also.</p>
*
* <p>If you know that no type conversions are required, the
* <code>copyProperties()</code> method in {@link PropertyUtils} will
* execute faster than this method.</p>
*
* <p><strong>FIXME</strong> - Indexed and mapped properties that do not
* have getter and setter methods for the underlying array or Map are not
* copied by this method.</p>
*
* @param dest Destination bean whose properties are modified
* @param orig Origin bean whose properties are retrieved
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException if the <code>dest</code> or
* <code>orig</code> argument is null
* @exception InvocationTargetException if the property accessor method
* throws an exception
*/
public void copyProperties(Object dest, Object orig)
throws IllegalAccessException, InvocationTargetException {
// Validate existence of the specified beans
if (dest == null) {
throw new IllegalArgumentException
("No destination bean specified");
}
if (orig == null) {
throw new IllegalArgumentException("No origin bean specified");
}
if (log.isDebugEnabled()) {
log.debug("BeanUtils.copyProperties(" + dest + ", " +
orig + ")");
}
// Copy the properties, converting as necessary
if (orig instanceof DynaBean) {
DynaProperty origDescriptors[] =
((DynaBean) orig).getDynaClass().getDynaProperties();
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if (getPropertyUtils().isWriteable(dest, name)) {
Object value = ((DynaBean) orig).get(name);
copyProperty(dest, name, value);
}
}
} else if (orig instanceof Map) {
Iterator names = ((Map) orig).keySet().iterator();
while (names.hasNext()) {
String name = (String) names.next();
if (getPropertyUtils().isWriteable(dest, name)) {
Object value = ((Map) orig).get(name);
copyProperty(dest, name, value);
}
}
} else /* if (orig is a standard JavaBean) */ {
PropertyDescriptor origDescriptors[] =
getPropertyUtils().getPropertyDescriptors(orig);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (getPropertyUtils().isReadable(orig, name) &&
getPropertyUtils().isWriteable(dest, name)) {
try {
Object value =
getPropertyUtils().getSimpleProperty(orig, name);
copyProperty(dest, name, value);
} catch (NoSuchMethodException e) {
; // Should not happen
}
}
}
}
}
PropertyUtilsBean.java
/**
* <p>Return <code>true</code> if the specified property name identifies
* a writeable property on the specified bean; otherwise, return
* <code>false</code>.
*
* @param bean Bean to be examined (may be a {@link DynaBean}
* @param name Property name to be evaluated
*
* @exception IllegalPointerException if <code>bean</code>
* or <code>name</code> is <code>null</code>
*
* @since BeanUtils 1.6
*/
public boolean isWriteable(Object bean, String name) {
// Validate method parameters
if (bean == null) {
throw new IllegalArgumentException("No bean specified");
}
if (name == null) {
throw new IllegalArgumentException("No name specified");
}
// Return the requested result
if (bean instanceof DynaBean) {
// All DynaBean properties are writeable
return (((DynaBean) bean).getDynaClass().getDynaProperty(name) != null);
} else {
try {
PropertyDescriptor desc =
getPropertyDescriptor(bean, name);
if (desc != null) {
Method writeMethod = desc.getWriteMethod();
if ((writeMethod == null) &&
(desc instanceof IndexedPropertyDescriptor)) {
writeMethod = ((IndexedPropertyDescriptor) desc).getIndexedWriteMethod();
}
return (writeMethod != null);
} else {
return (false);
}
} catch (IllegalAccessException e) {
return (false);
} catch (InvocationTargetException e) {
return (false);
} catch (NoSuchMethodException e) {
return (false);
}
}
}
I thought null cannot be used as a key. Having getten "java.lang.IllegalArgumentException: No name specified" exception, I can't understand but this is a fact.
I found a object was put into a map using an uninitialized key at last.
Below code is a test for a null key and an uninitialized key.
public class Test {
String uninitializedKey;
/*
* The result is :
* Test null as a key.
* Test null as a key.
* Test not initialized key.
* Test not initialized key.
*/
public void test() {
Map<String, String> map1 = new HashMap<String, String>();
map1.put(null, "Test null as a key.");
System.out.println(map1.get(null));
System.out.println(map1.get(uninitializedKey));
Map<String, String> map2 = new HashMap<String, String>();
map2.put(uninitializedKey, "Test an uninitialized key.");
System.out.println(map2.get(null));
System.out.println(map2.get(uninitializedKey));
}
public static void main(String[] args) {
new Test().test();
}
}
分享到:
相关推荐
解决ZipArchive::getFromName(): Invalid or uninitialized Zip object报错问题
Issue #21134: Fix segfault when str is called on an uninitialized UnicodeEncodeError, UnicodeDecodeError, or UnicodeTranslateError object. Issue #19537: Fix PyUnicode_DATA() alignment under m68k. ...
* directly with an access token, API key, or (recommended) using * Application Default Credentials. */ $client->useApplicationDefaultCredentials(); $client->addScope(Google_Service_Plus::PLUS_ME); ...
标题 "stl_uninitialized.rar_The First" 涉及的是C++标准模板库(STL)中的一个关键概念,即未初始化的拷贝。在C++编程中,当我们创建一个新的对象或者数组时,它们可能不会自动被初始化,而是处于一个未知状态。这种...
uninitialized pointer(解决方案).md
uninitialized variable(解决方案).md
variable name is uninitialized(解决方案).md
- **Problem**: In C and C++, using `malloc` (or similar functions) to allocate memory requires a corresponding `free` call to deallocate the memory. However, it is common to forget the `free` call. - ...
- **Initialization**: Variables should be initialized to avoid using uninitialized values. For example, `int count = 0;` initializes `count` to zero. - **Types**: Objective-C supports various data ...
July, 3rd, 2015 : 1.5.14 : fixes an information leak vulnerability (CVE-2015-3281) A vulnerability was found when HTTP pipelining is used. In some cases, a client might be able to cause a buffer ...
然而,当你遇到"Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session"这样的错误时,这意味着在尝试销毁session之前,session并没有被正确地初始化。 首先,理解错误...
Note: If you use a symbol Foo in your source file, you should bring in a definition for Foo yourself, either via an #include or via a forward declaration. Do not depend on the symbol being brought in ...
Looper,MessageQueue,Handler分析之ActivityThread.java
项目中碰到的,记录一下
A fix to a crash caused by a dir() on an uninitialized module. A fix to a crash for bytearray.translate() with invalid parameters. A remedy for a data corruption issue in the new I/O library. A patch ...
DES_set_key((DES_cblock*)key.constData(), &schedule); QByteArray encrypted(data.size(), Qt::Uninitialized); DES_ecb_encrypt((DES_cblock*)data.constData(), (DES_cblock*)encrypted.data(), &schedule, ...