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 * Maps constructor arguments to property names on beans with read-only properties. 024 * 025 * <p> 026 * This annotation can be used in the case of beans with properties whose values can only be set by passing them in 027 * through a constructor on the class. 028 * <br>Since method parameter names are lost during compilation, this annotation essentially redefines them so that they 029 * are available at runtime. 030 * 031 * <p> 032 * This annotation can only be applied to constructors and can only be applied to one constructor per class. 033 * 034 * <p> 035 * When present, bean instantiation is delayed until the call to {@link BeanMap#getBean()}. 036 * Until then, bean property values are stored in a local cache until <c>getBean()</c> is called. 037 * Because of this additional caching step, parsing into read-only beans tends to be slower and use more memory than 038 * parsing into beans with writable properties. 039 * 040 * <p> 041 * Attempting to call {@link BeanMap#put(String,Object)} on a read-only property after calling {@link BeanMap#getBean()} 042 * will result in a {@link BeanRuntimeException} being thrown. 043 * Multiple calls to {@link BeanMap#getBean()} will return the same bean instance. 044 * 045 * <ul class='seealso'> 046 * <li class='link'>{@doc juneau-marshall.Transforms.BeancAnnotation} 047 * </ul> 048 * 049 * @deprecated Use {@link Beanc} 050 */ 051@Documented 052@Target(CONSTRUCTOR) 053@Retention(RUNTIME) 054@Inherited 055@Deprecated 056public @interface BeanConstructor { 057 058 /** 059 * The names of the properties of the constructor arguments. 060 * <p> 061 * The {@link org.apache.juneau.annotation.BeanConstructor @BeanConstructor} annotation is used to 062 * map constructor arguments to property names on bean with read-only properties. 063 * <br>Since method parameter names are lost during compilation, this annotation essentially redefines 064 * them so that they are available at runtime. 065 * </p> 066 * <p> 067 * The definition of a read-only bean is a bean with properties with only getters, like shown below: 068 * </p> 069 * <p class='bpcode w800'> 070 * <jc>// Our read-only bean.</jc> 071 * <jk>public class</jk> Person { 072 * <jk>private final</jk> String <jf>name</jf>; 073 * <jk>private final int</jk> <jf>age</jf>; 074 * 075 * <ja>@BeanConstructor</ja>(properties=<js>"name,age"</js>) 076 * <jk>public</jk> Person(String name, <jk>int</jk> age) { 077 * <jk>this</jk>.<jf>name</jf> = name; 078 * <jk>this</jk>.<jf>age</jf> = age; 079 * } 080 * 081 * <jc>// Read only properties.</jc> 082 * <jc>// Getters, but no setters.</jc> 083 * 084 * <jk>public</jk> String getName() { 085 * <jk>return</jk> <jf>name</jf>; 086 * } 087 * 088 * <jk>public int</jk> getAge() { 089 * <jk>return</jk> <jf>age</jf>; 090 * } 091 * } 092 * </p> 093 * <p class='bpcode w800'> 094 * <jc>// Parsing into a read-only bean.</jc> 095 * String json = <js>"{name:'John Smith',age:45}"</js>; 096 * Person p = JsonParser.<jsf>DEFAULT</jsf>.parse(json); 097 * String name = p.getName(); <jc>// "John Smith"</jc> 098 * <jk>int</jk> age = p.getAge(); <jc>// 45</jc> 099 * </p> 100 * <p> 101 * Note that the {@link Name @Name} annotation can also be used to identify bean property names on constructor 102 * arguments. If neither this annotation or {@link Name @Name} is used, then we try to get the property names 103 * from the parameter names if they are available in the bytecode. 104 * </p> 105 */ 106 String properties() default ""; 107}