All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.annotation.Bean 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.beans.*;
import java.lang.annotation.*;
import java.util.*;

import org.apache.juneau.*;
import org.apache.juneau.swap.*;

/**
 * Used to tailor how beans get interpreted by the framework.
 *
 * 

* Can be used in the following locations: *

    *
  • Bean classes and parent interfaces. *
  • @Rest-annotated classes and @RestOp-annotated methods when an {@link #on()} value is specified. *
* *
See Also:
*/ @Documented @Target({METHOD,TYPE}) @Retention(RUNTIME) @Inherited @Repeatable(BeanAnnotation.Array.class) @ContextApply(BeanAnnotation.Applier.class) public @interface Bean { /** * Bean dictionary. * *

* The list of classes that make up the bean dictionary for all properties in this class and all subclasses. * *

See Also:
    *
  • {@link Beanp#dictionary()} *
  • {@link BeanConfig#dictionary()} *
  • {@link BeanConfig#dictionary_replace()} *
  • {@link org.apache.juneau.BeanContext.Builder#beanDictionary(Class...)} *
* * @return The annotation value. */ Class[] dictionary() default {}; /** * POJO example. * *

* Specifies an example of the specified class in Simplified JSON format. * *

* Examples are used in cases such as POJO examples in Swagger documents. * *

Example:
*

* @Bean(example="{foo:'bar'}") * public class MyClass {...} *

* *
Notes:
    *
  • * Setting applies to specified class and all subclasses. *
  • * Keys are the class of the example. *
    Values are JSON 5 representation of that class. *
  • * POJO examples can also be defined on classes via the following: *
      *
    • A static field annotated with {@link Example @Example}. *
    • A static method annotated with {@link Example @Example} with zero arguments or one {@link BeanSession} argument. *
    • A static method with name example with no arguments or one {@link BeanSession} argument. *
    *
  • * Supports VarResolver.DEFAULT (e.g. "$C{myConfigVar}"). *
* *
See Also:
    *
  • {@link Example} *
* * @return The annotation value. */ String example() default ""; /** * Bean property excludes. * *

* Specifies a list of properties that should be excluded from {@link BeanMap#entrySet()}. * *

Example:
*

* // Exclude the 'city' and 'state' properties from the Address class. * @Bean(excludeProperties="city,state"}) * public class Address {...} *

* *
Notes:
    *
  • * {@link #xp()} is a shortened synonym for this value. *
* *
See Also:
    *
  • {@link org.apache.juneau.BeanContext.Builder#beanPropertiesExcludes(Class, String)} *
  • {@link org.apache.juneau.BeanContext.Builder#beanPropertiesExcludes(String, String)} *
  • {@link org.apache.juneau.BeanContext.Builder#beanPropertiesExcludes(Map)} *
* * @return The annotation value. */ String excludeProperties() default ""; /** * Find fluent setters. * *

* When true, fluent setters will be detected on beans. * *

Example:
*

* @Bean(findFluentSetters=true) * public class MyBean { * public int getId() {...} * public MyBean id(int id) {...} * } *

* *

* Fluent setters must have the following attributes: *

    *
  • Public. *
  • Not static. *
  • Take in one parameter. *
  • Return the bean itself. *
* *
See Also:
    *
  • {@link BeanConfig#findFluentSetters()} *
  • {@link org.apache.juneau.BeanContext.Builder#findFluentSetters()} *
* * @return The annotation value. */ boolean findFluentSetters() default false; /** * Implementation class. * *

* For interfaces and abstract classes this method can be used to specify an implementation class for the * interface/abstract class so that instances of the implementation class are used when instantiated (e.g. during a * parse). * *

Example:
*

* @Bean(implClass=MyInterfaceImpl.class) * public class MyInterface {...} *

* * @return The annotation value. */ Class implClass() default void.class; /** * Bean property interceptor. * *

* Bean interceptors can be used to intercept calls to getters and setters and alter their values in transit. * *

See Also:
    *
  • {@link BeanInterceptor} *
* * @return The annotation value. */ Class> interceptor() default BeanInterceptor.Void.class; /** * Identifies a class to be used as the interface class for this and all subclasses. * *

* When specified, only the list of properties defined on the interface class will be used during serialization. * Additional properties on subclasses will be ignored. * *

* // Parent class * @Bean(interfaceClass=A.class) * public abstract class A { * public String f0 = "f0"; * } * * // Sub class * public class A1 extends A { * public String f1 = "f1"; * } * * // Produces "{f0:'f0'}" * String json = Json5Serializer.DEFAULT.serialize(new A1()); *

* *

* Note that this annotation can be used on the parent class so that it filters to all child classes, * or can be set individually on the child classes. * * @return The annotation value. */ Class interfaceClass() default void.class; /** * Dynamically apply this annotation to the specified classes. * *

* Used in conjunction with {@link org.apache.juneau.BeanContext.Builder#applyAnnotations(Class...)} to dynamically apply an annotation to an existing class. * It is ignored when the annotation is applied directly to classes. * *

Valid patterns:
*
    *
  • Classes: *
      *
    • Fully qualified: *
        *
      • "com.foo.MyClass" *
      *
    • Fully qualified inner class: *
        *
      • "com.foo.MyClass$Inner1$Inner2" *
      *
    • Simple: *
        *
      • "MyClass" *
      *
    • Simple inner: *
        *
      • "MyClass$Inner1$Inner2" *
      • "Inner1$Inner2" *
      • "Inner2" *
      *
    *
  • A comma-delimited list of anything on this list. *
* *
See Also:
* * @return The annotation value. */ String[] on() default {}; /** * Dynamically apply this annotation to the specified classes. * *

* Identical to {@link #on()} except allows you to specify class objects instead of a strings. * *

See Also:
* * @return The annotation value. */ Class[] onClass() default {}; /** * Synonym for {@link #properties()}. * * @return The annotation value. */ String p() default ""; /** * Bean property includes. * *

* The set and order of names of properties associated with a bean class. * *

* The order specified is the same order that the entries will be returned by the {@link BeanMap#entrySet()} and * related methods. * *

* This value is entirely optional if you simply want to expose all the getters and public fields on * a class as bean properties. *
However, it's useful if you want certain getters to be ignored or you want the properties to be * serialized in a particular order. *
Note that on IBM JREs, the property order is the same as the order in the source code, * whereas on Oracle JREs, the order is entirely random. * *

Example:
*

* // Address class with only street/city/state properties (in that order). * @Bean(properties="street,city,state") * public class Address {...} *

* *
Notes:
    *
  • * {@link #p()} is a shortened synonym for this value. *
* *
See Also:
    *
  • {@link org.apache.juneau.BeanContext.Builder#beanProperties(Class, String)} *
  • {@link org.apache.juneau.BeanContext.Builder#beanProperties(String, String)} *
  • {@link org.apache.juneau.BeanContext.Builder#beanProperties(Map)} *
* * @return The annotation value. */ String properties() default ""; /** * Associates a {@link PropertyNamer} with this bean to tailor the names of the bean properties. * *

* Property namers are used to transform bean property names from standard form to some other form. * *

Example:
*

* // Define a class with dashed-lowercase property names. * @Bean(propertyNamer=PropertyNamerDashedLC.class) * public class MyBean {...} *

* *
See Also:
    *
  • {@link org.apache.juneau.BeanContext.Builder#propertyNamer(Class)} *
* * @return The annotation value. */ Class propertyNamer() default PropertyNamer.Void.class; /** * Read-only bean properties. * *

* Specifies one or more properties on a bean that are read-only despite having valid getters. * Serializers will serialize such properties as usual, but parsers will silently ignore them. * *

Example:
*

* // Exclude the 'city' and 'state' properties from being parsed, but not serialized. * @Bean(readOnlyProperties="city,state"}) * public class Address {...} *

* *
Notes:
    *
  • * {@link #ro()} is a shortened synonym for this value. *
* *
See Also:
    *
  • {@link org.apache.juneau.BeanContext.Builder#beanPropertiesReadOnly(Class, String)} *
  • {@link org.apache.juneau.BeanContext.Builder#beanPropertiesReadOnly(String, String)} *
  • {@link org.apache.juneau.BeanContext.Builder#beanPropertiesReadOnly(Map)} *
* * @return The annotation value. */ String readOnlyProperties() default ""; /** * Synonym for {@link #readOnlyProperties()}. * * @return The annotation value. */ String ro() default ""; /** * Sort bean properties in alphabetical order. * *

* When true, all bean properties will be serialized and access in alphabetical order. *
Otherwise, the natural order of the bean properties is used which is dependent on the JVM vendor. * *

Example:
*

* // Sort bean properties alphabetically during serialization. * @Bean(sort=true) * public class MyBean {...} *

* *
See Also:
    *
  • {@link org.apache.juneau.BeanContext.Builder#sortProperties()} *
* * @return The annotation value. */ boolean sort() default false; /** * Identifies a stop class for the annotated class. * *

* Identical in purpose to the stop class specified by {@link Introspector#getBeanInfo(Class, Class)}. * Any properties in the stop class or in its base classes will be ignored during analysis. * *

* For example, in the following class hierarchy, instances of C3 will include property p3, * but not p1 or p2. *

* public class C1 { * public int getP1(); * } * * public class C2 extends C1 { * public int getP2(); * } * * @Bean(stopClass=C2.class) * public class C3 extends C2 { * public int getP3(); * } *

* * @return The annotation value. */ Class stopClass() default void.class; /** * An identifying name for this class. * *

* The name is used to identify the class type during parsing when it cannot be inferred through reflection. *
For example, if a bean property is of type Object, then the serializer will add the name to the * output so that the class can be determined during parsing. * *

* It is also used to specify element names in XML. * *

Example:
*

* // Use _type='mybean' to identify this bean. * @Bean(typeName="mybean") * public class MyBean {...} *

* *
See Also:
    *
  • {@link org.apache.juneau.BeanContext.Builder#beanDictionary(Class...)} *
* * @return The annotation value. */ String typeName() default ""; /** * The property name to use for representing the type name. * *

* This can be used to override the name used for the "_type" property used by the {@link #typeName()} setting. * *

* The default value if not specified is "_type" . * *

Example:
*

* // Use 'type' instead of '_type' for bean names. * @Bean(typePropertyName="type") * public class MyBean {...} *

* *
See Also:
    *
  • {@link BeanConfig#typePropertyName()} *
  • {@link org.apache.juneau.BeanContext.Builder#typePropertyName(String)} *
* * @return The annotation value. */ String typePropertyName() default ""; /** * Synonym for {@link #writeOnlyProperties()}. * * @return The annotation value. */ String wo() default ""; /** * Write-only bean properties. * *

* Specifies one or more properties on a bean that are write-only despite having valid setters. * Parsers will parse such properties as usual, but serializers will silently ignore them. * *

Example:
*

* // Exclude the 'city' and 'state' properties from being serialized, but not parsed. * @Bean(writeOnlyProperties="city,state"}) * public class Address {...} *

* *
Notes:
    *
  • * {@link #wo()} is a shortened synonym for this value. *
* *
See Also:
    *
  • {@link org.apache.juneau.BeanContext.Builder#beanPropertiesWriteOnly(Class, String)} *
  • {@link org.apache.juneau.BeanContext.Builder#beanPropertiesWriteOnly(String, String)} *
  • {@link org.apache.juneau.BeanContext.Builder#beanPropertiesWriteOnly(Map)} *
* * @return The annotation value. */ String writeOnlyProperties() default ""; /** * Synonym for {@link #excludeProperties()}. * * @return The annotation value. */ String xp() default ""; }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy