001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.json.annotation;
018
019import static java.lang.annotation.ElementType.*;
020import static java.lang.annotation.RetentionPolicy.*;
021import static org.apache.juneau.internal.ArrayUtils.*;
022
023import java.lang.annotation.*;
024
025import org.apache.juneau.*;
026import org.apache.juneau.annotation.*;
027import org.apache.juneau.reflect.*;
028import org.apache.juneau.svl.*;
029
030/**
031 * Utility classes and methods for the {@link Json @Json} annotation.
032 *
033 * <h5 class='section'>See Also:</h5><ul>
034 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JsonBasics">JSON Basics</a>
035 * </ul>
036 */
037public class JsonAnnotation {
038
039   //-----------------------------------------------------------------------------------------------------------------
040   // Static
041   //-----------------------------------------------------------------------------------------------------------------
042
043   /** Default value */
044   public static final Json DEFAULT = create().build();
045
046   /**
047    * Instantiates a new builder for this class.
048    *
049    * @return A new builder object.
050    */
051   public static Builder create() {
052      return new Builder();
053   }
054
055   /**
056    * Instantiates a new builder for this class.
057    *
058    * @param on The targets this annotation applies to.
059    * @return A new builder object.
060    */
061   public static Builder create(Class<?>...on) {
062      return create().on(on);
063   }
064
065   /**
066    * Instantiates a new builder for this class.
067    *
068    * @param on The targets this annotation applies to.
069    * @return A new builder object.
070    */
071   public static Builder create(String...on) {
072      return create().on(on);
073   }
074
075   /**
076    * Creates a copy of the specified annotation.
077    *
078    * @param a The annotation to copy.s
079    * @param r The var resolver for resolving any variables.
080    * @return A copy of the specified annotation.
081    */
082   public static Json copy(Json a, VarResolverSession r) {
083      return
084         create()
085         .on(r.resolve(a.on()))
086         .onClass(a.onClass())
087         .wrapperAttr(r.resolve(a.wrapperAttr()))
088         .build();
089   }
090
091   //-----------------------------------------------------------------------------------------------------------------
092   // Builder
093   //-----------------------------------------------------------------------------------------------------------------
094
095   /**
096    * Builder class.
097    *
098    * <h5 class='section'>See Also:</h5><ul>
099    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
100    * </ul>
101    */
102   public static class Builder extends TargetedAnnotationTMFBuilder<Builder> {
103
104      String wrapperAttr="";
105
106      /**
107       * Constructor.
108       */
109      protected Builder() {
110         super(Json.class);
111      }
112
113      /**
114       * Instantiates a new {@link Json @Json} object initialized with this builder.
115       *
116       * @return A new {@link Json @Json} object.
117       */
118      public Json build() {
119         return new Impl(this);
120      }
121
122      /**
123       * Sets the {@link Json#wrapperAttr} property on this annotation.
124       *
125       * @param value The new value for this property.
126       * @return This object.
127       */
128      public Builder wrapperAttr(String value) {
129         this.wrapperAttr = value;
130         return this;
131      }
132
133   }
134
135   //-----------------------------------------------------------------------------------------------------------------
136   // Implementation
137   //-----------------------------------------------------------------------------------------------------------------
138
139   private static class Impl extends TargetedAnnotationTImpl implements Json {
140
141      private final String wrapperAttr;
142
143      Impl(Builder b) {
144         super(b);
145         this.wrapperAttr = b.wrapperAttr;
146         postConstruct();
147      }
148
149      @Override /* Json */
150      public String wrapperAttr() {
151         return wrapperAttr;
152      }
153   }
154
155   //-----------------------------------------------------------------------------------------------------------------
156   // Appliers
157   //-----------------------------------------------------------------------------------------------------------------
158
159   /**
160    * Applies targeted {@link Json} annotations to a {@link org.apache.juneau.Context.Builder}.
161    */
162   public static class Apply extends AnnotationApplier<Json,Context.Builder> {
163
164      /**
165       * Constructor.
166       *
167       * @param vr The resolver for resolving values in annotations.
168       */
169      public Apply(VarResolverSession vr) {
170         super(Json.class, Context.Builder.class, vr);
171      }
172
173      @Override
174      public void apply(AnnotationInfo<Json> ai, Context.Builder b) {
175         Json a = ai.inner();
176         if (isEmptyArray(a.on(), a.onClass()))
177            return;
178         b.annotations(copy(a, vr()));
179      }
180   }
181
182   //-----------------------------------------------------------------------------------------------------------------
183   // Other
184   //-----------------------------------------------------------------------------------------------------------------
185
186   /**
187    * A collection of {@link Json @Json annotations}.
188    */
189   @Documented
190   @Target({METHOD,TYPE})
191   @Retention(RUNTIME)
192   @Inherited
193   public static @interface Array {
194
195      /**
196       * The child annotations.
197       *
198       * @return The annotation value.
199       */
200      Json[] value();
201   }
202}