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