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