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 * <ul class='seealso'>
029 *    <li class='link'>{@doc juneau-marshall.Transforms.BeanpAnnotation}
030 * </ul>
031 */
032@Documented
033@Target({FIELD,METHOD,PARAMETER})
034@Retention(RUNTIME)
035@Inherited
036public @interface Beanp {
037
038   /**
039    * Identifies the name of the property.
040    *
041    * <p>
042    * Normally, this is automatically inferred from the field name or getter method name of the property.
043    * However, this property can be used to assign a different property name from the automatically inferred value.
044    *
045    * <p>
046    * If the {@link BeanContext#BEAN_beanFieldVisibility} setting on the bean context excludes this field (e.g. the
047    * visibility is set to PUBLIC, but the field is PROTECTED), this annotation can be used to force the field to be
048    * identified as a property.
049    *
050    * <h5 class='topic'>Dynamic beans</h5>
051    * <p>
052    * The bean property named <js>"*"</js> is the designated "dynamic property" which allows for "extra" bean
053    * properties not otherwise defined.
054    * This is similar in concept to the Jackson <ja>@JsonGetterAll</ja> and <ja>@JsonSetterAll</ja> annotations.
055    * The primary purpose is for backwards compatibility in parsing newer streams with addition information into older
056    * beans.
057    *
058    * <p>
059    * The following examples show how to define dynamic bean properties.
060    * <p class='bcode w800'>
061    *    <jc>// Option #1 - A simple public Map field.
062    *    // The field name can be anything.</jc>
063    *    <jk>public class</jk> BeanWithDynaField {
064    *
065    *       <ja>@Beanp</ja>(name=<js>"*"</js>)
066    *       <jk>public</jk> Map&lt;String,Object&gt; extraStuff = <jk>new</jk> LinkedHashMap&lt;String,Object&gt;();
067    *    }
068    *
069    *    <jc>// Option #2 - Getters and setters.
070    *    // Method names can be anything.
071    *    // Getter must return a Map with String keys.
072    *    // Setter must take in two arguments.</jc>
073    *    <jk>public class</jk> BeanWithDynaMethods {
074    *
075    *       <ja>@Beanp</ja>(name=<js>"*"</js>)
076    *       <jk>public</jk> Map&lt;String,Object&gt; getMyExtraStuff() {
077    *          ...
078    *       }
079    *
080    *       <ja>@Beanp</ja>(name=<js>"*"</js>)
081    *       <jk>public void</jk> setAnExtraField(String name, Object value) {
082    *          ...
083    *       }
084    *    }
085    *
086    *    <jc>// Option #3 - Getter only.
087    *    // Properties will be added through the getter.</jc>
088    *    <jk>public class</jk> BeanWithDynaGetterOnly {
089    *
090    *       <ja>@Beanp</ja>(name=<js>"*"</js>)
091    *       <jk>public</jk> Map&lt;String,Object&gt; getMyExtraStuff() {
092    *          ...
093    *       }
094    *    }
095    *
096    *    <jc>// Option #4 - Getter, setter, and extra-keys method .
097    *    // Define a method that returns a Collection&lt;String&gt; with currently-set property names.</jc>
098    *    <jk>public class</jk> BeanWithDynaExtraKeys {
099    *
100    *       <ja>@Beanp</ja>(name=<js>"*"</js>)
101    *       <jk>public</jk> Object get(String name) {
102    *          ...
103    *       }
104    *
105    *       <ja>@Beanp</ja>(name=<js>"*"</js>)
106    *       <jk>public void</jk> set(String name, Object value) {
107    *          ...
108    *       }
109    *
110    *       <ja>@Beanp</ja>(name=<js>"*"</js>)
111    *       <jk>public</jk> Collection&lt;String&gt; extraKeys() {
112    *          ...
113    *       }
114    *    }
115    * </p>
116    *
117    *<p>
118    * Similar rules apply for value types and swaps.
119    * The property values optionally can be any serializable type or use swaps.
120    * <p class='bcode w800'>
121    *    <jc>// A serializable type other than Object.</jc>
122    *    <jk>public class</jk> BeanWithDynaFieldWithListValues {
123    *
124    *       <ja>@Beanp</ja>(name=<js>"*"</js>)
125    *       <jk>public</jk> Map&lt;String,List&lt;String&gt;&gt; getMyExtraStuff() {
126    *          ...
127    *       }
128    *    }
129    *
130    *    <jc>// A swapped value.</jc>
131    *    <jk>public class</jk> BeanWithDynaFieldWithSwappedValues {
132    *
133    *       <ja>@Beanp</ja>(name=<js>"*"</js>, swap=TemporalCalendarSwap.IsoLocalDateTime.<jk>class</jk>)
134    *       <jk>public</jk> Map&lt;String,Calendar&gt; getMyExtraStuff() {
135    *          ...
136    *       }
137    *    }
138    * </p>
139    *
140    * <div class='info'>
141    *    Note that if you're not interested in these additional properties, you can also use the
142    *    {@link BeanContext#BEAN_ignoreUnknownBeanProperties} setting to ignore values that don't fit into existing
143    *    properties.
144    * </div>
145    * <div class='info'>
146    *       Note that the {@link Name @Name} annotation can also be used for identifying a property name.
147    * </div>
148    */
149   String name() default "";
150
151   /**
152    * A synonym for {@link #name()}.
153    *
154    * <p>
155    * The following annotations are equivalent:
156    *
157    * <p class='bcode w800'>
158    *    <ja>@Beanp</ja>(name=<js>"foo"</js>)
159    *
160    *    <ja>@Beanp</ja>(<js>"foo"</js>)
161    * </p>
162    */
163   String value() default "";
164
165   /**
166    * Identifies a specialized class type for the property.
167    *
168    * <p>
169    * Normally this can be inferred through reflection of the field type or getter return type.
170    * However, you'll want to specify this value if you're parsing beans where the bean property class is an interface
171    * or abstract class to identify the bean type to instantiate.
172    * Otherwise, you may cause an {@link InstantiationException} when trying to set these fields.
173    *
174    * <p>
175    * This property must denote a concrete bean class with a no-arg constructor.
176    *
177    * <h5 class='section'>Example:</h5>
178    * <p class='bcode w800'>
179    *    <jk>public class</jk> MyBean {
180    *
181    *       <jc>// Identify concrete map type.</jc>
182    *       <ja>@Beanp</ja>(type=HashMap.<jk>class</jk>)
183    *       <jk>public</jk> Map <jf>p1</jf>;
184    *    }
185    * </p>
186    *
187    * <p>
188    * This annotation can also be used on private fields of a property like so:
189    *
190    * <h5 class='section'>Example:</h5>
191    * <p class='bcode w800'>
192    *    <jk>public class</jk> MyBean {
193    *
194    *       <ja>@Beanp</ja>(type=HashMap.<jk>class</jk>)
195    *       <jk>private</jk> Map <jf>p1</jf>;
196    *
197    *       <jk>public</jk> Map getP1() {
198    *          <jk>return</jk> <jf>p1</jf>;
199    *       }
200    *    }
201    * </p>
202    */
203   Class<?> type() default Object.class;
204
205   /**
206    * For bean properties of maps and collections, this annotation can be used to identify the class types of the
207    * contents of the bean property object when the generic parameter types are interfaces or abstract classes.
208    *
209    * <h5 class='section'>Example:</h5>
210    * <p class='bcode w800'>
211    *    <jk>public class</jk> MyBean {
212    *
213    *       <jc>// Identify concrete map type with String keys and Integer values.</jc>
214    *       <ja>@Beanp</ja>(type=HashMap.<jk>class</jk>, params={String.<jk>class</jk>,Integer.<jk>class</jk>})
215    *       <jk>public</jk> Map <jf>p1</jf>;
216    *    }
217    * </p>
218    *
219    * <p>
220    * This annotation can also be used on private fields of a property like so:
221    *
222    * <h5 class='section'>Example:</h5>
223    * <p class='bcode w800'>
224    *    <jk>public class</jk> MyBean {
225    *
226    *       <ja>@Beanp</ja>(type=HashMap.<jk>class</jk>, params={String.<jk>class</jk>,Integer.<jk>class</jk>})
227    *       <jk>private</jk> Map <jf>p1</jf>;
228    *
229    *       <jk>public</jk> Map getP1() {
230    *          <jk>return</jk> <jf>p1</jf>;
231    *       }
232    *    }
233    * </p>
234    */
235   Class<?>[] params() default {};
236
237   /**
238    * Used to limit which child properties are rendered by the serializers.
239    *
240    * <p>
241    * Can be used on any of the following bean property types:
242    * <ul class='spaced-list'>
243    *    <li>Beans - Only render the specified properties of the bean.
244    *    <li>Maps - Only render the specified entries in the map.
245    *    <li>Bean/Map arrays - Same, but applied to each element in the array.
246    *    <li>Bean/Map collections - Same, but applied to each element in the collection.
247    * </ul>
248    *
249    * <h5 class='section'>Example:</h5>
250    * <p class='bcode w800'>
251    *    <jk>public class</jk> MyClass {
252    *
253    *       <jc>// Only render 'f1' when serializing this bean property.</jc>
254    *       <ja>@Beanp</ja>(bpi=<js>"f1"</js>)
255    *       <jk>public</jk> MyChildClass x1 = <jk>new</jk> MyChildClass();
256    *    }
257    *
258    *    <jk>public class</jk> MyChildClass {
259    *       <jk>public int</jk> f1 = 1;
260    *       <jk>public int</jk> f2 = 2;
261    *    }
262    *
263    *    <jc>// Renders "{x1:{f1:1}}"</jc>
264    *    String json = JsonSerializer.<jsf>DEFAULT</jsf>.serialize(<jk>new</jk> MyClass());
265    * </p>
266    *
267    * <p>
268    * This annotation can also be used on private fields of a property like so:
269    *
270    * <h5 class='section'>Example:</h5>
271    * <p class='bcode w800'>
272    *    <jk>public class</jk> MyBean {
273    *
274    *       <ja>@Beanp</ja>(bpi=<js>"f1"</js>)
275    *       <jk>private</jk> MyChildClass <jf>x1</jf>;
276    *
277    *       <jk>public</jk> MyChildClass getX1() {
278    *          <jk>return</jk> <jf>x1</jf>;
279    *       }
280    *    }
281    * </p>
282    */
283   String bpi() default "";
284
285   /**
286    * Bean dictionary.
287    *
288    * <p>
289    * The list of classes that make up the bean dictionary this bean property.
290    *
291    * <ul class='seealso'>
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<?>[] dictionary() 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>@Beanp</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>@Beanp</ja>(format=<js>"$%.2f"</js>)
324    *       <jk>private float</jk> <jf>price</jf>;
325    *
326    *       <jk>public float</jk> getPrice() {
327    *          <jk>return</jk> <jf>price</jf>;
328    *       }
329    *    }
330    * </p>
331    */
332   String format() default "";
333
334   /**
335    * Identifies a property as read-only.
336    *
337    * <p>
338    * Serializers will serialize such properties as usual, but parsers will silently ignore them.
339    *
340    * <h5 class='section'>Example:</h5>
341    * <p class='bcode w800'>
342    *    <jk>public class</jk> MyBean {
343    *       <ja>@Beanp</ja>(ro=<js>"true"</js>)
344    *       <jk>public float</jk> <jf>price</jf>;
345    *    }
346    * </p>
347    *
348    * <ul class='seealso'>
349    *    <li class='jf'>{@link BeanContext#BEAN_bpro}
350    * </ul>
351    */
352   String ro() default "";
353
354   /**
355    * Identifies a property as write-only.
356    *
357    * <p>
358    * Parsers will parse such properties as usual, but serializers will silently ignore them.
359    *
360    * <h5 class='section'>Example:</h5>
361    * <p class='bcode w800'>
362    *    <jk>public class</jk> MyBean {
363    *       <ja>@Beanp</ja>(wo=<js>"true"</js>)
364    *       <jk>public float</jk> <jf>price</jf>;
365    *    }
366    * </p>
367    *
368    * <ul class='seealso'>
369    *    <li class='jf'>{@link BeanContext#BEAN_bpwo}
370    * </ul>
371    */
372   String wo() default "";
373}