This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 99a815b838 Marshall module improvements
99a815b838 is described below
commit 99a815b838ef4924bf38c63259b18f8446da9d56
Author: James Bognar <[email protected]>
AuthorDate: Wed Dec 10 09:34:36 2025 -0500
Marshall module improvements
---
.../src/main/java/org/apache/juneau/BeanMap.java | 34 ++++----
.../src/main/java/org/apache/juneau/BeanMeta.java | 98 +++++++++++++++++-----
.../java/org/apache/juneau/BeanMetaFiltered.java | 2 +-
.../java/org/apache/juneau/BeanPropertyMeta.java | 36 ++++----
.../apache/juneau/BeanProxyInvocationHandler.java | 6 +-
5 files changed, 116 insertions(+), 60 deletions(-)
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMap.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMap.java
index 73e050a677..2220693d3d 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMap.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMap.java
@@ -108,7 +108,7 @@ public class BeanMap<T> extends AbstractMap<String,Object>
implements Delegate<T
this.meta = meta;
if (isNotEmpty(meta.getConstructorArgs()))
propertyCache = new TreeMap<>();
- this.typePropertyName =
session.getBeanTypePropertyName(meta.classMeta);
+ this.typePropertyName =
session.getBeanTypePropertyName(meta.getClassMeta());
}
/**
@@ -124,9 +124,9 @@ public class BeanMap<T> extends AbstractMap<String,Object>
implements Delegate<T
public void add(String property, Object value) {
var p = getPropertyMeta(property);
if (p == null) {
- if (meta.ctx.isIgnoreUnknownBeanProperties())
+ if (meta.getCtx().isIgnoreUnknownBeanProperties())
return;
- throw bex(meta.c, "Bean property ''{0}'' not found.",
property);
+ throw bex(meta.getC(), "Bean property ''{0}'' not
found.", property);
}
p.add(this, property, value);
}
@@ -135,11 +135,11 @@ public class BeanMap<T> extends
AbstractMap<String,Object> implements Delegate<T
public boolean containsKey(Object property) {
// JUNEAU-248: Match the behavior of keySet() - only check
properties map, not hiddenProperties
var key = emptyIfNull(property);
- if (meta.properties.containsKey(key) && ! "*".equals(key))
+ if (meta.getProperties().containsKey(key) && ! "*".equals(key))
return true;
- if (nn(meta.dynaProperty)) {
+ if (nn(meta.getDynaProperty())) {
try {
- return
meta.dynaProperty.getDynaMap(bean).containsKey(key);
+ return
meta.getDynaProperty().getDynaMap(bean).containsKey(key);
} catch (Exception e) {
throw bex(e);
}
@@ -157,7 +157,7 @@ public class BeanMap<T> extends AbstractMap<String,Object>
implements Delegate<T
// If this bean has a dyna-property, then we need to construct
the entire set before returning.
// Otherwise, we can create an iterator without a new data
structure.
- if (nn(meta.dynaProperty)) {
+ if (nn(meta.getDynaProperty())) {
Set<Entry<String,Object>> s = set();
forEachProperty(x -> true, x -> {
if (x.isDyna()) {
@@ -185,7 +185,7 @@ public class BeanMap<T> extends AbstractMap<String,Object>
implements Delegate<T
@Override /* Overridden from Set */
public Iterator<java.util.Map.Entry<String,Object>>
iterator() {
- // Construct our own anonymous iterator that
uses iterators against the meta.properties
+ // Construct our own anonymous iterator that
uses iterators against the meta.getProperties()
// map to maintain position. This prevents us
from having to construct any of our own
// collection objects.
return new Iterator<>() {
@@ -243,7 +243,7 @@ public class BeanMap<T> extends AbstractMap<String,Object>
implements Delegate<T
public BeanMap<T> forEachValue(Predicate<Object> valueFilter,
BeanPropertyConsumer action) {
// Normal bean.
- if (meta.dynaProperty == null) {
+ if (meta.getDynaProperty() == null) {
forEachProperty(BeanPropertyMeta::canRead, bpm -> {
try {
var val = bpm.get(this, null);
@@ -259,7 +259,7 @@ public class BeanMap<T> extends AbstractMap<String,Object>
implements Delegate<T
// Bean with dyna properties.
} else {
- Map<String,BeanPropertyValue> actions =
(meta.sortProperties ? sortedMap() : map());
+ Map<String,BeanPropertyValue> actions =
(meta.isSortProperties() ? sortedMap() : map());
forEachProperty(x -> ! x.isDyna(), bpm -> {
try {
@@ -433,7 +433,7 @@ public class BeanMap<T> extends AbstractMap<String,Object>
implements Delegate<T
propertyCache.forEach((k, v) -> put(k, v));
propertyCache = null;
} catch (IllegalArgumentException e) {
- throw bex(e, meta.classMeta.inner(),
"IllegalArgumentException occurred on call to class constructor ''{0}'' with
argument types ''{1}''", c.getSimpleName(),
+ throw bex(e, meta.getClassMeta().inner(),
"IllegalArgumentException occurred on call to class constructor ''{0}'' with
argument types ''{1}''", c.getSimpleName(),
Json5Serializer.DEFAULT.toString(getClasses(args)));
} catch (Exception e) {
throw bex(e);
@@ -529,15 +529,15 @@ public class BeanMap<T> extends
AbstractMap<String,Object> implements Delegate<T
*/
@Override /* Overridden from Map */
public Set<String> keySet() {
- if (meta.dynaProperty == null)
- return meta.properties.keySet();
+ if (meta.getDynaProperty() == null)
+ return meta.getProperties().keySet();
Set<String> l = set();
- meta.properties.forEach((k, v) -> {
+ meta.getProperties().forEach((k, v) -> {
if (! "*".equals(k))
l.add(k);
});
try {
- l.addAll(meta.dynaProperty.getDynaMap(bean).keySet());
+
l.addAll(meta.getDynaProperty().getDynaMap(bean).keySet());
} catch (Exception e) {
throw new BeanRuntimeException(e);
}
@@ -634,12 +634,12 @@ public class BeanMap<T> extends
AbstractMap<String,Object> implements Delegate<T
public Object put(String property, Object value) {
var p = getPropertyMeta(property);
if (p == null) {
- if (meta.ctx.isIgnoreUnknownBeanProperties() ||
property.equals(typePropertyName))
+ if (meta.getCtx().isIgnoreUnknownBeanProperties() ||
property.equals(typePropertyName))
return meta.onWriteProperty(bean, property,
null);
p = getPropertyMeta("*");
if (p == null)
- throw bex(meta.c, "Bean property ''{0}'' not
found.", property);
+ throw bex(meta.getC(), "Bean property ''{0}''
not found.", property);
}
return p.set(this, property, value);
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMeta.java
index beb446d563..95864260cf 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMeta.java
@@ -341,27 +341,27 @@ public class BeanMeta<T> {
return null;
}
- private final BeanConstructor beanConstructor;
// The constructor for this bean.
- protected final BeanFilter beanFilter;
// Optional bean filter associated with
the target class.
- private final OptionalSupplier<InvocationHandler>
beanProxyInvocationHandler; // The invocation
handler for this bean (if it's an interface).
- private final Supplier<BeanRegistry> beanRegistry;
// The bean registry for this bean.
- protected final Class<T> c;
// The target class that this meta
object describes.
- private final Supplier<List<ClassInfo>> classHierarchy;
// List of all classes traversed in the
class hierarchy.
- protected final ClassMeta<T> classMeta;
// The target class type that this meta
object describes.
- protected final BeanContext ctx;
// The bean context that created this
metadata object.
- private final Supplier<String> dictionaryName2;
// The @Bean(typeName) annotation
defined on this bean class.
- final BeanPropertyMeta dynaProperty;
// "extras" property.
- final boolean fluentSetters;
// Whether fluent setters are enabled.
- protected final Map<Method,String> getterProps;
// The getter properties on the target
class.
- protected final Map<String,BeanPropertyMeta> hiddenProperties;
// The hidden properties on the target
class.
- private final ConstructorInfo implClassConstructor;
// Optional constructor to use if one
cannot be found.
- final String notABeanReason;
// Readable string explaining why this
class wasn't a bean.
- protected final Map<String,BeanPropertyMeta> properties;
// The properties on the target class.
- protected final Map<Method,String> setterProps;
// The setter properties on the target
class.
- final boolean sortProperties;
// Whether properties should be sorted.
- private final Class<?> stopClass;
// The stop class for hierarchy
traversal.
- private final BeanPropertyMeta typeProperty;
// "_type" mock bean property.
- final String typePropertyName;
// "_type" property actual name.
+ private final BeanConstructor beanConstructor;
// The constructor for this bean.
+ private final BeanFilter beanFilter;
// Optional bean filter associated with the target class.
+ private final OptionalSupplier<InvocationHandler>
beanProxyInvocationHandler; // The invocation handler for this bean (if it's
an interface).
+ private final Supplier<BeanRegistry> beanRegistry;
// The bean registry for this bean.
+ private final Class<T> c;
// The target class that this meta object describes.
+ private final Supplier<List<ClassInfo>> classHierarchy;
// List of all classes traversed in the class hierarchy.
+ private final ClassMeta<T> classMeta;
// The target class type that this meta object describes.
+ private final BeanContext ctx;
// The bean context that created this metadata object.
+ private final Supplier<String> dictionaryName2;
// The @Bean(typeName) annotation defined on this bean class.
+ private final BeanPropertyMeta dynaProperty;
// "extras" property.
+ private final boolean fluentSetters;
// Whether fluent setters are enabled.
+ private final Map<Method,String> getterProps;
// The getter properties on the target class.
+ private final Map<String,BeanPropertyMeta> hiddenProperties;
// The hidden properties on the target class.
+ private final ConstructorInfo implClassConstructor;
// Optional constructor to use if one cannot be found.
+ private final String notABeanReason;
// Readable string explaining why this class wasn't a bean.
+ private final Map<String,BeanPropertyMeta> properties;
// The properties on the target class.
+ private final Map<Method,String> setterProps;
// The setter properties on the target class.
+ private final boolean sortProperties;
// Whether properties should be sorted.
+ private final Class<?> stopClass;
// The stop class for hierarchy traversal.
+ private final BeanPropertyMeta typeProperty;
// "_type" mock bean property.
+ private final String typePropertyName;
// "_type" property actual name.
/**
* Constructor.
@@ -720,6 +720,62 @@ public class BeanMeta<T> {
*/
public final String getTypePropertyName() { return typePropertyName; }
+ /**
+ * Returns the target class that this meta object describes.
+ *
+ * @return The target class.
+ */
+ protected final Class<T> getC() { return c; }
+
+ /**
+ * Returns the bean context that created this metadata object.
+ *
+ * @return The bean context.
+ */
+ protected final BeanContext getCtx() { return ctx; }
+
+ /**
+ * Returns the "extras" property for dynamic bean properties.
+ *
+ * @return The dynamic property, or <jk>null</jk> if not present.
+ */
+ protected final BeanPropertyMeta getDynaProperty() { return
dynaProperty; }
+
+ /**
+ * Returns whether fluent setters are enabled for this bean.
+ *
+ * @return <jk>true</jk> if fluent setters are enabled.
+ */
+ protected final boolean isFluentSetters() { return fluentSetters; }
+
+ /**
+ * Returns the map of getter methods to property names.
+ *
+ * @return The getter properties map.
+ */
+ protected final Map<Method,String> getGetterProps() { return
getterProps; }
+
+ /**
+ * Returns the reason why this class is not a bean, if applicable.
+ *
+ * @return The not-a-bean reason, or <jk>null</jk> if this is a bean.
+ */
+ protected final String getNotABeanReason() { return notABeanReason; }
+
+ /**
+ * Returns the map of setter methods to property names.
+ *
+ * @return The setter properties map.
+ */
+ protected final Map<Method,String> getSetterProps() { return
setterProps; }
+
+ /**
+ * Returns whether properties should be sorted for this bean.
+ *
+ * @return <jk>true</jk> if properties should be sorted.
+ */
+ protected final boolean isSortProperties() { return sortProperties; }
+
@Override /* Overridden from Object */
public int hashCode() {
return classMeta.hashCode();
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMetaFiltered.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMetaFiltered.java
index 8eab5d9274..74abfe763e 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMetaFiltered.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanMetaFiltered.java
@@ -45,6 +45,6 @@ public class BeanMetaFiltered<T> extends BeanMeta<T> {
* @param pNames The list of transformed property names.
*/
public BeanMetaFiltered(BeanMeta<T> innerMeta, String[] pNames) {
- super(innerMeta.classMeta, innerMeta.beanFilter, pNames, null);
+ super(innerMeta.getClassMeta(), innerMeta.getBeanFilter(),
pNames, null);
}
}
\ No newline at end of file
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
index 8478b62530..b8b0b5d0cb 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
@@ -77,7 +77,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
Builder(BeanMeta<?> beanMeta, String name) {
this.beanMeta = beanMeta;
- this.bc = beanMeta.ctx;
+ this.bc = beanMeta.getCtx();
this.name = name;
}
@@ -466,7 +466,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
var isArray = rawTypeMeta.isArray();
if (! (isCollection || isArray))
- throw bex(beanMeta.c, "Attempt to add element to
property ''{0}'' which is not a collection or array", name);
+ throw bex(beanMeta.getC(), "Attempt to add element to
property ''{0}'' which is not a collection or array", name);
var bean = m.getBean(true);
@@ -552,7 +552,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
var isBean = rawTypeMeta.isBean();
if (! (isBean || isMap))
- throw bex(beanMeta.c, "Attempt to add key/value to
property ''{0}'' which is not a map or bean", name);
+ throw bex(beanMeta.getC(), "Attempt to add key/value to
property ''{0}'' which is not a map or bean", name);
var bean = m.getBean(true);
@@ -793,7 +793,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
return (Map)getter.invoke(bean);
if (nn(field))
return (Map)field.get(bean);
- throw bex(beanMeta.c, "Getter or public field not
defined on property ''{0}''", name);
+ throw bex(beanMeta.getC(), "Getter or public field not
defined on property ''{0}''", name);
}
return Collections.EMPTY_MAP;
}
@@ -858,7 +858,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
return
rawTypeMeta.getPrimitiveDefault();
return null;
}
- throw bex(e, beanMeta.c, "Exception occurred while
getting property ''{0}''", name);
+ throw bex(e, beanMeta.getC(), "Exception occurred while
getting property ''{0}''", name);
}
}
@@ -1017,7 +1017,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
return
rawTypeMeta.getPrimitiveDefault();
return null;
}
- throw bex(e, beanMeta.c, "Exception occurred while
getting property ''{0}''", name);
+ throw bex(e, beanMeta.getC(), "Exception occurred while
getting property ''{0}''", name);
}
}
@@ -1031,14 +1031,14 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
} else if (nn(field))
m = (Map)field.get(bean);
else
- throw bex(beanMeta.c, "Getter or public field
not defined on property ''{0}''", name);
+ throw bex(beanMeta.getC(), "Getter or public
field not defined on property ''{0}''", name);
return (m == null ? null : m.get(pName));
}
if (nn(getter))
return getter.invoke(bean);
if (nn(field))
return field.get(bean);
- throw bex(beanMeta.c, "Getter or public field not defined on
property ''{0}''", name);
+ throw bex(beanMeta.getC(), "Getter or public field not defined
on property ''{0}''", name);
}
private Object invokeSetter(Object bean, String pName, Object val)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
@@ -1051,7 +1051,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
else if (nn(getter))
m = (Map<String,Object>)getter.invoke(bean);
else
- throw bex(beanMeta.c, "Cannot set property
''{0}'' of type ''{1}'' to object of type ''{2}'' because no setter is defined
on this property, and the existing property value is null",
+ throw bex(beanMeta.getC(), "Cannot set property
''{0}'' of type ''{1}'' to object of type ''{2}'' because no setter is defined
on this property, and the existing property value is null",
name, cn(this.getClassMeta()),
findClassName(val));
return (m == null ? null : m.put(pName, val));
}
@@ -1061,7 +1061,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
field.set(bean, val);
return null;
}
- throw bex(beanMeta.c, "Cannot set property ''{0}'' of type
''{1}'' to object of type ''{2}'' because no setter is defined on this
property, and the existing property value is null", name,
+ throw bex(beanMeta.getC(), "Cannot set property ''{0}'' of type
''{1}'' to object of type ''{2}'' because no setter is defined on this
property, and the existing property value is null", name,
cn(this.getClassMeta()), findClassName(val));
}
@@ -1092,7 +1092,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
if ((! isDyna) && field == null && setter == null && !
(isMap || isCollection)) {
if ((value == null &&
bc.isIgnoreUnknownNullBeanProperties()) || bc.isIgnoreMissingSetters())
return null;
- throw bex(beanMeta.c, "Setter or public field
not defined on property ''{0}''", name);
+ throw bex(beanMeta.getC(), "Setter or public
field not defined on property ''{0}''", name);
}
var bean = m.getBean(true); // Don't use getBean()
because it triggers array creation!
@@ -1116,7 +1116,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
if (value instanceof
CharSequence value2)
value =
JsonMap.ofJson(value2).session(session);
else
- throw bex(beanMeta.c,
"Cannot set property ''{0}'' of type ''{1}'' to object of type ''{2}''", name,
propertyClass.getName(), findClassName(value));
+ throw
bex(beanMeta.getC(), "Cannot set property ''{0}'' of type ''{1}'' to object of
type ''{2}''", name, propertyClass.getName(), findClassName(value));
}
var valueMap = (Map)value;
@@ -1128,7 +1128,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
if (!
rawTypeMeta.canCreateNewInstance()) {
if (propMap == null) {
if (setter == null &&
field == null)
- throw
bex(beanMeta.c,
+ throw
bex(beanMeta.getC(),
"Cannot
set property ''{0}'' of type ''{1}'' to object of type ''{2}'' because no
setter or public field is defined, and the current value is null", name,
propertyClass.getName(), findClassName(value));
@@ -1146,7 +1146,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
invokeSetter(bean, pName, valueMap);
return r;
}
- throw bex(beanMeta.c,
+ throw
bex(beanMeta.getC(),
"Cannot set
property ''{0}'' of type ''{2}'' to object of type ''{2}'' because the assigned
map cannot be converted to the specified type because the property type is
abstract, and the property value is currently null",
name,
propertyClass.getName(), findClassName(value));
}
@@ -1174,7 +1174,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
if (value instanceof
CharSequence value2)
value = new
JsonList(value2).setBeanSession(session);
else
- throw bex(beanMeta.c,
"Cannot set property ''{0}'' of type ''{1}'' to object of type ''{2}''", name,
propertyClass.getName(), findClassName(value));
+ throw
bex(beanMeta.getC(), "Cannot set property ''{0}'' of type ''{1}'' to object of
type ''{2}''", name, propertyClass.getName(), findClassName(value));
}
var valueList = (Collection)value;
@@ -1186,7 +1186,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
if (!
rawTypeMeta.canCreateNewInstance()) {
if (propList == null) {
if (setter == null &&
field == null)
- throw
bex(beanMeta.c,
+ throw
bex(beanMeta.getC(),
"Cannot
set property ''{0}'' of type ''{1}'' to object of type ''{2}'' because no
setter or public field is defined, and the current value is null", name,
propertyClass.getName(), findClassName(value));
@@ -1204,7 +1204,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
invokeSetter(bean, pName, valueList);
return r;
}
- throw bex(beanMeta.c,
+ throw
bex(beanMeta.getC(),
"Cannot set
property ''{0}'' of type ''{1}'' to object of type ''{2}'' because the assigned
map cannot be converted to the specified type because the property type is
abstract, and the property value is currently null",
name,
propertyClass.getName(), findClassName(value));
}
@@ -1245,7 +1245,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
return
rawTypeMeta.getPrimitiveDefault();
return null;
}
- throw bex(e, beanMeta.c, "Error occurred trying
to set property ''{0}''", name);
+ throw bex(e, beanMeta.getC(), "Error occurred
trying to set property ''{0}''", name);
}
} catch (ParseException e) {
throw bex(e);
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanProxyInvocationHandler.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanProxyInvocationHandler.java
index 098d3a1eaf..cb4b931196 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanProxyInvocationHandler.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanProxyInvocationHandler.java
@@ -68,7 +68,7 @@ public class BeanProxyInvocationHandler<T> implements
InvocationHandler {
return
this.beanProps.equals(ih2.beanProps);
}
}
- BeanMap<Object> bean = this.meta.ctx.toBeanMap(arg);
+ BeanMap<Object> bean =
this.meta.getCtx().toBeanMap(arg);
return this.beanProps.equals(bean);
}
@@ -78,11 +78,11 @@ public class BeanProxyInvocationHandler<T> implements
InvocationHandler {
if (mi.hasName("toString") && mi.getParameterCount() == 0)
return Json5Serializer.DEFAULT.toString(this.beanProps);
- String prop = this.meta.getterProps.get(method);
+ String prop = this.meta.getGetterProps().get(method);
if (nn(prop))
return this.beanProps.get(prop);
- prop = this.meta.setterProps.get(method);
+ prop = this.meta.getSetterProps().get(method);
if (nn(prop)) {
this.beanProps.put(prop, args[0]);
return null;