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.rest.annotation;
014
015import static java.lang.annotation.ElementType.*;
016import static java.lang.annotation.RetentionPolicy.*;
017
018import java.lang.annotation.*;
019
020import org.apache.juneau.annotation.*;
021
022/**
023 * Utility classes and methods for the {@link RestInject RestInject} annotation.
024 *
025 * <h5 class='section'>See Also:</h5><ul>
026 * </ul>
027 */
028public class RestInjectAnnotation {
029
030   //-----------------------------------------------------------------------------------------------------------------
031   // Static
032   //-----------------------------------------------------------------------------------------------------------------
033
034   /** Default value */
035   public static final RestInject DEFAULT = create().build();
036
037   /**
038    * Instantiates a new builder for this class.
039    *
040    * @return A new builder object.
041    */
042   public static Builder create() {
043      return new Builder();
044   }
045
046   /**
047    * Pulls the name/value attribute from a {@link RestInject} annotation.
048    *
049    * @param a The annotation to check.  Can be <jk>null</jk>.
050    * @return The annotation value, or an empty string if the annotation is <jk>null</jk>.
051    */
052   public static String name(RestInject a) {
053      if (a == null)
054         return "";
055      if (! a.name().isEmpty())
056         return a.name();
057      return a.value();
058   }
059
060   //-----------------------------------------------------------------------------------------------------------------
061   // Builder
062   //-----------------------------------------------------------------------------------------------------------------
063
064   /**
065    * Builder class.
066    *
067    * <h5 class='section'>See Also:</h5><ul>
068    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
069    * </ul>
070    */
071   public static class Builder extends TargetedAnnotationMBuilder {
072
073      String name, value;
074      String[] methodScope;
075
076      /**
077       * Constructor.
078       */
079      protected Builder() {
080         super(RestInject.class);
081      }
082
083      /**
084       * Instantiates a new {@link RestInject @RestInject} object initialized with this builder.
085       *
086       * @return A new {@link RestInject @RestInject} object.
087       */
088      public RestInject build() {
089         return new Impl(this);
090      }
091
092      /**
093       * Sets the {@link RestInject#name()} property on this annotation.
094       *
095       * @param value The new value for this property.
096       * @return This object.
097       */
098      public Builder name(String value) {
099         this.name = value;
100         return this;
101      }
102
103      /**
104       * Sets the {@link RestInject#value()} property on this annotation.
105       *
106       * @param value The new value for this property.
107       * @return This object.
108       */
109      public Builder value(String value) {
110         this.value = value;
111         return this;
112      }
113
114      /**
115       * Sets the {@link RestInject#methodScope()} property on this annotation.
116       *
117       * @param value The new value for this property.
118       * @return This object.
119       */
120      public Builder methodScope(String...value) {
121         this.methodScope = value;
122         return this;
123      }
124
125      // <FluentSetters>
126
127      @Override /* GENERATED - TargetedAnnotationBuilder */
128      public Builder on(String...values) {
129         super.on(values);
130         return this;
131      }
132
133      @Override /* GENERATED - TargetedAnnotationTMBuilder */
134      public Builder on(java.lang.reflect.Method...value) {
135         super.on(value);
136         return this;
137      }
138
139      // </FluentSetters>
140   }
141
142   //-----------------------------------------------------------------------------------------------------------------
143   // Implementation
144   //-----------------------------------------------------------------------------------------------------------------
145
146   private static class Impl extends TargetedAnnotationImpl implements RestInject {
147
148      private final String name, value;
149      private final String[] methodScope;
150
151      Impl(Builder b) {
152         super(b);
153         this.name = b.name;
154         this.value = b.value;
155         this.methodScope = b.methodScope;
156         postConstruct();
157      }
158
159      @Override /* RestInject */
160      public String name() {
161         return name;
162      }
163
164      @Override /* RestInject */
165      public String value() {
166         return value;
167      }
168
169      @Override /* RestInject */
170      public String[] methodScope() {
171         return methodScope;
172      }
173   }
174
175   //-----------------------------------------------------------------------------------------------------------------
176   // Other
177   //-----------------------------------------------------------------------------------------------------------------
178
179   /**
180    * A collection of {@link RestInject @RestInject annotations}.
181    */
182   @Documented
183   @Target({FIELD,METHOD,TYPE})
184   @Retention(RUNTIME)
185   @Inherited
186   public static @interface Array {
187
188      /**
189       * The child annotations.
190       *
191       * @return The annotation value.
192       */
193      RestInject[] value();
194   }
195}