Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom map wrapper to handle keys with dots or brackets #3076

Merged
merged 3 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions src/main/java/org/apache/ibatis/reflection/MetaObject.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -112,31 +112,11 @@ public boolean hasGetter(String name) {

public Object getValue(String name) {
PropertyTokenizer prop = new PropertyTokenizer(name);
if (!prop.hasNext()) {
return objectWrapper.get(prop);
}
MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return null;
}
return metaValue.getValue(prop.getChildren());
return objectWrapper.get(prop);
}

public void setValue(String name, Object value) {
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
if (value == null) {
// don't instantiate child path if value is null
return;
}
metaValue = objectWrapper.instantiatePropertyValue(name, prop, objectFactory);
}
metaValue.setValue(prop.getChildren(), value);
} else {
objectWrapper.set(prop, value);
}
objectWrapper.set(new PropertyTokenizer(name), value);
}

public MetaObject metaObjectForProperty(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@

import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ReflectionException;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.reflection.property.PropertyTokenizer;

/**
Expand All @@ -42,6 +43,10 @@ protected Object resolveCollection(PropertyTokenizer prop, Object object) {
}

protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
if (collection == null) {
throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
+ prop.getName() + "' is null.");
}
if (collection instanceof Map) {
return ((Map) collection).get(prop.getIndex());
}
Expand All @@ -67,12 +72,16 @@ protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
} else if (collection instanceof short[]) {
return ((short[]) collection)[i];
} else {
throw new ReflectionException(
"The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
+ prop.getName() + "' is not Map, List or Array.");
}
}

protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
if (collection == null) {
throw new ReflectionException("Cannot set the value '" + prop.getIndexedName() + "' because the property '"
+ prop.getName() + "' is null.");
}
if (collection instanceof Map) {
((Map) collection).put(prop.getIndex(), value);
} else {
Expand All @@ -98,10 +107,29 @@ protected void setCollectionValue(PropertyTokenizer prop, Object collection, Obj
} else if (collection instanceof short[]) {
((short[]) collection)[i] = (Short) value;
} else {
throw new ReflectionException(
"The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
throw new ReflectionException("Cannot set the value '" + prop.getIndexedName() + "' because the property '"
+ prop.getName() + "' is not Map, List or Array.");
}
}
}

protected Object getChildValue(PropertyTokenizer prop) {
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return null;
}
return metaValue.getValue(prop.getChildren());
}

protected void setChildValue(PropertyTokenizer prop, Object value) {
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
if (value == null) {
// don't instantiate child path if value is null
return;
}
metaValue = instantiatePropertyValue(null, new PropertyTokenizer(prop.getName()), metaObject.getObjectFactory());
}
metaValue.setValue(prop.getChildren(), value);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,18 +42,21 @@ public BeanWrapper(MetaObject metaObject, Object object) {

@Override
public Object get(PropertyTokenizer prop) {
if (prop.getIndex() != null) {
Object collection = resolveCollection(prop, object);
return getCollectionValue(prop, collection);
if (prop.hasNext()) {
return getChildValue(prop);
} else if (prop.getIndex() != null) {
return getCollectionValue(prop, resolveCollection(prop, object));
} else {
return getBeanProperty(prop, object);
}
return getBeanProperty(prop, object);
}

@Override
public void set(PropertyTokenizer prop, Object value) {
if (prop.getIndex() != null) {
Object collection = resolveCollection(prop, object);
setCollectionValue(prop, collection, value);
if (prop.hasNext()) {
setChildValue(prop, value);
} else if (prop.getIndex() != null) {
setCollectionValue(prop, resolveCollection(prop, object), value);
} else {
setBeanProperty(prop, object, value);
}
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/org/apache/ibatis/reflection/wrapper/MapWrapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@
*/
public class MapWrapper extends BaseWrapper {

private final Map<String, Object> map;
protected final Map<String, Object> map;

public MapWrapper(MetaObject metaObject, Map<String, Object> map) {
super(metaObject);
Expand All @@ -38,18 +38,21 @@ public MapWrapper(MetaObject metaObject, Map<String, Object> map) {

@Override
public Object get(PropertyTokenizer prop) {
if (prop.getIndex() != null) {
Object collection = resolveCollection(prop, map);
return getCollectionValue(prop, collection);
if (prop.hasNext()) {
return getChildValue(prop);
} else if (prop.getIndex() != null) {
return getCollectionValue(prop, resolveCollection(prop, map));
} else {
return map.get(prop.getName());
}
return map.get(prop.getName());
}

@Override
public void set(PropertyTokenizer prop, Object value) {
if (prop.getIndex() != null) {
Object collection = resolveCollection(prop, map);
setCollectionValue(prop, collection, value);
if (prop.hasNext()) {
setChildValue(prop, value);
} else if (prop.getIndex() != null) {
setCollectionValue(prop, resolveCollection(prop, map), value);
} else {
map.put(prop.getName(), value);
}
Expand Down
Loading