![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.annotation.Beanc Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * 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. *
// ***************************************************************************************************************************
package org.apache.juneau.annotation;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import java.lang.annotation.*;
import org.apache.juneau.*;
/**
* Maps constructor arguments to property names on beans with read-only properties.
*
*
* Can be used in the following locations:
*
* - Bean constructors.
*
@Rest -annotated classes and @RestOp -annotated methods when an {@link #on()} value is specified.
*
*
* This annotation can be used in the case of beans with properties whose values can only be set by passing them in
* through a constructor on the class.
*
Since method parameter names are lost during compilation, this annotation essentially redefines them so that they
* are available at runtime.
*
*
* This annotation can only be applied to constructors and can only be applied to one constructor per class.
*
*
* When present, bean instantiation is delayed until the call to {@link BeanMap#getBean()}.
* Until then, bean property values are stored in a local cache until getBean() is called.
* Because of this additional caching step, parsing into read-only beans tends to be slower and use more memory than
* parsing into beans with writable properties.
*
*
* Attempting to call {@link BeanMap#put(String,Object)} on a read-only property after calling {@link BeanMap#getBean()}
* will result in a {@link BeanRuntimeException} being thrown.
* Multiple calls to {@link BeanMap#getBean()} will return the same bean instance.
*
*
See Also:
*/
@Documented
@Target({METHOD,TYPE,CONSTRUCTOR})
@Retention(RUNTIME)
@Inherited
@Repeatable(BeancAnnotation.Array.class)
@ContextApply(BeancAnnotation.Applier.class)
public @interface Beanc {
/**
* Dynamically apply this annotation to the specified constructors.
*
*
* Used in conjunction with {@link org.apache.juneau.BeanContext.Builder#applyAnnotations(Class...)} to dynamically apply an annotation to an existing constructor.
* It is ignored when the annotation is applied directly to constructors.
*
*
* The following example shows this annotation in use:
*
* // Our read-only bean.
* public class Person {
* private final String name ;
* private final int age ;
*
* public Person(String name , int age ) {
* this .name = name ;
* this .age = age ;
* }
*
* // Read only properties.
* // Getters, but no setters.
*
* public String getName() {
* return name ;
* }
*
* public int getAge() {
* return age ;
* }
* }
*
* @BeanConfig
* @Beanc (on="Person(String,int)" , properties="name,age" ))
* public static class MyConfig {}
*
*
* // Parsing into a read-only bean.
* String json = "{name:'John Smith',age:45}" ;
* Person person = JsonParser.DEFAULT .copy().applyAnnotations(MyConfig.class ).build().parse(json );
* String name = person .getName(); // "John Smith"
* int age = person .getAge(); // 45
*
*
* Valid patterns:
*
* - Constructors:
*
* - Fully qualified with args:
*
* "com.foo.MyClass(String,int)"
* "com.foo.MyClass(java.lang.String,int)"
* "com.foo.MyClass()"
*
* - Simple with args:
*
* "MyClass(String,int)"
* "MyClass(java.lang.String,int)"
* "MyClass()"
*
* - Simple inner class:
*
* "MyClass$Inner1$Inner2()"
* "Inner1$Inner2()"
* "Inner2()"
*
*
* - A comma-delimited list of anything on this list.
*
*
* See Also:
*
* @return The annotation value.
*/
String[] on() default {};
/**
* The names of the properties of the constructor arguments.
*
*
* The {@link org.apache.juneau.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.
*
*
* The definition of a read-only bean is a bean with properties with only getters, like shown below:
*
* // Our read-only bean.
* public class Person {
* private final String name ;
* private final int age ;
*
* @Beanc (properties="name,age" )
* public Person(String name , int age ) {
* this .name = name ;
* this .age = age ;
* }
*
* // Read only properties.
* // Getters, but no setters.
*
* public String getName() {
* return name ;
* }
*
* public int getAge() {
* return age ;
* }
* }
*
*
* // Parsing into a read-only bean.
* String json = "{name:'John Smith',age:45}" ;
* Person person = JsonParser.DEFAULT .parse(json );
* String name = person .getName(); // "John Smith"
* int age = person .getAge(); // 45
*
*
* Note that the {@link Name @Name} annotation can also be used to identify bean property names on constructor
* arguments. If neither this annotation or {@link Name @Name} is used, then we try to get the property names
* from the parameter names if they are available in the bytecode.
*
*
* @return The annotation value.
*/
String properties() default "";
}