1 package com.fasterxml.jackson.databind.annotation; 2 3 import java.lang.annotation.*; 4 5 /** 6 * Annotation used to configure details of a Builder class: 7 * instances of which are used as Builders for deserialized 8 * POJO values, instead of POJOs being instantiated using 9 * constructors or factory methods. 10 * Note that this annotation is NOT used to define what is 11 * the Builder class for a POJO: rather, this is determined 12 * by {@link JsonDeserialize#builder} property of {@link JsonDeserialize}. 13 *<p> 14 * Annotation is typically used if the naming convention 15 * of a Builder class is different from defaults: 16 *<ul> 17 * <li>By default, setters are expected to have names like `withName()` (for property "name"); 18 * override by {@link #withPrefix()} property. 19 * </li> 20 * </ul> 21 *<p> 22 * In addition to configuration using this annotation, note that many other configuration 23 * annotations are also applied to Builders, for example 24 * {@link com.fasterxml.jackson.annotation.JsonIgnoreProperties} can be used to ignore 25 * "unknown" properties. 26 * 27 * @since 2.0 28 */ 29 @Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE}) 30 @Retention(RetentionPolicy.RUNTIME) 31 @com.fasterxml.jackson.annotation.JacksonAnnotation 32 public @interface JsonPOJOBuilder 33 { 34 /** 35 * @since 2.9 36 */ 37 public final static String DEFAULT_BUILD_METHOD = "build"; 38 39 /** 40 * @since 2.9 41 */ 42 public final static String DEFAULT_WITH_PREFIX = "with"; 43 44 /** 45 * Property to use for re-defining which zero-argument method 46 * is considered the actual "build-method": method called after 47 * all data has been bound, and the actual instance needs to 48 * be instantiated. 49 *<p> 50 * Default value is "build". 51 */ buildMethodName()52 public String buildMethodName() default DEFAULT_BUILD_METHOD; 53 54 /** 55 * Property used for (re)defining name prefix to use for 56 * auto-detecting "with-methods": methods that are similar to 57 * "set-methods" (in that they take an argument), but that 58 * may also return the new builder instance to use 59 * (which may be 'this', or a new modified builder instance). 60 * Note that in addition to this prefix, it is also possible 61 * to use {@link com.fasterxml.jackson.annotation.JsonProperty} 62 * annotation to indicate "with-methods" (as well as 63 * {@link com.fasterxml.jackson.annotation.JsonSetter}). 64 *<p> 65 * Default value is "with", so that method named "withValue()" 66 * would be used for binding JSON property "value" (using type 67 * indicated by the argument; or one defined with annotations. 68 */ withPrefix()69 public String withPrefix() default DEFAULT_WITH_PREFIX; 70 71 /* 72 /********************************************************** 73 /* Helper classes 74 /********************************************************** 75 */ 76 77 /** 78 * Simple value container for containing values read from 79 * {@link JsonPOJOBuilder} annotation instance. 80 */ 81 public class Value 82 { 83 public final String buildMethodName; 84 public final String withPrefix; 85 Value(JsonPOJOBuilder ann)86 public Value(JsonPOJOBuilder ann) { 87 this(ann.buildMethodName(), ann.withPrefix()); 88 } 89 Value(String buildMethodName, String withPrefix)90 public Value(String buildMethodName, String withPrefix) 91 { 92 this.buildMethodName = buildMethodName; 93 this.withPrefix = withPrefix; 94 } 95 } 96 } 97