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