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.urlencoding.annotation; 018 019import static java.lang.annotation.ElementType.*; 020import static java.lang.annotation.RetentionPolicy.*; 021import static org.apache.juneau.internal.ArrayUtils.*; 022 023import java.lang.annotation.*; 024 025import org.apache.juneau.*; 026import org.apache.juneau.annotation.*; 027import org.apache.juneau.reflect.*; 028import org.apache.juneau.svl.*; 029 030/** 031 * Utility classes and methods for the {@link UrlEncoding @UrlEncoding} annotation. 032 * 033 * <h5 class='section'>See Also:</h5><ul> 034 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/UrlEncodingBasics">URL-Encoding Basics</a> 035 * </ul> 036 */ 037public class UrlEncodingAnnotation { 038 039 //----------------------------------------------------------------------------------------------------------------- 040 // Static 041 //----------------------------------------------------------------------------------------------------------------- 042 043 /** Default value */ 044 public static final UrlEncoding DEFAULT = create().build(); 045 046 /** 047 * Instantiates a new builder for this class. 048 * 049 * @return A new builder object. 050 */ 051 public static Builder create() { 052 return new Builder(); 053 } 054 055 /** 056 * Instantiates a new builder for this class. 057 * 058 * @param on The targets this annotation applies to. 059 * @return A new builder object. 060 */ 061 public static Builder create(Class<?>...on) { 062 return create().on(on); 063 } 064 065 /** 066 * Instantiates a new builder for this class. 067 * 068 * @param on The targets this annotation applies to. 069 * @return A new builder object. 070 */ 071 public static Builder create(String...on) { 072 return create().on(on); 073 } 074 075 /** 076 * Creates a copy of the specified annotation. 077 * 078 * @param a The annotation to copy.s 079 * @param r The var resolver for resolving any variables. 080 * @return A copy of the specified annotation. 081 */ 082 public static UrlEncoding copy(UrlEncoding a, VarResolverSession r) { 083 return 084 create() 085 .expandedParams(a.expandedParams()) 086 .on(r.resolve(a.on())) 087 .onClass(a.onClass()) 088 .build(); 089 } 090 091 //----------------------------------------------------------------------------------------------------------------- 092 // Builder 093 //----------------------------------------------------------------------------------------------------------------- 094 095 /** 096 * Builder class. 097 * 098 * <h5 class='section'>See Also:</h5><ul> 099 * <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)} 100 * </ul> 101 */ 102 public static class Builder extends TargetedAnnotationTMFBuilder<Builder> { 103 104 boolean expandedParams; 105 106 /** 107 * Constructor. 108 */ 109 protected Builder() { 110 super(UrlEncoding.class); 111 } 112 113 /** 114 * Instantiates a new {@link UrlEncoding @UrlEncoding} object initialized with this builder. 115 * 116 * @return A new {@link UrlEncoding @UrlEncoding} object. 117 */ 118 public UrlEncoding build() { 119 return new Impl(this); 120 } 121 122 123 /** 124 * Sets the {@link UrlEncoding#expandedParams} property on this annotation. 125 * 126 * @param value The new value for this property. 127 * @return This object. 128 */ 129 public Builder expandedParams(boolean value) { 130 this.expandedParams = value; 131 return this; 132 } 133 134 } 135 136 //----------------------------------------------------------------------------------------------------------------- 137 // Implementation 138 //----------------------------------------------------------------------------------------------------------------- 139 140 private static class Impl extends TargetedAnnotationTImpl implements UrlEncoding { 141 142 private final boolean expandedParams; 143 144 Impl(Builder b) { 145 super(b); 146 this.expandedParams = b.expandedParams; 147 postConstruct(); 148 } 149 150 151 @Override /* UrlEncoding */ 152 public boolean expandedParams() { 153 return expandedParams; 154 } 155 } 156 157 //----------------------------------------------------------------------------------------------------------------- 158 // Appliers 159 //----------------------------------------------------------------------------------------------------------------- 160 161 /** 162 * Applies targeted {@link UrlEncoding} annotations to a {@link org.apache.juneau.Context.Builder}. 163 */ 164 public static class Apply extends AnnotationApplier<UrlEncoding,Context.Builder> { 165 166 /** 167 * Constructor. 168 * 169 * @param vr The resolver for resolving values in annotations. 170 */ 171 public Apply(VarResolverSession vr) { 172 super(UrlEncoding.class, Context.Builder.class, vr); 173 } 174 175 @Override 176 public void apply(AnnotationInfo<UrlEncoding> ai, Context.Builder b) { 177 UrlEncoding a = ai.inner(); 178 if (isEmptyArray(a.on(), a.onClass())) 179 return; 180 b.annotations(copy(a, vr())); 181 } 182 } 183 184 //----------------------------------------------------------------------------------------------------------------- 185 // Other 186 //----------------------------------------------------------------------------------------------------------------- 187 188 /** 189 * A collection of {@link UrlEncoding @UrlEncoding annotations}. 190 */ 191 @Documented 192 @Target({METHOD,TYPE}) 193 @Retention(RUNTIME) 194 @Inherited 195 public static @interface Array { 196 197 /** 198 * The child annotations. 199 * 200 * @return The annotation value. 201 */ 202 UrlEncoding[] value(); 203 } 204}