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.marshall.uon;
018
019import static org.apache.juneau.commons.utils.CollectionUtils.*;
020
021import java.lang.reflect.*;
022
023import org.apache.juneau.commons.*;
024import org.apache.juneau.commons.reflect.*;
025import org.apache.juneau.commons.svl.*;
026import org.apache.juneau.marshall.*;
027
028/**
029 * Utility classes and methods for the {@link UonApply @UonApply} annotation.
030 *
031 */
032public class UonApplyAnnotation {
033
034   /**
035    * Prevents instantiation.
036    */
037   private UonApplyAnnotation() {}
038
039   /**
040    * Applies targeted {@link UonApply} annotations to a {@link org.apache.juneau.marshall.Context.Builder Context.Builder}.
041    */
042   @SuppressWarnings({
043      "rawtypes" // Raw types required for reflective annotation application.
044   })
045   public static class Applier extends AnnotationApplier<UonApply,Context.Builder> {
046
047      /**
048       * Constructor.
049       *
050       * @param vr The resolver for resolving values in annotations.
051       *    <br>Must not be <jk>null</jk>.
052       */
053      public Applier(VarResolverSession vr) {
054         super(UonApply.class, Context.Builder.class, vr);
055      }
056
057      @Override
058      public void apply(AnnotationInfo<UonApply> ai, Context.Builder b) {
059         UonApply a = ai.inner();
060         if (isEmptyArray(a.on()) && isEmptyArray(a.onClass()))
061            return;
062         b.annotations(a);
063      }
064   }
065
066   /**
067    * Builder class.
068    *
069    * <h5 class='section'>See Also:</h5><ul>
070    *    <li class='jm'>{@link org.apache.juneau.marshall.MarshallingContext.Builder#annotations(java.lang.annotation.Annotation...) MarshallingContext.Builder#annotations(java.lang.annotation.Annotation...)}
071    * </ul>
072    */
073   public static class Builder extends AppliedAnnotationObject.BuilderTMF {
074
075      Uon value = UonAnnotation.DEFAULT;
076
077      /**
078       * Constructor.
079       */
080      protected Builder() {
081         super(UonApply.class);
082      }
083
084      /**
085       * Sets the {@link UonApply#value()} property on this annotation.
086       *
087       * @param value The new value for this property.
088       * @return This object.
089       */
090      public Builder value(Uon value) {
091         this.value = value;
092         return this;
093      }
094
095      @Override public Builder on(String...value) { super.on(value); return this; }
096      @Override public Builder on(Class<?>...value) { super.on(value); return this; }
097      @Override public Builder onClass(Class<?>...value) { super.onClass(value); return this; }
098      @Override public Builder on(Method...value) { super.on(value); return this; }
099      @Override public Builder on(Field...value) { super.on(value); return this; }
100      @Override public Builder on(ClassInfo...value) { super.on(value); return this; }
101      @Override public Builder onClass(ClassInfo...value) { super.onClass(value); return this; }
102      @Override public Builder on(FieldInfo...value) { super.on(value); return this; }
103      @Override public Builder on(MethodInfo...value) { super.on(value); return this; }
104
105      /**
106       * Instantiates a new {@link UonApply @UonApply} object initialized with this builder.
107       *
108       * @return A new {@link UonApply} object.
109       */
110      public UonApply build() {
111         return new Object(this);
112      }
113   }
114
115   @SuppressWarnings({
116      "java:S2160" // equals() inherited from AnnotationObject compares all annotation interface methods; subclass fields are accessed via those methods
117   })
118   private static class Object extends AppliedOnClassAnnotationObject implements UonApply {
119
120      private final Uon value;
121
122      Object(UonApplyAnnotation.Builder b) {
123         super(b);
124         value = b.value;
125      }
126
127      @Override public Uon value() { return value; }
128   }
129
130   /** Default value */
131   public static final UonApply DEFAULT = create().build();
132
133   /**
134    * Instantiates a new builder for this class.
135    *
136    * @return A new builder object.
137    */
138   public static Builder create() { return new Builder(); }
139
140   /**
141    * Instantiates a new builder for this class.
142    *
143    * @param on The targets this annotation applies to.
144    * @return A new builder object.
145    */
146   public static Builder create(Class<?>...on) { return create().on(on); }
147
148   /**
149    * Instantiates a new builder for this class.
150    *
151    * @param on The targets this annotation applies to.
152    * @return A new builder object.
153    */
154   public static Builder create(String...on) { return create().on(on); }
155
156   /**
157    * Returns <jk>true</jk> if the specified annotation contains all default values.
158    *
159    * @param a The annotation to check.
160    *    <br>Can be <jk>null</jk> (returns <jk>true</jk>).
161    * @return <jk>true</jk> if the specified annotation contains all default values.
162    */
163   public static boolean empty(UonApply a) {
164      return a == null || DEFAULT.equals(a);
165   }
166}