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.commons.utils.CollectionUtils.*; 022 023import java.lang.annotation.*; 024import java.lang.reflect.*; 025 026import org.apache.juneau.*; 027import org.apache.juneau.commons.annotation.*; 028import org.apache.juneau.commons.reflect.*; 029import org.apache.juneau.svl.*; 030 031/** 032 * Utility classes and methods for the {@link Swap @Swap} annotation. 033 * 034 */ 035public class SwapAnnotation { 036 /** 037 * Applies targeted {@link Swap} annotations to a {@link org.apache.juneau.BeanContext.Builder}. 038 */ 039 public static class Applier extends AnnotationApplier<Swap,BeanContext.Builder> { 040 041 /** 042 * Constructor. 043 * 044 * @param vr The resolver for resolving values in annotations. 045 */ 046 public Applier(VarResolverSession vr) { 047 super(Swap.class, BeanContext.Builder.class, vr); 048 } 049 050 @Override 051 public void apply(AnnotationInfo<Swap> ai, BeanContext.Builder b) { 052 Swap a = ai.inner(); 053 if (isEmptyArray(a.on()) && isEmptyArray(a.onClass())) 054 return; 055 b.annotations(copy(a, vr())); 056 } 057 } 058 059 /** 060 * A collection of {@link Swap @Swap annotations}. 061 */ 062 @Documented 063 @Target({ METHOD, TYPE }) 064 @Retention(RUNTIME) 065 @Inherited 066 public static @interface Array { 067 068 /** 069 * The child annotations. 070 * 071 * @return The annotation value. 072 */ 073 Swap[] value(); 074 } 075 076 /** 077 * Builder class. 078 * 079 * <h5 class='section'>See Also:</h5><ul> 080 * <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)} 081 * </ul> 082 */ 083 public static class Builder extends AppliedAnnotationObject.BuilderTMF { 084 085 private String[] description = {}; 086 private Class<?> impl = void.class; 087 private Class<?> value = void.class; 088 private String template = ""; 089 private String[] mediaTypes = {}; 090 091 /** 092 * Constructor. 093 */ 094 protected Builder() { 095 super(Swap.class); 096 } 097 098 /** 099 * Instantiates a new {@link Swap @Swap} object initialized with this builder. 100 * 101 * @return A new {@link Swap @Swap} object. 102 */ 103 public Swap build() { 104 return new Object(this); 105 } 106 107 /** 108 * Sets the description property on this annotation. 109 * 110 * @param value The new value for this property. 111 * @return This object. 112 */ 113 public Builder description(String...value) { 114 description = value; 115 return this; 116 } 117 118 /** 119 * Sets the {@link Swap#impl()} property on this annotation. 120 * 121 * @param value The new value for this property. 122 * @return This object. 123 */ 124 public Builder impl(Class<?> value) { 125 impl = value; 126 return this; 127 } 128 129 /** 130 * Sets the {@link Swap#mediaTypes()} property on this annotation. 131 * 132 * @param value The new value for this property. 133 * @return This object. 134 */ 135 public Builder mediaTypes(String...value) { 136 mediaTypes = value; 137 return this; 138 } 139 140 /** 141 * Sets the {@link Swap#template()} property on this annotation. 142 * 143 * @param value The new value for this property. 144 * @return This object. 145 */ 146 public Builder template(String value) { 147 template = value; 148 return this; 149 } 150 151 /** 152 * Sets the {@link Swap#value()} property on this annotation. 153 * 154 * @param value The new value for this property. 155 * @return This object. 156 */ 157 public Builder value(Class<?> value) { 158 this.value = value; 159 return this; 160 } 161 162 @Override /* Overridden from AppliedAnnotationObject.Builder */ 163 public Builder on(String...value) { 164 super.on(value); 165 return this; 166 } 167 168 @Override /* Overridden from AppliedAnnotationObject.BuilderT */ 169 public Builder on(Class<?>...value) { 170 super.on(value); 171 return this; 172 } 173 174 @Override /* Overridden from AppliedOnClassAnnotationObject.Builder */ 175 public Builder onClass(Class<?>...value) { 176 super.onClass(value); 177 return this; 178 } 179 180 @Override /* Overridden from AppliedAnnotationObject.BuilderM */ 181 public Builder on(Method...value) { 182 super.on(value); 183 return this; 184 } 185 186 @Override /* Overridden from AppliedAnnotationObject.BuilderMF */ 187 public Builder on(Field...value) { 188 super.on(value); 189 return this; 190 } 191 192 @Override /* Overridden from AppliedAnnotationObject.BuilderT */ 193 public Builder on(ClassInfo...value) { 194 super.on(value); 195 return this; 196 } 197 198 @Override /* Overridden from AppliedAnnotationObject.BuilderT */ 199 public Builder onClass(ClassInfo...value) { 200 super.onClass(value); 201 return this; 202 } 203 204 @Override /* Overridden from AppliedAnnotationObject.BuilderTMF */ 205 public Builder on(FieldInfo...value) { 206 super.on(value); 207 return this; 208 } 209 210 @Override /* Overridden from AppliedAnnotationObject.BuilderTMF */ 211 public Builder on(MethodInfo...value) { 212 super.on(value); 213 return this; 214 } 215 216 } 217 218 private static class Object extends AppliedOnClassAnnotationObject implements Swap { 219 220 private final String[] description; 221 private final Class<?> impl, value; 222 private final String template; 223 private final String[] mediaTypes; 224 225 Object(SwapAnnotation.Builder b) { 226 super(b); 227 description = copyOf(b.description); 228 impl = b.impl; 229 mediaTypes = copyOf(b.mediaTypes); 230 template = b.template; 231 value = b.value; 232 } 233 234 @Override /* Overridden from Swap */ 235 public Class<?> impl() { 236 return impl; 237 } 238 239 @Override /* Overridden from Swap */ 240 public String[] mediaTypes() { 241 return mediaTypes; 242 } 243 244 @Override /* Overridden from Swap */ 245 public String template() { 246 return template; 247 } 248 249 @Override /* Overridden from Swap */ 250 public Class<?> value() { 251 return value; 252 } 253 254 @Override /* Overridden from annotation */ 255 public String[] description() { 256 return description; 257 } 258 } 259 260 /** Default value */ 261 public static final Swap DEFAULT = create().build(); 262 263 /** 264 * Creates a copy of the specified annotation. 265 * 266 * @param a The annotation to copy.s 267 * @param r The var resolver for resolving any variables. 268 * @return A copy of the specified annotation. 269 */ 270 public static Swap copy(Swap a, VarResolverSession r) { 271 // @formatter:off 272 return 273 create() 274 .impl(a.impl()) 275 .mediaTypes(r.resolve(a.mediaTypes())) 276 .on(r.resolve(a.on())) 277 .onClass(a.onClass()) 278 .template(r.resolve(a.template())) 279 .value(a.value()) 280 .build(); 281 // @formatter:on 282 } 283 284 /** 285 * Instantiates a new builder for this class. 286 * 287 * @return A new builder object. 288 */ 289 public static Builder create() { 290 return new Builder(); 291 } 292 293 /** 294 * Instantiates a new builder for this class. 295 * 296 * @param on The targets this annotation applies to. 297 * @return A new builder object. 298 */ 299 public static Builder create(Class<?>...on) { 300 return create().on(on); 301 } 302 303 /** 304 * Instantiates a new builder for this class. 305 * 306 * @param on The targets this annotation applies to. 307 * @return A new builder object. 308 */ 309 public static Builder create(String...on) { 310 return create().on(on); 311 } 312}