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