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.beans.*; 019import java.lang.annotation.*; 020 021import org.apache.juneau.*; 022import org.apache.juneau.transform.*; 023 024/** 025 * Used to tailor how beans get interpreted by the framework. 026 * 027 * <p> 028 * This annotation can be applied to classes and interfaces. 029 * 030 * <ul class='seealso'> 031 * <li class='link'>{@doc juneau-marshall.Transforms.BeanAnnotation} 032 * </ul> 033 */ 034@Documented 035@Target(TYPE) 036@Retention(RUNTIME) 037@Inherited 038public @interface Bean { 039 040 /** 041 * Bean dictionary. 042 * 043 * <p> 044 * The list of classes that make up the bean dictionary for all properties in this class and all subclasses. 045 * 046 * <ul class='seealso'> 047 * <li class='jf'>{@link BeanContext#BEAN_beanDictionary} 048 * </ul> 049 * @deprecated Use {@link #dictionary()}. 050 */ 051 @Deprecated 052 Class<?>[] beanDictionary() default {}; 053 054 /** 055 * Bean property includes. 056 * 057 * <p> 058 * The set and order of names of properties associated with a bean class. 059 * 060 * <p> 061 * The order specified is the same order that the entries will be returned by the {@link BeanMap#entrySet()} and 062 * related methods. 063 * 064 * <p> 065 * This value is entirely optional if you simply want to expose all the getters and public fields on 066 * a class as bean properties. 067 * <br>However, it's useful if you want certain getters to be ignored or you want the properties to be 068 * serialized in a particular order. 069 * <br>Note that on IBM JREs, the property order is the same as the order in the source code, 070 * whereas on Oracle JREs, the order is entirely random. 071 * 072 * <h5 class='section'>Example:</h5> 073 * <p class='bcode w800'> 074 * <jc>// Address class with only street/city/state properties (in that order).</jc> 075 * <ja>@Bean</ja>(bpi=<js>"street,city,state"</js>) 076 * <jk>public class</jk> Address {...} 077 * </p> 078 * 079 * <ul class='seealso'> 080 * <li class='jf'>{@link BeanContext#BEAN_bpi} 081 * </ul> 082 */ 083 String bpi() default ""; 084 085 /** 086 * Bean property excludes. 087 * 088 * <p> 089 * Specifies a list of properties that should be excluded from {@link BeanMap#entrySet()}. 090 * 091 * <h5 class='section'>Example:</h5> 092 * <p class='bcode w800'> 093 * <jc>// Exclude the 'city' and 'state' properties from the Address class.</jc> 094 * <ja>@Bean</ja>(bpx=<js>"city,state"</js>}) 095 * <jk>public class</jk> Address {...} 096 * </p> 097 * 098 * <ul class='seealso'> 099 * <li class='jf'>{@link BeanContext#BEAN_bpx} 100 * </ul> 101 */ 102 String bpx() default ""; 103 104 /** 105 * Read-only bean properties. 106 * 107 * <p> 108 * Specifies one or more properties on a bean that are read-only despite having valid getters. 109 * Serializers will serialize such properties as usual, but parsers will silently ignore them. 110 * 111 * <h5 class='section'>Example:</h5> 112 * <p class='bcode w800'> 113 * <jc>// Exclude the 'city' and 'state' properties from being parsed, but not serialized.</jc> 114 * <ja>@Bean</ja>(bpro=<js>"city,state"</js>}) 115 * <jk>public class</jk> Address {...} 116 * </p> 117 * 118 * <ul class='seealso'> 119 * <li class='jf'>{@link BeanContext#BEAN_bpro} 120 * </ul> 121 */ 122 String bpro() default ""; 123 124 /** 125 * Write-only bean properties. 126 * 127 * <p> 128 * Specifies one or more properties on a bean that are write-only despite having valid setters. 129 * Parsers will parse such properties as usual, but serializers will silently ignore them. 130 * 131 * <h5 class='section'>Example:</h5> 132 * <p class='bcode w800'> 133 * <jc>// Exclude the 'city' and 'state' properties from being serialized, but not parsed.</jc> 134 * <ja>@Bean</ja>(bpro=<js>"city,state"</js>}) 135 * <jk>public class</jk> Address {...} 136 * </p> 137 * 138 * <ul class='seealso'> 139 * <li class='jf'>{@link BeanContext#BEAN_bpwo} 140 * </ul> 141 */ 142 String bpwo() default ""; 143 144 /** 145 * Bean dictionary. 146 * 147 * <p> 148 * The list of classes that make up the bean dictionary for all properties in this class and all subclasses. 149 * 150 * <ul class='seealso'> 151 * <li class='jf'>{@link BeanContext#BEAN_beanDictionary} 152 * </ul> 153 */ 154 Class<?>[] dictionary() default {}; 155 156 /** 157 * Specifies a list of properties that should be excluded from {@link BeanMap#entrySet()}. 158 * 159 * @deprecated Use {@link #bpx()} 160 */ 161 @Deprecated String excludeProperties() default ""; 162 163 /** 164 * Find fluent setters. 165 * 166 * <p> 167 * When <jk>true</jk>, fluent setters will be detected on beans. 168 * 169 * <p> 170 * Fluent setters 171 * 172 * <h5 class='section'>Example:</h5> 173 * <p class='bcode w800'> 174 * <ja>@Bean</ja>(fluentSetters=<jk>true</jk>) 175 * <jk>public class</jk> MyBean { 176 * <jk>public int</jk> getId() {...} 177 * <jk>public</jk> MyBean id(<jk>int</jk> id) {...} 178 * } 179 * </p> 180 * 181 * <p> 182 * Fluent setters must have the following attributes: 183 * <ul> 184 * <li>Public. 185 * <li>Not static. 186 * <li>Take in one parameter. 187 * <li>Return the bean itself. 188 * </ul> 189 * 190 * <ul class='seealso'> 191 * <li class='jf'>{@link BeanContext#BEAN_fluentSetters} 192 * </ul> 193 */ 194 boolean fluentSetters() default false; 195 196 /** 197 * Identifies a class to be used as the interface class for this and all subclasses. 198 * 199 * <p> 200 * When specified, only the list of properties defined on the interface class will be used during serialization. 201 * Additional properties on subclasses will be ignored. 202 * 203 * <p class='bcode w800'> 204 * <jc>// Parent class</jc> 205 * <ja>@Bean</ja>(interfaceClass=A.<jk>class</jk>) 206 * <jk>public abstract class</jk> A { 207 * <jk>public</jk> String <jf>f0</jf> = <js>"f0"</js>; 208 * } 209 * 210 * <jc>// Sub class</jc> 211 * <jk>public class</jk> A1 <jk>extends</jk> A { 212 * <jk>public</jk> String <jf>f1</jf> = <js>"f1"</js>; 213 * } 214 * 215 * <jc>// Produces "{f0:'f0'}"</jc> 216 * String json = SimpleJsonSerializer.<jsf>DEFAULT</jsf>.serialize(<jk>new</jk> A1()); 217 * </p> 218 * 219 * <p> 220 * Note that this annotation can be used on the parent class so that it filters to all child classes, 221 * or can be set individually on the child classes. 222 * 223 * <ul class='seealso'> 224 * <li class='jf'>{@link BeanContext#BEAN_beanFilters} 225 * </ul> 226 */ 227 Class<?> interfaceClass() default Object.class; 228 229 /** 230 * The set and order of names of properties associated with a bean class. 231 * 232 * @deprecated Use {@link #bpi()} 233 */ 234 @Deprecated String properties() default ""; 235 236 /** 237 * Property filter. 238 * 239 * <p> 240 * Property filters can be used to intercept calls to getters and setters and alter their values in transit. 241 * 242 * <ul class='seealso'> 243 * <li class='jc'>{@link PropertyFilter} 244 * </ul> 245 */ 246 Class<? extends PropertyFilter> propertyFilter() default PropertyFilter.class; 247 248 /** 249 * Associates a {@link PropertyNamer} with this bean to tailor the names of the bean properties. 250 * 251 * <p> 252 * Property namers are used to transform bean property names from standard form to some other form. 253 * 254 * <h5 class='section'>Example:</h5> 255 * <p class='bcode w800'> 256 * <jc>// Define a class with dashed-lowercase property names.</jc> 257 * <ja>@Bean</ja>(propertyNamer=PropertyNamerDashedLC.<jk>class</jk>) 258 * <jk>public class</jk> MyBean {...} 259 * </p> 260 * 261 * <ul class='seealso'> 262 * <li class='jf'>{@link BeanContext#BEAN_propertyNamer} 263 * </ul> 264 */ 265 Class<? extends PropertyNamer> propertyNamer() default PropertyNamerDefault.class; 266 267 /** 268 * Sort bean properties in alphabetical order. 269 * 270 * <p> 271 * When <jk>true</jk>, all bean properties will be serialized and access in alphabetical order. 272 * <br>Otherwise, the natural order of the bean properties is used which is dependent on the JVM vendor. 273 * 274 * <h5 class='section'>Example:</h5> 275 * <p class='bcode w800'> 276 * <jc>// Sort bean properties alphabetically during serialization.</jc> 277 * <ja>@Bean</ja>(sort=<jk>true</jk>) 278 * <jk>public class</jk> MyBean {...} 279 * </p> 280 * 281 * <ul class='seealso'> 282 * <li class='jf'>{@link BeanContext#BEAN_sortProperties} 283 * </ul> 284 */ 285 boolean sort() default false; 286 287 /** 288 * Identifies a stop class for the annotated class. 289 * 290 * <p> 291 * Identical in purpose to the stop class specified by {@link Introspector#getBeanInfo(Class, Class)}. 292 * Any properties in the stop class or in its base classes will be ignored during analysis. 293 * 294 * <p> 295 * For example, in the following class hierarchy, instances of <c>C3</c> will include property <c>p3</c>, 296 * but not <c>p1</c> or <c>p2</c>. 297 * <p class='bcode w800'> 298 * <jk>public class</jk> C1 { 299 * <jk>public int</jk> getP1(); 300 * } 301 * 302 * <jk>public class</jk> C2 <jk>extends</jk> C1 { 303 * <jk>public int</jk> getP2(); 304 * } 305 * 306 * <ja>@Bean</ja>(stopClass=C2.<jk>class</jk>) 307 * <jk>public class</jk> C3 <jk>extends</jk> C2 { 308 * <jk>public int</jk> getP3(); 309 * } 310 * </p> 311 */ 312 Class<?> stopClass() default Object.class; 313 314 /** 315 * An identifying name for this class. 316 * 317 * <p> 318 * The name is used to identify the class type during parsing when it cannot be inferred through reflection. 319 * <br>For example, if a bean property is of type <c>Object</c>, then the serializer will add the name to the 320 * output so that the class can be determined during parsing. 321 * 322 * <p> 323 * It is also used to specify element names in XML. 324 * 325 * <h5 class='section'>Example:</h5> 326 * <p class='bcode w800'> 327 * <jc>// Use _type='mybean' to identify this bean.</jc> 328 * <ja>@Bean</ja>(typeName=<js>"mybean"</js>) 329 * <jk>public class</jk> MyBean {...} 330 * </p> 331 * 332 * <ul class='seealso'> 333 * <li class='jf'>{@link BeanContext#BEAN_beanDictionary} 334 * </ul> 335 */ 336 String typeName() default ""; 337 338 /** 339 * The property name to use for representing the type name. 340 * 341 * <p> 342 * This can be used to override the name used for the <js>"_type"</js> property used by the {@link #typeName()} setting. 343 * 344 * <p> 345 * The default value if not specified is <js>"_type"</js> . 346 * 347 * <h5 class='section'>Example:</h5> 348 * <p class='bcode w800'> 349 * <jc>// Use 'type' instead of '_type' for bean names.</jc> 350 * <ja>@Bean</ja>(typePropertyName=<js>"type"</js>) 351 * <jk>public class</jk> MyBean {...} 352 * </p> 353 * 354 * <ul class='seealso'> 355 * <li class='jf'>{@link BeanContext#BEAN_beanTypePropertyName} 356 * </ul> 357 */ 358 String typePropertyName() default ""; 359}