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