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<String,Object> extraStuff = <jk>new</jk> LinkedHashMap<String,Object>(); 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<String,Object> 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<String,Object> getMyExtraStuff() { 093 * ... 094 * } 095 * } 096 * 097 * <jc>// Option #4 - Getter, setter, and extra-keys method . 098 * // Define a method that returns a Collection<String> 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<String> 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<String,List<String>> 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<String,Calendar> 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 Class<?> type() default Object.class; 187 188 /** 189 * For bean properties of maps and collections, this annotation can be used to identify the class types of the 190 * contents of the bean property object when the generic parameter types are interfaces or abstract classes. 191 * 192 * <h5 class='section'>Example:</h5> 193 * <p class='bcode w800'> 194 * <jk>public class</jk> MyBean { 195 * 196 * <jc>// Identify concrete map type with String keys and Integer values.</jc> 197 * <ja>@BeanProperty</ja>(type=HashMap.<jk>class</jk>, params={String.<jk>class</jk>,Integer.<jk>class</jk>}) 198 * <jk>public</jk> Map <jf>p1</jf>; 199 * } 200 * </p> 201 */ 202 Class<?>[] params() default {}; 203 204 /** 205 * Used to limit which child properties are rendered by the serializers. 206 * 207 * <p> 208 * Can be used on any of the following bean property types: 209 * <ul class='spaced-list'> 210 * <li>Beans - Only render the specified properties of the bean. 211 * <li>Maps - Only render the specified entries in the map. 212 * <li>Bean/Map arrays - Same, but applied to each element in the array. 213 * <li>Bean/Map collections - Same, but applied to each element in the collection. 214 * </ul> 215 * 216 * <h5 class='section'>Example:</h5> 217 * <p class='bcode w800'> 218 * <jk>public class</jk> MyClass { 219 * 220 * <jc>// Only render 'f1' when serializing this bean property.</jc> 221 * <ja>@BeanProperty</ja>(properties=<js>"f1"</js>) 222 * <jk>public</jk> MyChildClass x1 = <jk>new</jk> MyChildClass(); 223 * } 224 * 225 * <jk>public class</jk> MyChildClass { 226 * <jk>public int</jk> f1 = 1; 227 * <jk>public int</jk> f2 = 2; 228 * } 229 * 230 * <jc>// Renders "{x1:{f1:1}}"</jc> 231 * String json = JsonSerializer.<jsf>DEFAULT</jsf>.serialize(<jk>new</jk> MyClass()); 232 * </p> 233 */ 234 String properties() default ""; 235 236 /** 237 * Bean dictionary. 238 * 239 * <p> 240 * The list of classes that make up the bean dictionary this bean property. 241 * 242 * <h5 class='section'>See Also:</h5> 243 * <ul> 244 * <li class='jf'>{@link BeanContext#BEAN_beanDictionary} 245 * </ul> 246 */ 247 Class<?>[] beanDictionary() default {}; 248 249 /** 250 * Specifies a String format for converting the bean property value to a formatted string. 251 * 252 * <p> 253 * Note that this is usually a one-way conversion during serialization. 254 * 255 * <p> 256 * During parsing, we will attempt to convert the value to the original form by using the 257 * {@link BeanSession#convertToType(Object, Class)} but there is no guarantee that this will succeed. 258 * 259 * <h5 class='section'>Example:</h5> 260 * <p class='bcode w800'> 261 * <ja>@BeanProperty</ja>(format=<js>"$%.2f"</js>) 262 * <jk>public float</jk> <jf>price</jf>; 263 * </p> 264 */ 265 String format() default ""; 266}