You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2018/09/28 13:05:49 UTC
[juneau] branch master updated: Allow bean property annotations on
private fields.
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 d012753 Allow bean property annotations on private fields.
d012753 is described below
commit d0127539dce46044c8057660fe9f52c475ef314e
Author: JamesBognar <ja...@apache.org>
AuthorDate: Fri Sep 28 09:05:28 2018 -0400
Allow bean property annotations on private fields.
---
.../src/main/java/org/apache/juneau/BeanMeta.java | 17 +++++++++++
.../java/org/apache/juneau/BeanPropertyMeta.java | 35 ++++++++++++++++------
.../apache/juneau/html/HtmlBeanPropertyMeta.java | 4 +--
.../jsonschema/JsonSchemaBeanPropertyMeta.java | 4 +--
.../org/apache/juneau/xml/XmlBeanPropertyMeta.java | 4 +--
.../src/main/resources/ReleaseNotes/7.2.1.html | 4 +++
6 files changed, 53 insertions(+), 15 deletions(-)
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 15d02fb..f7c63ba 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
@@ -347,6 +347,9 @@ public class BeanMeta<T> {
for (Iterator<BeanPropertyMeta.Builder> i = normalProps.values().iterator(); i.hasNext();) {
BeanPropertyMeta.Builder p = i.next();
try {
+ if (p.field == null)
+ p.setInnerField(findInnerBeanField(c, stopClass, p.name));
+
if (p.validate(ctx, beanRegistry, typeVarImpls)) {
if (p.getter != null)
@@ -721,6 +724,20 @@ public class BeanMeta<T> {
return l;
}
+ static final Field findInnerBeanField(Class<?> c, Class<?> stopClass, String name) {
+ for (Class<?> c2 : findClasses(c, stopClass)) {
+ for (Field f : c2.getDeclaredFields()) {
+ if (isAny(f, STATIC, TRANSIENT))
+ continue;
+ if (f.isAnnotationPresent(BeanIgnore.class))
+ continue;
+ if (f.getName().equals(name))
+ return f;
+ }
+ }
+ return null;
+ }
+
private static List<Class<?>> findClasses(Class<?> c, Class<?> stopClass) {
LinkedList<Class<?>> l = new LinkedList<>();
findClasses(c, l, stopClass);
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 9fbcfb4..95e9adc 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
@@ -50,6 +50,7 @@ public final class BeanPropertyMeta {
private final String name; // The name of the property.
private final Field field; // The bean property field (if it has one).
+ private final Field innerField; // The bean property field (if it has one).
private final Method getter, setter, extraKeys; // The bean property getter and setter.
private final boolean isUri; // True if this is a URL/URI or annotated with @URI.
private final boolean isDyna, isDynaGetterMap; // This is a dyna property (i.e. name="*")
@@ -86,7 +87,7 @@ public final class BeanPropertyMeta {
BeanMeta<?> beanMeta;
BeanContext beanContext;
String name;
- Field field;
+ Field field, innerField;
Method getter, setter, extraKeys;
boolean isConstructorArg, isUri, isDyna, isDynaGetterMap;
ClassMeta<?> rawTypeMeta, typeMeta;
@@ -172,16 +173,16 @@ public final class BeanPropertyMeta {
canRead |= (field != null || getter != null);
canWrite |= (field != null || setter != null);
- if (field != null) {
- BeanProperty p = field.getAnnotation(BeanProperty.class);
- rawTypeMeta = f.resolveClassMeta(p, field.getGenericType(), typeVarImpls);
- isUri |= (rawTypeMeta.isUri() || field.isAnnotationPresent(org.apache.juneau.annotation.URI.class));
+ if (innerField != null) {
+ BeanProperty p = innerField.getAnnotation(BeanProperty.class);
+ rawTypeMeta = f.resolveClassMeta(p, innerField.getGenericType(), typeVarImpls);
+ isUri |= (rawTypeMeta.isUri() || innerField.isAnnotationPresent(org.apache.juneau.annotation.URI.class));
if (p != null) {
if (! p.properties().isEmpty())
properties = split(p.properties());
bdClasses.addAll(Arrays.asList(p.beanDictionary()));
}
- Swap s = field.getAnnotation(Swap.class);
+ Swap s = innerField.getAnnotation(Swap.class);
if (s != null) {
swap = getPropertyPojoSwap(s);
}
@@ -260,12 +261,12 @@ public final class BeanPropertyMeta {
return false;
}
}
- if (field != null) {
+ if (innerField != null) {
if (isDyna) {
- if (! isParentClass(Map.class, field.getType()))
+ if (! isParentClass(Map.class, innerField.getType()))
return false;
} else {
- if (! isParentClass(field.getType(), c))
+ if (! isParentClass(innerField.getType(), c))
return false;
}
}
@@ -333,6 +334,12 @@ public final class BeanPropertyMeta {
BeanPropertyMeta.Builder setField(Field field) {
setAccessible(field, false);
this.field = field;
+ this.innerField = field;
+ return this;
+ }
+
+ BeanPropertyMeta.Builder setInnerField(Field innerField) {
+ this.innerField = field;
return this;
}
@@ -356,6 +363,7 @@ public final class BeanPropertyMeta {
*/
protected BeanPropertyMeta(BeanPropertyMeta.Builder b) {
this.field = b.field;
+ this.innerField = b.innerField;
this.getter = b.getter;
this.setter = b.setter;
this.extraKeys = b.extraKeys;
@@ -424,6 +432,15 @@ public final class BeanPropertyMeta {
}
/**
+ * Returns the field for this property even if the field is private.
+ *
+ * @return The field for this bean property, or <jk>null</jk> if there is no field associated with this bean property.
+ */
+ public Field getInnerField() {
+ return innerField;
+ }
+
+ /**
* Returns the {@link ClassMeta} of the class of this property.
*
* <p>
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java
index 2c1401c..929e753 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java
@@ -41,8 +41,8 @@ public final class HtmlBeanPropertyMeta extends BeanPropertyMetaExtended {
public HtmlBeanPropertyMeta(BeanPropertyMeta bpm) throws Exception {
super(bpm);
Builder b = new Builder();
- if (bpm.getField() != null)
- b.findHtmlInfo(bpm.getField().getAnnotation(Html.class));
+ if (bpm.getInnerField() != null)
+ b.findHtmlInfo(bpm.getInnerField().getAnnotation(Html.class));
if (bpm.getGetter() != null)
b.findHtmlInfo(bpm.getGetter().getAnnotation(Html.class));
if (bpm.getSetter() != null)
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/jsonschema/JsonSchemaBeanPropertyMeta.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/jsonschema/JsonSchemaBeanPropertyMeta.java
index ab63296..9544558 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/jsonschema/JsonSchemaBeanPropertyMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/jsonschema/JsonSchemaBeanPropertyMeta.java
@@ -41,8 +41,8 @@ public class JsonSchemaBeanPropertyMeta extends BeanPropertyMetaExtended {
public JsonSchemaBeanPropertyMeta(BeanPropertyMeta bpm) {
super(bpm);
- if (bpm.getField() != null)
- findInfo(bpm.getField().getAnnotation(Schema.class));
+ if (bpm.getInnerField() != null)
+ findInfo(bpm.getInnerField().getAnnotation(Schema.class));
if (bpm.getGetter() != null)
findInfo(bpm.getGetter().getAnnotation(Schema.class));
if (bpm.getSetter() != null)
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlBeanPropertyMeta.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlBeanPropertyMeta.java
index 8d47d30..a98013f 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlBeanPropertyMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlBeanPropertyMeta.java
@@ -40,8 +40,8 @@ public class XmlBeanPropertyMeta extends BeanPropertyMetaExtended {
public XmlBeanPropertyMeta(BeanPropertyMeta bpm) {
super(bpm);
- if (bpm.getField() != null)
- findXmlInfo(bpm.getField().getAnnotation(Xml.class));
+ if (bpm.getInnerField() != null)
+ findXmlInfo(bpm.getInnerField().getAnnotation(Xml.class));
if (bpm.getGetter() != null)
findXmlInfo(bpm.getGetter().getAnnotation(Xml.class));
if (bpm.getSetter() != null)
diff --git a/juneau-doc/src/main/resources/ReleaseNotes/7.2.1.html b/juneau-doc/src/main/resources/ReleaseNotes/7.2.1.html
index f710c67..25011da 100644
--- a/juneau-doc/src/main/resources/ReleaseNotes/7.2.1.html
+++ b/juneau-doc/src/main/resources/ReleaseNotes/7.2.1.html
@@ -23,4 +23,8 @@
<ul class='spaced-list'>
<li>
The <code><del>@JsonSchema</del></code> annotation has been merged with the {@link oaj.jsonschema.annotation.Schema @Schema} annotation.
+ <li>
+ Annotations typically used on bean properties (getters/setters/public fields) can now be used on private fields.
+ This is inline with behavior on JPA-annotated beans.
+ These include: <ja>@Swap</ja>, <ja>@Html</ja>, <ja>@Xml</ja>, <ja>@BeanProperty</ja>.
</ul>