| <!-- |
| /*************************************************************************************************************************** |
| * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations under the License. |
| ***************************************************************************************************************************/ |
| --> |
| |
| @Beanc Annotation |
| |
| <p> |
| The {@link oaj.annotation.Beanc @Beanc} annotation is used to |
| map constructor arguments to property names on bean with read-only properties. |
| Since method parameter names are lost during compilation, this annotation essentially redefines |
| them so that they are available at runtime. |
| </p> |
| <p> |
| The definition of a read-only bean is a bean with properties with only getters, like shown below: |
| </p> |
| <p class='bpcode w800'> |
| <jc>// Our read-only bean.</jc> |
| <jk>public class</jk> Person { |
| <jk>private final</jk> String <jf>name</jf>; |
| <jk>private final int</jk> <jf>age</jf>; |
| |
| <ja>@Beanc</ja>(properties=<js>"name,age"</js>}) |
| <jk>public</jk> Person(String name, <jk>int</jk> age) { |
| <jk>this</jk>.<jf>name</jf> = name; |
| <jk>this</jk>.<jf>age</jf> = age; |
| } |
| |
| <jc>// Read only properties.</jc> |
| <jc>// Getters, but no setters.</jc> |
| |
| <jk>public</jk> String getName() { |
| <jk>return</jk> <jf>name</jf>; |
| } |
| |
| <jk>public int</jk> getAge() { |
| <jk>return</jk> <jf>age</jf>; |
| } |
| } |
| </p> |
| <p class='bpcode w800'> |
| <jc>// Parsing into a read-only bean.</jc> |
| String json = <js>"{name:'John Smith',age:45}"</js>; |
| Person p = JsonParser.<jsf>DEFAULT</jsf>.parse(json); |
| String name = p.getName(); <jc>// "John Smith"</jc> |
| <jk>int</jk> age = p.getAge(); <jc>// 45</jc> |
| </p> |
| <p> |
| Beans can also be defined with a combination of read-only and read-write properties. |
| </p> |
| <p> |
| The {@link oaj.annotation.Name @Name} annotation can also be used instead of <c><ja>@Beanc</ja>(properties)</c>: |
| </p> |
| <p class='bpcode w800'> |
| <ja>@Beanc</ja> |
| <jk>public</jk> Person(<ja>@Name</ja>(<js>"name"</js>) String name, <ja>@Name</ja>(<js>"age"</js>) <jk>int</jk> age) { |
| <jk>this</jk>.<jf>name</jf> = name; |
| <jk>this</jk>.<jf>age</jf> = age; |
| } |
| </p> |
| <p> |
| If neither <c><ja>@Beanc</ja>(properties)</c> or <ja>@Name</ja> is used to identify the bean property names, |
| we will try to use the parameter names if they are available in the bytecode. |
| </p> |