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; 018 019import static org.apache.juneau.commons.utils.CollectionUtils.*; 020 021import java.lang.annotation.*; 022import java.lang.reflect.*; 023 024import org.apache.juneau.commons.*; 025import org.apache.juneau.commons.reflect.*; 026import org.apache.juneau.commons.svl.*; 027 028/** 029 * Utility classes and methods for the {@link ExampleApply @ExampleApply} annotation. 030 * 031 */ 032public class ExampleApplyAnnotation { 033 034 /** 035 * Prevents instantiation. 036 */ 037 private ExampleApplyAnnotation() {} 038 039 /** 040 * Applies targeted {@link ExampleApply} annotations to a {@link Context.Builder}. 041 * 042 * <p> 043 * Passes the {@link ExampleApply @ExampleApply} annotation through to the builder's annotation list. 044 * The {@link org.apache.juneau.commons.reflect.AnnotationProvider.Builder#addRuntimeAnnotations(java.util.List) AnnotationProvider.Builder#addRuntimeAnnotations(java.util.List)} 045 * method handles unwrapping the nested {@link Example @Example} from {@link ExampleApply#value()} and registering it 046 * under the targets specified by {@link ExampleApply#on()} and {@link ExampleApply#onClass()}. 047 */ 048 @SuppressWarnings({ 049 "rawtypes" // Raw types required for reflective annotation application. 050 }) 051 public static class Applier extends AnnotationApplier<ExampleApply,Context.Builder> { 052 053 /** 054 * Constructor. 055 * 056 * @param vr The resolver for resolving values in annotations. Must not be <jk>null</jk>. 057 */ 058 public Applier(VarResolverSession vr) { 059 super(ExampleApply.class, Context.Builder.class, vr); 060 } 061 062 @Override 063 public void apply(AnnotationInfo<ExampleApply> ai, Context.Builder b) { 064 ExampleApply a = ai.inner(); 065 if (isEmptyArray(a.on()) && isEmptyArray(a.onClass())) 066 return; 067 b.annotations(a); 068 } 069 } 070 071 /** 072 * Builder class. 073 * 074 * <h5 class='section'>See Also:</h5><ul> 075 * <li class='jm'>{@link MarshallingContext.Builder#annotations(Annotation...)} 076 * </ul> 077 */ 078 public static class Builder extends AppliedAnnotationObject.BuilderTMF { 079 080 Example value = ExampleAnnotation.DEFAULT; 081 082 /** 083 * Constructor. 084 */ 085 protected Builder() { 086 super(ExampleApply.class); 087 } 088 089 /** 090 * Sets the {@link ExampleApply#value()} property on this annotation. 091 * 092 * @param value The new value for this property. 093 * @return This object. 094 */ 095 public Builder value(Example value) { 096 this.value = value; 097 return this; 098 } 099 100 @Override /* Overridden from AppliedAnnotationObject.Builder */ 101 public Builder on(String...value) { 102 super.on(value); 103 return this; 104 } 105 106 @Override /* Overridden from AppliedAnnotationObject.BuilderT */ 107 public Builder on(Class<?>...value) { 108 super.on(value); 109 return this; 110 } 111 112 @Override /* Overridden from AppliedOnClassAnnotationObject.Builder */ 113 public Builder onClass(Class<?>...value) { 114 super.onClass(value); 115 return this; 116 } 117 118 @Override /* Overridden from AppliedAnnotationObject.BuilderM */ 119 public Builder on(Method...value) { 120 super.on(value); 121 return this; 122 } 123 124 @Override /* Overridden from AppliedAnnotationObject.BuilderMF */ 125 public Builder on(Field...value) { 126 super.on(value); 127 return this; 128 } 129 130 @Override /* Overridden from AppliedAnnotationObject.BuilderT */ 131 public Builder on(ClassInfo...value) { 132 super.on(value); 133 return this; 134 } 135 136 @Override /* Overridden from AppliedAnnotationObject.BuilderT */ 137 public Builder onClass(ClassInfo...value) { 138 super.onClass(value); 139 return this; 140 } 141 142 @Override /* Overridden from AppliedAnnotationObject.BuilderTMF */ 143 public Builder on(FieldInfo...value) { 144 super.on(value); 145 return this; 146 } 147 148 @Override /* Overridden from AppliedAnnotationObject.BuilderTMF */ 149 public Builder on(MethodInfo...value) { 150 super.on(value); 151 return this; 152 } 153 154 /** 155 * Instantiates a new {@link ExampleApply @ExampleApply} object initialized with this builder. 156 * 157 * @return A new {@link ExampleApply} object. 158 */ 159 public ExampleApply build() { 160 return new Object(this); 161 } 162 } 163 164 @SuppressWarnings({ 165 "java:S2160" // equals() inherited from AnnotationObject compares all annotation interface methods; subclass fields are accessed via those methods 166 }) 167 private static class Object extends AppliedOnClassAnnotationObject implements ExampleApply { 168 169 private final Example value; 170 171 Object(ExampleApplyAnnotation.Builder b) { 172 super(b); 173 value = b.value; 174 } 175 176 @Override /* Overridden from ExampleApply */ 177 public Example value() { 178 return value; 179 } 180 } 181 182 /** Default value */ 183 public static final ExampleApply DEFAULT = create().build(); 184 185 /** 186 * Instantiates a new builder for this class. 187 * 188 * @return A new builder object. 189 */ 190 public static Builder create() { 191 return new Builder(); 192 } 193 194 /** 195 * Instantiates a new builder for this class. 196 * 197 * @param on The targets this annotation applies to. 198 * @return A new builder object. 199 */ 200 public static Builder create(Class<?>...on) { 201 return create().on(on); 202 } 203 204 /** 205 * Instantiates a new builder for this class. 206 * 207 * @param on The targets this annotation applies to. 208 * @return A new builder object. 209 */ 210 public static Builder create(String...on) { 211 return create().on(on); 212 } 213 214 /** 215 * Returns <jk>true</jk> if the specified annotation contains all default values. 216 * 217 * @param a The annotation to check. Can be <jk>null</jk> (returns <jk>true</jk>). 218 * @return <jk>true</jk> if the specified annotation contains all default values. 219 */ 220 public static boolean empty(ExampleApply a) { 221 return a == null || DEFAULT.equals(a); 222 } 223}