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