@@ -77,6 +77,17 @@ public class TransformQuery<Transformed>
7777 String value () default "" ;
7878 }
7979
80+ /**
81+ * This annotation marks the metadata field.
82+ * <p>
83+ * Metadata is returned in the {@link CDAEntry#metadata()} method.
84+ * <p>
85+ */
86+ @ Target (ElementType .FIELD )
87+ @ Retention (RetentionPolicy .RUNTIME )
88+ public @interface ContentfulMetadata {
89+ String value () default "metadata" ;
90+ }
8091 private final String contentTypeId ;
8192
8293 private final Map <String , Object > instanceCache = new HashMap <>();
@@ -114,6 +125,11 @@ public class TransformQuery<Transformed>
114125 final ContentfulSystemField systemField = field .getAnnotation (ContentfulSystemField .class );
115126 if (systemField != null ) {
116127 parseSystemFieldAnnotation (field , systemField );
128+ } else {
129+ final ContentfulMetadata metadata = field .getAnnotation (ContentfulMetadata .class );
130+ if (metadata != null ) {
131+ parseMetadataAnnotation (metadata );
132+ }
117133 }
118134 }
119135 }
@@ -221,6 +237,10 @@ private void parseSystemFieldAnnotation(Field field, ContentfulSystemField annot
221237 select ("sys." + name );
222238 }
223239
240+ private void parseMetadataAnnotation (ContentfulMetadata annotation ) {
241+ select (annotation .value ());
242+ }
243+
224244 /**
225245 * Retrieve the transformed entry from Contentful.
226246 *
@@ -376,6 +396,12 @@ private Object transform(CDAEntry entry) {
376396 field .getAnnotation (ContentfulSystemField .class );
377397 if (systemField != null ) {
378398 transformSystemFieldAnnotation (entry , result , field , systemField );
399+ } else {
400+ final ContentfulMetadata metadata =
401+ field .getAnnotation (ContentfulMetadata .class );
402+ if (metadata != null ) {
403+ transformMetadataAnnotation (entry , result , field , metadata );
404+ }
379405 }
380406 }
381407 }
@@ -389,6 +415,12 @@ private Object transform(CDAEntry entry) {
389415 field .getAnnotation (ContentfulSystemField .class );
390416 if (systemField != null ) {
391417 transformSystemFieldAnnotation (entry , result , field , systemField );
418+ } else {
419+ final ContentfulMetadata metadata =
420+ field .getAnnotation (ContentfulMetadata .class );
421+ if (metadata != null ) {
422+ transformMetadataAnnotation (entry , result , field , metadata );
423+ }
392424 }
393425 }
394426 }
@@ -475,4 +507,19 @@ private void transformSystemFieldAnnotation(CDAEntry entry, Object result, Field
475507 throw new IllegalStateException ("Cannot set custom system field " + key + "." );
476508 }
477509 }
510+
511+ private void transformMetadataAnnotation (CDAEntry entry , Object result , Field field ,
512+ ContentfulMetadata annotation ) {
513+ if (!field .isAccessible ()) {
514+ field .setAccessible (true );
515+ }
516+
517+ final String key = annotation .value ();
518+
519+ try {
520+ field .set (result , entry .metadata ());
521+ } catch (IllegalAccessException e ) {
522+ throw new IllegalStateException ("Cannot set " + key + "." );
523+ }
524+ }
478525}
0 commit comments