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