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.annotation;
014
015import static java.lang.annotation.ElementType.*;
016import static java.lang.annotation.RetentionPolicy.*;
017import static org.apache.juneau.internal.ArrayUtils.*;
018
019import java.lang.annotation.*;
020import java.lang.reflect.*;
021
022import org.apache.juneau.*;
023import org.apache.juneau.reflect.*;
024import org.apache.juneau.svl.*;
025
026/**
027 * Utility classes and methods for the {@link BeanIgnore @BeanIgnore} annotation.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 * </ul>
031 */
032public class BeanIgnoreAnnotation {
033
034   //-----------------------------------------------------------------------------------------------------------------
035   // Static
036   //-----------------------------------------------------------------------------------------------------------------
037
038   /** Default value */
039   public static final BeanIgnore 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    * Instantiates a new builder for this class.
052    *
053    * @param on The targets this annotation applies to.
054    * @return A new builder object.
055    */
056   public static Builder create(Class<?>...on) {
057      return create().on(on);
058   }
059
060   /**
061    * Instantiates a new builder for this class.
062    *
063    * @param on The targets this annotation applies to.
064    * @return A new builder object.
065    */
066   public static Builder create(String...on) {
067      return create().on(on);
068   }
069
070   /**
071    * Creates a copy of the specified annotation.
072    *
073    * @param a The annotation to copy.
074    * @param r The var resolver for resolving any variables.
075    * @return A copy of the specified annotation.
076    */
077   public static BeanIgnore copy(BeanIgnore a, VarResolverSession r) {
078      return
079         create()
080         .on(r.resolve(a.on()))
081         .onClass(a.onClass())
082         .build();
083   }
084
085   //-----------------------------------------------------------------------------------------------------------------
086   // Builder
087   //-----------------------------------------------------------------------------------------------------------------
088
089   /**
090    * Builder class.
091    *
092    * <h5 class='section'>See Also:</h5><ul>
093    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
094    * </ul>
095    */
096   public static class Builder extends TargetedAnnotationTMFCBuilder {
097
098      /**
099       * Constructor.
100       */
101      protected Builder() {
102         super(BeanIgnore.class);
103      }
104
105      /**
106       * Instantiates a new {@link BeanIgnore @BeanIgnore} object initialized with this builder.
107       *
108       * @return A new {@link BeanIgnore @BeanIgnore} object.
109       */
110      public BeanIgnore build() {
111         return new Impl(this);
112      }
113
114      // <FluentSetters>
115
116      @Override /* GENERATED - TargetedAnnotationBuilder */
117      public Builder on(String...values) {
118         super.on(values);
119         return this;
120      }
121
122      @Override /* GENERATED - TargetedAnnotationTBuilder */
123      public Builder on(java.lang.Class<?>...value) {
124         super.on(value);
125         return this;
126      }
127
128      @Override /* GENERATED - TargetedAnnotationTBuilder */
129      public Builder onClass(java.lang.Class<?>...value) {
130         super.onClass(value);
131         return this;
132      }
133
134      @Override /* GENERATED - TargetedAnnotationTMFBuilder */
135      public Builder on(Field...value) {
136         super.on(value);
137         return this;
138      }
139
140      @Override /* GENERATED - TargetedAnnotationTMFBuilder */
141      public Builder on(Method...value) {
142         super.on(value);
143         return this;
144      }
145
146      @Override /* GENERATED - TargetedAnnotationTMFCBuilder */
147      public Builder on(java.lang.reflect.Constructor<?>...value) {
148         super.on(value);
149         return this;
150      }
151
152      // </FluentSetters>
153   }
154
155   //-----------------------------------------------------------------------------------------------------------------
156   // Implementation
157   //-----------------------------------------------------------------------------------------------------------------
158
159   private static class Impl extends TargetedAnnotationTImpl implements BeanIgnore {
160
161      Impl(Builder b) {
162         super(b);
163         postConstruct();
164      }
165   }
166
167   //-----------------------------------------------------------------------------------------------------------------
168   // Appliers
169   //-----------------------------------------------------------------------------------------------------------------
170
171   /**
172    * Applies targeted {@link BeanIgnore} annotations to a {@link org.apache.juneau.BeanContext.Builder}.
173    */
174   public static class Applier extends AnnotationApplier<BeanIgnore,BeanContext.Builder> {
175
176      /**
177       * Constructor.
178       *
179       * @param vr The resolver for resolving values in annotations.
180       */
181      public Applier(VarResolverSession vr) {
182         super(BeanIgnore.class, BeanContext.Builder.class, vr);
183      }
184
185      @Override
186      public void apply(AnnotationInfo<BeanIgnore> ai, BeanContext.Builder b) {
187         BeanIgnore a = ai.inner();
188         if (isEmptyArray(a.on(), a.onClass()))
189            return;
190         b.annotations(copy(a, vr()));
191      }
192   }
193
194   //-----------------------------------------------------------------------------------------------------------------
195   // Other
196   //-----------------------------------------------------------------------------------------------------------------
197
198   /**
199    * A collection of {@link BeanIgnore @BeanIgnore annotations}.
200    */
201   @Documented
202   @Target({METHOD,TYPE})
203   @Retention(RUNTIME)
204   @Inherited
205   public static @interface Array {
206
207      /**
208       * The child annotations.
209       *
210       * @return The annotation value.
211       */
212      BeanIgnore[] value();
213   }
214}