001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.annotation;
014
015import static org.apache.juneau.BeanContext.*;
016import static org.apache.juneau.BeanTraverseContext.*;
017
018import java.util.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.http.*;
022import org.apache.juneau.marshall.*;
023import org.apache.juneau.parser.*;
024import org.apache.juneau.reflect.*;
025import org.apache.juneau.svl.*;
026
027/**
028 * Applies {@link BeanConfig} annotations to a {@link PropertyStoreBuilder}.
029 */
030public class BeanConfigApply extends ConfigApply<BeanConfig> {
031
032   /**
033    * Constructor.
034    *
035    * @param c The annotation class.
036    * @param r The resolver for resolving values in annotations.
037    */
038   public BeanConfigApply(Class<BeanConfig> c, VarResolverSession r) {
039      super(c, r);
040   }
041
042   @SuppressWarnings("deprecation")
043   @Override
044   public void apply(AnnotationInfo<BeanConfig> ai, PropertyStoreBuilder psb) {
045      BeanConfig a = ai.getAnnotation();
046
047      for (CS e : a.excludeProperties())
048         psb.addTo(BEAN_bpx, e.k().getName(), string(e.v()));
049      for (CS e : a.includeProperties())
050         psb.addTo(BEAN_bpi, e.k().getName(), string(e.v()));
051      if (a.beanDictionary().length != 0)
052         psb.addTo(BEAN_beanDictionary, a.beanDictionary());
053      if (a.beanDictionary_replace().length != 0)
054         psb.set(BEAN_beanDictionary, a.beanDictionary_replace());
055      if (a.beanDictionary_remove().length != 0)
056         psb.removeFrom(BEAN_beanDictionary, a.beanDictionary_remove());
057
058      if (! a.beanClassVisibility().isEmpty())
059         psb.set(BEAN_beanClassVisibility, visibility(a.beanClassVisibility(), "beanClassVisibility"));
060      if (! a.beanConstructorVisibility().isEmpty())
061         psb.set(BEAN_beanConstructorVisibility, visibility(a.beanConstructorVisibility(), "beanConstructorVisibility"));
062      if (a.dictionary().length != 0)
063         psb.addTo(BEAN_beanDictionary, a.dictionary());
064      if (a.dictionary_replace().length != 0)
065         psb.set(BEAN_beanDictionary, a.dictionary_replace());
066      if (a.dictionary_remove().length != 0)
067         psb.removeFrom(BEAN_beanDictionary, a.dictionary_remove());
068      if (! a.beanFieldVisibility().isEmpty())
069         psb.set(BEAN_beanFieldVisibility, visibility(a.beanFieldVisibility(), "beanFieldVisibility"));
070      if (a.beanFilters().length != 0)
071         psb.addTo(BEAN_beanFilters, a.beanFilters());
072      if (a.beanFilters_replace().length != 0)
073         psb.set(BEAN_beanFilters, a.beanFilters_replace());
074      if (a.beanFilters_remove().length != 0)
075         psb.removeFrom(BEAN_beanFilters, a.beanFilters_remove());
076      if (! a.beanMapPutReturnsOldValue().isEmpty())
077         psb.set(BEAN_beanMapPutReturnsOldValue, bool(a.beanMapPutReturnsOldValue()));
078      if (! a.beanMethodVisibility().isEmpty())
079         psb.set(BEAN_beanMethodVisibility, visibility(a.beanMethodVisibility(), "beanMethodVisibility"));
080      if (! a.beansRequireDefaultConstructor().isEmpty())
081         psb.set(BEAN_beansRequireDefaultConstructor, bool(a.beansRequireDefaultConstructor()));
082      if (! a.beansRequireSerializable().isEmpty())
083         psb.set(BEAN_beansRequireSerializable, bool(a.beansRequireSerializable()));
084      if (! a.beansRequireSettersForGetters().isEmpty())
085         psb.set(BEAN_beansRequireSettersForGetters, bool(a.beansRequireSettersForGetters()));
086      if (! a.beansRequireSomeProperties().isEmpty())
087         psb.set(BEAN_beansRequireSomeProperties, bool(a.beansRequireSomeProperties()));
088      if (! a.beanTypePropertyName().isEmpty())
089         psb.set(BEAN_beanTypePropertyName, string(a.beanTypePropertyName()));
090      if (a.bpi().length > 0)
091         psb.addTo(BEAN_bpi, stringsMap(a.bpi(), "bpi"));
092      for (CS e : a.bpiMap())
093         psb.addTo(BEAN_bpi, e.k().getName(), string(e.v()));
094      if (a.bpx().length > 0)
095         psb.addTo(BEAN_bpx, stringsMap(a.bpi(), "bpx"));
096      for (CS e : a.bpxMap())
097         psb.addTo(BEAN_bpx, e.k().getName(), string(e.v()));
098      if (a.bpro().length > 0)
099         psb.addTo(BEAN_bpro, stringsMap(a.bpro(), "bpro"));
100      for (CS e : a.bproMap())
101         psb.addTo(BEAN_bpro, e.k().getName(), string(e.v()));
102      if (a.bpwo().length > 0)
103         psb.addTo(BEAN_bpwo, stringsMap(a.bpi(), "bpwo"));
104      for (CS e : a.bpwoMap())
105         psb.addTo(BEAN_bpwo, e.k().getName(), string(e.v()));
106      if (! a.debug().isEmpty())
107         psb.set(BEAN_debug, bool(a.debug()));
108      for (CS e : a.example())
109         psb.addTo(BEAN_examples, e.k().getName(), parse(e.k(), e.v(), "example"));
110      if (a.examples().length > 0)
111         psb.addTo(BEAN_examples, objectMap(a.examples(), "examples"));
112      if (! a.fluentSetters().isEmpty())
113         psb.set(BEAN_fluentSetters, bool(a.fluentSetters()));
114      if (! a.ignoreInvocationExceptionsOnGetters().isEmpty())
115         psb.set(BEAN_ignoreInvocationExceptionsOnGetters, bool(a.ignoreInvocationExceptionsOnGetters()));
116      if (! a.ignoreInvocationExceptionsOnSetters().isEmpty())
117         psb.set(BEAN_ignoreInvocationExceptionsOnSetters, bool(a.ignoreInvocationExceptionsOnSetters()));
118      if (! a.ignorePropertiesWithoutSetters().isEmpty())
119         psb.set(BEAN_ignorePropertiesWithoutSetters, bool(a.ignorePropertiesWithoutSetters()));
120      if (! a.ignoreUnknownBeanProperties().isEmpty())
121         psb.set(BEAN_ignoreUnknownBeanProperties, bool(a.ignoreUnknownBeanProperties()));
122      if (! a.ignoreUnknownNullBeanProperties().isEmpty())
123         psb.set(BEAN_ignoreUnknownNullBeanProperties, bool(a.ignoreUnknownNullBeanProperties()));
124      for (CC e : a.implClasses())
125         psb.addTo(BEAN_implClasses, e.k().getName(), e.v());
126      if (! a.locale().isEmpty())
127         psb.set(BEAN_locale, locale(a.locale()));
128      if (! a.mediaType().isEmpty())
129         psb.set(BEAN_mediaType, mediaType(a.mediaType()));
130      if (a.notBeanClasses().length != 0)
131         psb.addTo(BEAN_notBeanClasses, a.notBeanClasses());
132      if (a.notBeanClasses_replace().length != 0)
133         psb.set(BEAN_notBeanClasses, a.notBeanClasses_replace());
134      if (a.notBeanClasses_remove().length != 0)
135         psb.removeFrom(BEAN_notBeanClasses, a.notBeanClasses_remove());
136      if (a.notBeanPackages().length != 0)
137         psb.addTo(BEAN_notBeanPackages, strings(a.notBeanPackages()));
138      if (a.notBeanPackages_replace().length != 0)
139         psb.set(BEAN_notBeanPackages, strings(a.notBeanPackages_replace()));
140      if (a.notBeanPackages_remove().length != 0)
141         psb.removeFrom(BEAN_notBeanPackages, strings(a.notBeanPackages_remove()));
142      if (a.pojoSwaps().length != 0)
143         psb.addTo(BEAN_pojoSwaps, a.pojoSwaps());
144      if (a.pojoSwaps_replace().length != 0)
145         psb.set(BEAN_pojoSwaps, a.pojoSwaps_replace());
146      if (a.pojoSwaps_remove().length != 0)
147         psb.removeFrom(BEAN_pojoSwaps, a.pojoSwaps_remove());
148      if (a.propertyNamer() != PropertyNamer.Null.class)
149         psb.set(BEAN_propertyNamer, a.propertyNamer());
150      if (! a.sortProperties().isEmpty())
151         psb.set(BEAN_sortProperties, bool(a.sortProperties()));
152      if (! a.timeZone().isEmpty())
153         psb.set(BEAN_timeZone, timeZone(a.timeZone()));
154      if (! a.useEnumNames().isEmpty())
155         psb.set(BEAN_useEnumNames, bool(a.useEnumNames()));
156      if (! a.useInterfaceProxies().isEmpty())
157         psb.set(BEAN_useInterfaceProxies, bool(a.useInterfaceProxies()));
158      if (! a.useJavaBeanIntrospector().isEmpty())
159         psb.set(BEAN_useJavaBeanIntrospector, bool(a.useJavaBeanIntrospector()));
160      if (! a.detectRecursions().isEmpty())
161         psb.set(BEANTRAVERSE_detectRecursions, bool(a.detectRecursions()));
162      if (! a.ignoreRecursions().isEmpty())
163         psb.set(BEANTRAVERSE_ignoreRecursions, bool(a.ignoreRecursions()));
164      if (! a.initialDepth().isEmpty())
165         psb.set(BEANTRAVERSE_initialDepth, integer(a.initialDepth(), "initialDepth"));
166      if (! a.maxDepth().isEmpty())
167         psb.set(BEANTRAVERSE_maxDepth, integer(a.maxDepth(), "maxDepth"));
168   }
169
170   private Locale locale(String in) {
171      return Locale.forLanguageTag(string(in));
172   }
173
174   private MediaType mediaType(String in) {
175      return MediaType.forString(string(in));
176   }
177
178   private TimeZone timeZone(String in) {
179      return TimeZone.getTimeZone(string(in));
180   }
181
182   private <T> T parse(Class<T> c, String in, String loc) {
183      try {
184         return SimpleJson.DEFAULT.read(string(in), c);
185      } catch (ParseException e) {
186         throw new ConfigException("Invalid syntax for visibility on annotation @BeanConfig({0}): {1}", loc, in);
187      }
188   }
189}