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