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.json.annotation;
014
015import static java.lang.annotation.ElementType.*;
016import static java.lang.annotation.RetentionPolicy.*;
017import static org.apache.juneau.internal.ArrayUtils.*;
018
019import java.lang.annotation.*;
020import java.lang.reflect.*;
021
022import org.apache.juneau.*;
023import org.apache.juneau.annotation.*;
024import org.apache.juneau.reflect.*;
025import org.apache.juneau.svl.*;
026
027/**
028 * Utility classes and methods for the {@link Json @Json} annotation.
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.JsonDetails">JSON Details</a>
032 * </ul>
033 */
034public class JsonAnnotation {
035
036   //-----------------------------------------------------------------------------------------------------------------
037   // Static
038   //-----------------------------------------------------------------------------------------------------------------
039
040   /** Default value */
041   public static final Json DEFAULT = create().build();
042
043   /**
044    * Instantiates a new builder for this class.
045    *
046    * @return A new builder object.
047    */
048   public static Builder create() {
049      return new Builder();
050   }
051
052   /**
053    * Instantiates a new builder for this class.
054    *
055    * @param on The targets this annotation applies to.
056    * @return A new builder object.
057    */
058   public static Builder create(Class<?>...on) {
059      return create().on(on);
060   }
061
062   /**
063    * Instantiates a new builder for this class.
064    *
065    * @param on The targets this annotation applies to.
066    * @return A new builder object.
067    */
068   public static Builder create(String...on) {
069      return create().on(on);
070   }
071
072   /**
073    * Creates a copy of the specified annotation.
074    *
075    * @param a The annotation to copy.s
076    * @param r The var resolver for resolving any variables.
077    * @return A copy of the specified annotation.
078    */
079   public static Json copy(Json a, VarResolverSession r) {
080      return
081         create()
082         .on(r.resolve(a.on()))
083         .onClass(a.onClass())
084         .wrapperAttr(r.resolve(a.wrapperAttr()))
085         .build();
086   }
087
088   //-----------------------------------------------------------------------------------------------------------------
089   // Builder
090   //-----------------------------------------------------------------------------------------------------------------
091
092   /**
093    * Builder class.
094    *
095    * <h5 class='section'>See Also:</h5><ul>
096    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
097    * </ul>
098    */
099   public static class Builder extends TargetedAnnotationTMFBuilder {
100
101      String wrapperAttr="";
102
103      /**
104       * Constructor.
105       */
106      protected Builder() {
107         super(Json.class);
108      }
109
110      /**
111       * Instantiates a new {@link Json @Json} object initialized with this builder.
112       *
113       * @return A new {@link Json @Json} object.
114       */
115      public Json build() {
116         return new Impl(this);
117      }
118
119      /**
120       * Sets the {@link Json#wrapperAttr} property on this annotation.
121       *
122       * @param value The new value for this property.
123       * @return This object.
124       */
125      public Builder wrapperAttr(String value) {
126         this.wrapperAttr = value;
127         return this;
128      }
129
130      // <FluentSetters>
131
132      @Override /* GENERATED - TargetedAnnotationBuilder */
133      public Builder on(String...values) {
134         super.on(values);
135         return this;
136      }
137
138      @Override /* GENERATED - TargetedAnnotationTBuilder */
139      public Builder on(java.lang.Class<?>...value) {
140         super.on(value);
141         return this;
142      }
143
144      @Override /* GENERATED - TargetedAnnotationTBuilder */
145      public Builder onClass(java.lang.Class<?>...value) {
146         super.onClass(value);
147         return this;
148      }
149
150      @Override /* GENERATED - TargetedAnnotationTMFBuilder */
151      public Builder on(Field...value) {
152         super.on(value);
153         return this;
154      }
155
156      @Override /* GENERATED - TargetedAnnotationTMFBuilder */
157      public Builder on(Method...value) {
158         super.on(value);
159         return this;
160      }
161
162      // </FluentSetters>
163   }
164
165   //-----------------------------------------------------------------------------------------------------------------
166   // Implementation
167   //-----------------------------------------------------------------------------------------------------------------
168
169   private static class Impl extends TargetedAnnotationTImpl implements Json {
170
171      private final String wrapperAttr;
172
173      Impl(Builder b) {
174         super(b);
175         this.wrapperAttr = b.wrapperAttr;
176         postConstruct();
177      }
178
179      @Override /* Json */
180      public String wrapperAttr() {
181         return wrapperAttr;
182      }
183   }
184
185   //-----------------------------------------------------------------------------------------------------------------
186   // Appliers
187   //-----------------------------------------------------------------------------------------------------------------
188
189   /**
190    * Applies targeted {@link Json} annotations to a {@link org.apache.juneau.Context.Builder}.
191    */
192   public static class Apply extends AnnotationApplier<Json,Context.Builder> {
193
194      /**
195       * Constructor.
196       *
197       * @param vr The resolver for resolving values in annotations.
198       */
199      public Apply(VarResolverSession vr) {
200         super(Json.class, Context.Builder.class, vr);
201      }
202
203      @Override
204      public void apply(AnnotationInfo<Json> ai, Context.Builder b) {
205         Json a = ai.inner();
206         if (isEmptyArray(a.on(), a.onClass()))
207            return;
208         b.annotations(copy(a, vr()));
209      }
210   }
211
212   //-----------------------------------------------------------------------------------------------------------------
213   // Other
214   //-----------------------------------------------------------------------------------------------------------------
215
216   /**
217    * A collection of {@link Json @Json annotations}.
218    */
219   @Documented
220   @Target({METHOD,TYPE})
221   @Retention(RUNTIME)
222   @Inherited
223   public static @interface Array {
224
225      /**
226       * The child annotations.
227       *
228       * @return The annotation value.
229       */
230      Json[] value();
231   }
232}