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