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.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.reflect.*;
027import org.apache.juneau.svl.*;
028
029/**
030 * Utility classes and methods for the {@link Marshalled @Marshalled} annotation.
031 *
032 * <h5 class='section'>See Also:</h5><ul>
033 * </ul>
034 */
035public class MarshalledAnnotation {
036
037   //-----------------------------------------------------------------------------------------------------------------
038   // Static
039   //-----------------------------------------------------------------------------------------------------------------
040
041   /** Default value */
042   public static final Marshalled DEFAULT = create().build();
043
044   /**
045    * Instantiates a new builder for this class.
046    *
047    * @return A new builder object.
048    */
049   public static Builder create() {
050      return new Builder();
051   }
052
053   /**
054    * Instantiates a new builder for this class.
055    *
056    * @param on The targets this annotation applies to.
057    * @return A new builder object.
058    */
059   public static Builder create(Class<?>...on) {
060      return create().on(on);
061   }
062
063   /**
064    * Instantiates a new builder for this class.
065    *
066    * @param on The targets this annotation applies to.
067    * @return A new builder object.
068    */
069   public static Builder create(String...on) {
070      return create().on(on);
071   }
072
073   /**
074    * Creates a copy of the specified annotation.
075    *
076    * @param a The annotation to copy.s
077    * @param r The var resolver for resolving any variables.
078    * @return A copy of the specified annotation.
079    */
080   public static Marshalled copy(Marshalled a, VarResolverSession r) {
081      return
082         create()
083         .example(r.resolve(a.example()))
084         .implClass(a.implClass())
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 TargetedAnnotationTBuilder<Builder> {
102
103      Class<?> implClass=void.class;
104      String example="";
105
106      /**
107       * Constructor.
108       */
109      protected Builder() {
110         super(Marshalled.class);
111      }
112
113      /**
114       * Instantiates a new {@link Marshalled @Marshalled} object initialized with this builder.
115       *
116       * @return A new {@link Marshalled @Marshalled} object.
117       */
118      public Marshalled build() {
119         return new Impl(this);
120      }
121
122      /**
123       * Sets the {@link Marshalled#example()} property on this annotation.
124       *
125       * @param value The new value for this property.
126       * @return This object.
127       */
128      public Builder example(String value) {
129         this.example = value;
130         return this;
131      }
132
133      /**
134       * Sets the {@link Marshalled#implClass()} property on this annotation.
135       *
136       * @param value The new value for this property.
137       * @return This object.
138       */
139      public Builder implClass(Class<?> value) {
140         this.implClass = value;
141         return this;
142      }
143
144   }
145
146   //-----------------------------------------------------------------------------------------------------------------
147   // Implementation
148   //-----------------------------------------------------------------------------------------------------------------
149
150   private static class Impl extends TargetedAnnotationTImpl implements Marshalled {
151
152      private final Class<?> implClass;
153      private final String example;
154
155      Impl(Builder b) {
156         super(b);
157         this.example = b.example;
158         this.implClass = b.implClass;
159         postConstruct();
160      }
161
162      @Override /* Marshalled */
163      public String example() {
164         return example;
165      }
166
167      @Override /* Marshalled */
168      public Class<?> implClass() {
169         return implClass;
170      }
171   }
172
173   //-----------------------------------------------------------------------------------------------------------------
174   // Appliers
175   //-----------------------------------------------------------------------------------------------------------------
176
177   /**
178    * Applies targeted {@link Marshalled} annotations to a {@link org.apache.juneau.BeanContext.Builder}.
179    */
180   public static class Applier extends AnnotationApplier<Marshalled,BeanContext.Builder> {
181
182      /**
183       * Constructor.
184       *
185       * @param vr The resolver for resolving values in annotations.
186       */
187      public Applier(VarResolverSession vr) {
188         super(Marshalled.class, BeanContext.Builder.class, vr);
189      }
190
191      @Override
192      public void apply(AnnotationInfo<Marshalled> ai, BeanContext.Builder b) {
193         Marshalled a = ai.inner();
194         if (isEmptyArray(a.on(), a.onClass()))
195            return;
196         b.annotations(copy(a, vr()));
197      }
198   }
199
200   //-----------------------------------------------------------------------------------------------------------------
201   // Other
202   //-----------------------------------------------------------------------------------------------------------------
203
204   /**
205    * A collection of {@link Marshalled @Marshalled annotations}.
206    */
207   @Documented
208   @Target({METHOD,TYPE})
209   @Retention(RUNTIME)
210   @Inherited
211   public static @interface Array {
212
213      /**
214       * The child annotations.
215       *
216       * @return The annotation value.
217       */
218      Marshalled[] value();
219   }
220}