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.transform;
014
015import static org.apache.juneau.internal.StringUtils.*;
016import java.util.*;
017
018import org.apache.juneau.*;
019import org.apache.juneau.annotation.*;
020import org.apache.juneau.reflect.*;
021
022/**
023 * Simple bean filter that simply identifies a class to be used as an interface class for all child classes.
024 *
025 * <p>
026 * These objects are created when you pass in non-<c>BeanFilterBuilder</c> classes to
027 * {@link BeanContextBuilder#beanFilters(Class...)}, and are equivalent to adding a
028 * <code><ja>@Bean</ja>(interfaceClass=Foo.<jk>class</jk>)</code> annotation on the <c>Foo</c> class.
029 *
030 * @param <T> The interface class.
031 */
032public class InterfaceBeanFilterBuilder<T> extends BeanFilterBuilder<T> {
033
034   /**
035    * Constructor.
036    *
037    * <p>
038    * Interface class is determined through reflection.
039    */
040   protected InterfaceBeanFilterBuilder() {
041      init(beanClass);
042   }
043
044   /**
045    * Constructor.
046    *
047    * @param interfaceClass The class to use as an interface on all child classes.
048    */
049   public InterfaceBeanFilterBuilder(Class<T> interfaceClass) {
050      super(interfaceClass);
051      init(interfaceClass);
052   }
053
054   @SuppressWarnings("deprecation")
055   private void init(Class<?> interfaceClass) {
056      interfaceClass(interfaceClass);
057      List<Bean> annotations = ClassInfo.of(interfaceClass).getAnnotations(Bean.class);
058
059      ListIterator<Bean> li = annotations.listIterator(annotations.size());
060      while (li.hasPrevious()) {
061         Bean b = li.previous();
062
063         if (! b.properties().isEmpty())
064            bpi(split(b.properties()));
065
066         if (! b.excludeProperties().isEmpty())
067            bpx(split(b.excludeProperties()));
068
069         if (! b.bpi().isEmpty())
070            bpi(split(b.bpi()));
071
072         if (! b.bpx().isEmpty())
073            bpx(split(b.bpx()));
074
075         if (! b.bpro().isEmpty())
076            bpro(split(b.bpro()));
077
078         if (! b.bpwo().isEmpty())
079            bpwo(split(b.bpwo()));
080
081         if (! b.typeName().isEmpty())
082            typeName(b.typeName());
083
084         if (b.sort())
085            sortProperties(true);
086
087         if (b.fluentSetters())
088            fluentSetters(true);
089
090         try {
091            if (b.propertyNamer() != PropertyNamerDefault.class)
092               propertyNamer(b.propertyNamer());
093         } catch (Exception e) {
094            throw new RuntimeException(e);
095         }
096
097         if (b.interfaceClass() != Object.class)
098            interfaceClass(b.interfaceClass());
099
100         if (b.stopClass() != Object.class)
101            stopClass(b.stopClass());
102
103         if (b.beanDictionary().length > 0)
104            dictionary(b.beanDictionary());
105
106         if (b.dictionary().length > 0)
107            dictionary(b.dictionary());
108      }
109   }
110}