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   @Override
043   public void apply(AnnotationInfo<BeanConfig> ai, PropertyStoreBuilder psb) {
044      BeanConfig a = ai.getAnnotation();
045      if (! a.beanClassVisibility().isEmpty())
046         psb.set(BEAN_beanClassVisibility, visibility(a.beanClassVisibility(), "beanClassVisibility"));
047      if (! a.beanConstructorVisibility().isEmpty())
048         psb.set(BEAN_beanConstructorVisibility, visibility(a.beanConstructorVisibility(), "beanConstructorVisibility"));
049      if (a.beanDictionary().length != 0)
050         psb.addTo(BEAN_beanDictionary, a.beanDictionary());
051      if (a.beanDictionary_replace().length != 0)
052         psb.set(BEAN_beanDictionary, a.beanDictionary_replace());
053      if (a.beanDictionary_remove().length != 0)
054         psb.removeFrom(BEAN_beanDictionary, a.beanDictionary_remove());
055      if (! a.beanFieldVisibility().isEmpty())
056         psb.set(BEAN_beanFieldVisibility, visibility(a.beanFieldVisibility(), "beanFieldVisibility"));
057      if (a.beanFilters().length != 0)
058         psb.addTo(BEAN_beanFilters, a.beanFilters());
059      if (a.beanFilters_replace().length != 0)
060         psb.set(BEAN_beanFilters, a.beanFilters_replace());
061      if (a.beanFilters_remove().length != 0)
062         psb.removeFrom(BEAN_beanFilters, a.beanFilters_remove());
063      if (! a.beanMapPutReturnsOldValue().isEmpty())
064         psb.set(BEAN_beanMapPutReturnsOldValue, bool(a.beanMapPutReturnsOldValue()));
065      if (! a.beanMethodVisibility().isEmpty())
066         psb.set(BEAN_beanMethodVisibility, visibility(a.beanMethodVisibility(), "beanMethodVisibility"));
067      if (! a.beansRequireDefaultConstructor().isEmpty())
068         psb.set(BEAN_beansRequireDefaultConstructor, bool(a.beansRequireDefaultConstructor()));
069      if (! a.beansRequireSerializable().isEmpty())
070         psb.set(BEAN_beansRequireSerializable, bool(a.beansRequireSerializable()));
071      if (! a.beansRequireSettersForGetters().isEmpty())
072         psb.set(BEAN_beansRequireSettersForGetters, bool(a.beansRequireSettersForGetters()));
073      if (! a.beansRequireSomeProperties().isEmpty())
074         psb.set(BEAN_beansRequireSomeProperties, bool(a.beansRequireSomeProperties()));
075      if (! a.beanTypePropertyName().isEmpty())
076         psb.set(BEAN_beanTypePropertyName, string(a.beanTypePropertyName()));
077      if (a.bpi().length > 0)
078         psb.addTo(BEAN_includeProperties, stringsMap(a.bpi(), "bpi"));
079      if (a.bpx().length > 0)
080         psb.addTo(BEAN_excludeProperties, stringsMap(a.bpi(), "bpx"));
081      if (! a.debug().isEmpty())
082         psb.set(BEAN_debug, bool(a.debug()));
083      for (CS e : a.example())
084         psb.addTo(BEAN_examples, e.k().getName(), parse(e.k(), e.v(), "example"));
085      if (a.examples().length > 0)
086         psb.addTo(BEAN_examples, objectMap(a.examples(), "examples"));
087      for (CS e : a.excludeProperties())
088         psb.addTo(BEAN_excludeProperties, e.k().getName(), string(e.v()));
089      if (! a.fluentSetters().isEmpty())
090         psb.set(BEAN_fluentSetters, bool(a.fluentSetters()));
091      if (! a.ignoreInvocationExceptionsOnGetters().isEmpty())
092         psb.set(BEAN_ignoreInvocationExceptionsOnGetters, bool(a.ignoreInvocationExceptionsOnGetters()));
093      if (! a.ignoreInvocationExceptionsOnSetters().isEmpty())
094         psb.set(BEAN_ignoreInvocationExceptionsOnSetters, bool(a.ignoreInvocationExceptionsOnSetters()));
095      if (! a.ignorePropertiesWithoutSetters().isEmpty())
096         psb.set(BEAN_ignorePropertiesWithoutSetters, bool(a.ignorePropertiesWithoutSetters()));
097      if (! a.ignoreUnknownBeanProperties().isEmpty())
098         psb.set(BEAN_ignoreUnknownBeanProperties, bool(a.ignoreUnknownBeanProperties()));
099      if (! a.ignoreUnknownNullBeanProperties().isEmpty())
100         psb.set(BEAN_ignoreUnknownNullBeanProperties, bool(a.ignoreUnknownNullBeanProperties()));
101      for (CC e : a.implClasses())
102         psb.addTo(BEAN_implClasses, e.k().getName(), e.v());
103      for (CS e : a.includeProperties())
104         psb.addTo(BEAN_includeProperties, e.k().getName(), string(e.v()));
105      if (! a.locale().isEmpty())
106         psb.set(BEAN_locale, locale(a.locale()));
107      if (! a.mediaType().isEmpty())
108         psb.set(BEAN_mediaType, mediaType(a.mediaType()));
109      if (a.notBeanClasses().length != 0)
110         psb.addTo(BEAN_notBeanClasses, a.notBeanClasses());
111      if (a.notBeanClasses_replace().length != 0)
112         psb.set(BEAN_notBeanClasses, a.notBeanClasses_replace());
113      if (a.notBeanClasses_remove().length != 0)
114         psb.removeFrom(BEAN_notBeanClasses, a.notBeanClasses_remove());
115      if (a.notBeanPackages().length != 0)
116         psb.addTo(BEAN_notBeanPackages, strings(a.notBeanPackages()));
117      if (a.notBeanPackages_replace().length != 0)
118         psb.set(BEAN_notBeanPackages, strings(a.notBeanPackages_replace()));
119      if (a.notBeanPackages_remove().length != 0)
120         psb.removeFrom(BEAN_notBeanPackages, strings(a.notBeanPackages_remove()));
121      if (a.pojoSwaps().length != 0)
122         psb.addTo(BEAN_pojoSwaps, a.pojoSwaps());
123      if (a.pojoSwaps_replace().length != 0)
124         psb.set(BEAN_pojoSwaps, a.pojoSwaps_replace());
125      if (a.pojoSwaps_remove().length != 0)
126         psb.removeFrom(BEAN_pojoSwaps, a.pojoSwaps_remove());
127      if (a.propertyNamer() != PropertyNamer.Null.class)
128         psb.set(BEAN_propertyNamer, a.propertyNamer());
129      if (! a.sortProperties().isEmpty())
130         psb.set(BEAN_sortProperties, bool(a.sortProperties()));
131      if (! a.timeZone().isEmpty())
132         psb.set(BEAN_timeZone, timeZone(a.timeZone()));
133      if (! a.useEnumNames().isEmpty())
134         psb.set(BEAN_useEnumNames, bool(a.useEnumNames()));
135      if (! a.useInterfaceProxies().isEmpty())
136         psb.set(BEAN_useInterfaceProxies, bool(a.useInterfaceProxies()));
137      if (! a.useJavaBeanIntrospector().isEmpty())
138         psb.set(BEAN_useJavaBeanIntrospector, bool(a.useJavaBeanIntrospector()));
139      if (! a.detectRecursions().isEmpty())
140         psb.set(BEANTRAVERSE_detectRecursions, bool(a.detectRecursions()));
141      if (! a.ignoreRecursions().isEmpty())
142         psb.set(BEANTRAVERSE_ignoreRecursions, bool(a.ignoreRecursions()));
143      if (! a.initialDepth().isEmpty())
144         psb.set(BEANTRAVERSE_initialDepth, integer(a.initialDepth(), "initialDepth"));
145      if (! a.maxDepth().isEmpty())
146         psb.set(BEANTRAVERSE_maxDepth, integer(a.maxDepth(), "maxDepth"));
147   }
148
149   private Locale locale(String in) {
150      return Locale.forLanguageTag(string(in));
151   }
152
153   private MediaType mediaType(String in) {
154      return MediaType.forString(string(in));
155   }
156
157   private TimeZone timeZone(String in) {
158      return TimeZone.getTimeZone(string(in));
159   }
160
161   private <T> T parse(Class<T> c, String in, String loc) {
162      try {
163         return SimpleJson.DEFAULT.read(string(in), c);
164      } catch (ParseException e) {
165         throw new ConfigException("Invalid syntax for visibility on annotation @BeanConfig({0}): {1}", loc, in);
166      }
167   }
168}