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