001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.annotation;
014
015import static java.lang.annotation.ElementType.*;
016import static java.lang.annotation.RetentionPolicy.*;
017
018import java.lang.annotation.*;
019
020import org.apache.juneau.*;
021
022/**
023 * Used tailor how bean properties get interpreted by the framework.
024 *
025 * <p>
026 * This annotation is applied to public fields and public getter/setter methods of beans.
027 *
028 * <h5 class='section'>See Also:</h5>
029 * <ul>
030 *    <li class='link'>{@doc juneau-marshall.Transforms.BeanPropertyAnnotation}
031 * </ul>
032 */
033@Documented
034@Target({FIELD,METHOD})
035@Retention(RUNTIME)
036@Inherited
037public @interface BeanProperty {
038
039   /**
040    * Identifies the name of the property.
041    *
042    * <p>
043    * Normally, this is automatically inferred from the field name or getter method name of the property.
044    * However, this property can be used to assign a different property name from the automatically inferred value.
045    *
046    * <p>
047    * If the {@link BeanContext#BEAN_beanFieldVisibility} setting on the bean context excludes this field (e.g. the
048    * visibility is set to PUBLIC, but the field is PROTECTED), this annotation can be used to force the field to be
049    * identified as a property.
050    *
051    * <h5 class='topic'>Dynamic beans</h5>
052    * <p>
053    * The bean property named <js>"*"</js> is the designated "dynamic property" which allows for "extra" bean
054    * properties not otherwise defined.
055    * This is similar in concept to the Jackson <ja>@JsonGetterAll</ja> and <ja>@JsonSetterAll</ja> annotations.
056    * The primary purpose is for backwards compatibility in parsing newer streams with addition information into older
057    * beans.
058    *
059    * <p>
060    * The following examples show how to define dynamic bean properties.
061    * <p class='bcode w800'>
062    *    <jc>// Option #1 - A simple public Map field.
063    *    // The field name can be anything.</jc>
064    *    <jk>public class</jk> BeanWithDynaField {
065    *
066    *       <ja>@BeanProperty</ja>(name=<js>"*"</js>)
067    *       <jk>public</jk> Map&lt;String,Object&gt; extraStuff = <jk>new</jk> LinkedHashMap&lt;String,Object&gt;();
068    *    }
069    *
070    *    <jc>// Option #2 - Getters and setters.
071    *    // Method names can be anything.
072    *    // Getter must return a Map with String keys.
073    *    // Setter must take in two arguments.</jc>
074    *    <jk>public class</jk> BeanWithDynaMethods {
075    *
076    *       <ja>@BeanProperty</ja>(name=<js>"*"</js>)
077    *       <jk>public</jk> Map&lt;String,Object&gt; getMyExtraStuff() {
078    *          ...
079    *       }
080    *
081    *       <ja>@BeanProperty</ja>(name=<js>"*"</js>)
082    *       <jk>public void</jk> setAnExtraField(String name, Object value) {
083    *          ...
084    *       }
085    *    }
086    *
087    *    <jc>// Option #3 - Getter only.
088    *    // Properties will be added through the getter.</jc>
089    *    <jk>public class</jk> BeanWithDynaGetterOnly {
090    *
091    *       <ja>@BeanProperty</ja>(name=<js>"*"</js>)
092    *       <jk>public</jk> Map&lt;String,Object&gt; getMyExtraStuff() {
093    *          ...
094    *       }
095    *    }
096    *
097    *    <jc>// Option #4 - Getter, setter, and extra-keys method .
098    *    // Define a method that returns a Collection&lt;String&gt; with currently-set property names.</jc>
099    *    <jk>public class</jk> BeanWithDynaExtraKeys {
100    *
101    *       <ja>@BeanProperty</ja>(name=<js>"*"</js>)
102    *       <jk>public</jk> Object get(String name) {
103    *          ...
104    *       }
105    *
106    *       <ja>@BeanProperty</ja>(name=<js>"*"</js>)
107    *       <jk>public void</jk> set(String name, Object value) {
108    *          ...
109    *       }
110    *
111    *       <ja>@BeanProperty</ja>(name=<js>"*"</js>)
112    *       <jk>public</jk> Collection&lt;String&gt; extraKeys() {
113    *          ...
114    *       }
115    *    }
116    * </p>
117    *
118    *<p>
119    * Similar rules apply for value types and swaps.
120    * The property values optionally can be any serializable type or use swaps.
121    * <p class='bcode w800'>
122    *    <jc>// A serializable type other than Object.</jc>
123    *    <jk>public class</jk> BeanWithDynaFieldWithListValues {
124    *
125    *       <ja>@BeanProperty</ja>(name=<js>"*"</js>)
126    *       <jk>public</jk> Map&lt;String,List&lt;String&gt;&gt; getMyExtraStuff() {
127    *          ...
128    *       }
129    *    }
130    *
131    *    <jc>// A swapped value.</jc>
132    *    <jk>public class</jk> BeanWithDynaFieldWithSwappedValues {
133    *
134    *       <ja>@BeanProperty</ja>(name=<js>"*"</js>, swap=CalendarSwap.<jsf>ISO8601DTZ</jsf>.<jk>class</jk>)
135    *       <jk>public</jk> Map&lt;String,Calendar&gt; getMyExtraStuff() {
136    *          ...
137    *       }
138    *    }
139    * </p>
140    *
141    * <ul class='doctree'>
142    *    <li class='info'>
143    *       Note that if you're not interested in these additional properties, you can also use the
144    *       {@link BeanContext#BEAN_ignoreUnknownBeanProperties} setting to ignore values that don't fit into existing
145    *       properties.
146    * </ul>
147    */
148   String name() default "";
149
150   /**
151    * A synonym for {@link #name()}.
152    *
153    * <p>
154    * The following annotations are equivalent:
155    *
156    * <p class='bcode w800'>
157    *    <ja>@BeanProperty</ja>(name=<js>"foo"</js>)
158    *
159    *    <ja>@BeanProperty</ja>(<js>"foo"</js>)
160    * </p>
161    */
162   String value() default "";
163
164   /**
165    * Identifies a specialized class type for the property.
166    *
167    * <p>
168    * Normally this can be inferred through reflection of the field type or getter return type.
169    * However, you'll want to specify this value if you're parsing beans where the bean property class is an interface
170    * or abstract class to identify the bean type to instantiate.
171    * Otherwise, you may cause an {@link InstantiationException} when trying to set these fields.
172    *
173    * <p>
174    * This property must denote a concrete bean class with a no-arg constructor.
175    *
176    * <h5 class='section'>Example:</h5>
177    * <p class='bcode w800'>
178    *    <jk>public class</jk> MyBean {
179    *
180    *       <jc>// Identify concrete map type.</jc>
181    *       <ja>@BeanProperty</ja>(type=HashMap.<jk>class</jk>)
182    *       <jk>public</jk> Map <jf>p1</jf>;
183    *    }
184    * </p>
185    *
186    * <p>
187    * This annotation can also be used on private fields of a property like so:
188    *
189    * <h5 class='section'>Example:</h5>
190    * <p class='bcode w800'>
191    *    <jk>public class</jk> MyBean {
192    *
193    *       <ja>@BeanProperty</ja>(type=HashMap.<jk>class</jk>)
194    *       <jk>private</jk> Map <jf>p1</jf>;
195    *
196    *       <jk>public</jk> Map getP1() {
197    *          <jk>return</jk> <jf>p1</jf>;
198    *       }
199    *    }
200    * </p>
201    */
202   Class<?> type() default Object.class;
203
204   /**
205    * For bean properties of maps and collections, this annotation can be used to identify the class types of the
206    * contents of the bean property object when the generic parameter types are interfaces or abstract classes.
207    *
208    * <h5 class='section'>Example:</h5>
209    * <p class='bcode w800'>
210    *    <jk>public class</jk> MyBean {
211    *
212    *       <jc>// Identify concrete map type with String keys and Integer values.</jc>
213    *       <ja>@BeanProperty</ja>(type=HashMap.<jk>class</jk>, params={String.<jk>class</jk>,Integer.<jk>class</jk>})
214    *       <jk>public</jk> Map <jf>p1</jf>;
215    *    }
216    * </p>
217    *
218    * <p>
219    * This annotation can also be used on private fields of a property like so:
220    *
221    * <h5 class='section'>Example:</h5>
222    * <p class='bcode w800'>
223    *    <jk>public class</jk> MyBean {
224    *
225    *       <ja>@BeanProperty</ja>(type=HashMap.<jk>class</jk>, params={String.<jk>class</jk>,Integer.<jk>class</jk>})
226    *       <jk>private</jk> Map <jf>p1</jf>;
227    *
228    *       <jk>public</jk> Map getP1() {
229    *          <jk>return</jk> <jf>p1</jf>;
230    *       }
231    *    }
232    * </p>
233    */
234   Class<?>[] params() default {};
235
236   /**
237    * Used to limit which child properties are rendered by the serializers.
238    *
239    * <p>
240    * Can be used on any of the following bean property types:
241    * <ul class='spaced-list'>
242    *    <li>Beans - Only render the specified properties of the bean.
243    *    <li>Maps - Only render the specified entries in the map.
244    *    <li>Bean/Map arrays - Same, but applied to each element in the array.
245    *    <li>Bean/Map collections - Same, but applied to each element in the collection.
246    * </ul>
247    *
248    * <h5 class='section'>Example:</h5>
249    * <p class='bcode w800'>
250    *    <jk>public class</jk> MyClass {
251    *
252    *       <jc>// Only render 'f1' when serializing this bean property.</jc>
253    *       <ja>@BeanProperty</ja>(properties=<js>"f1"</js>)
254    *       <jk>public</jk> MyChildClass x1 = <jk>new</jk> MyChildClass();
255    *    }
256    *
257    *    <jk>public class</jk> MyChildClass {
258    *       <jk>public int</jk> f1 = 1;
259    *       <jk>public int</jk> f2 = 2;
260    *    }
261    *
262    *    <jc>// Renders "{x1:{f1:1}}"</jc>
263    *    String json = JsonSerializer.<jsf>DEFAULT</jsf>.serialize(<jk>new</jk> MyClass());
264    * </p>
265    *
266    * <p>
267    * This annotation can also be used on private fields of a property like so:
268    *
269    * <h5 class='section'>Example:</h5>
270    * <p class='bcode w800'>
271    *    <jk>public class</jk> MyBean {
272    *
273    *       <ja>@BeanProperty</ja>(properties=<js>"f1"</js>)
274    *       <jk>private</jk> MyChildClass <jf>x1</jf>;
275    *
276    *       <jk>public</jk> MyChildClass getX1() {
277    *          <jk>return</jk> <jf>x1</jf>;
278    *       }
279    *    }
280    * </p>
281    */
282   String properties() default "";
283
284   /**
285    * Bean dictionary.
286    *
287    * <p>
288    * The list of classes that make up the bean dictionary this bean property.
289    *
290    * <h5 class='section'>See Also:</h5>
291    * <ul>
292    *    <li class='jf'>{@link BeanContext#BEAN_beanDictionary}
293    * </ul>
294    *
295    * <p>
296    * This annotation can also be used on private fields of a property.
297    */
298   Class<?>[] beanDictionary() default {};
299
300   /**
301    * Specifies a String format for converting the bean property value to a formatted string.
302    *
303    * <p>
304    * Note that this is usually a one-way conversion during serialization.
305    *
306    * <p>
307    * During parsing, we will attempt to convert the value to the original form by using the
308    * {@link BeanSession#convertToType(Object, Class)} but there is no guarantee that this will succeed.
309    *
310    * <h5 class='section'>Example:</h5>
311    * <p class='bcode w800'>
312    *    <ja>@BeanProperty</ja>(format=<js>"$%.2f"</js>)
313    *    <jk>public float</jk> <jf>price</jf>;
314    * </p>
315    *
316    * <p>
317    * This annotation can also be used on private fields of a property like so:
318    *
319    * <h5 class='section'>Example:</h5>
320    * <p class='bcode w800'>
321    *    <jk>public class</jk> MyBean {
322    *
323    *       <ja>@BeanProperty</ja>(format=<js>"$%.2f"</js>)
324    *       <jk>private float</jk> <jf>price</jf>;
325    *
326    *       <jk>public float</jk> gePrice() {
327    *          <jk>return</jk> <jf>price</jf>;
328    *       }
329    *    }
330    * </p>
331    */
332   String format() default "";
333}