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   private void init(Class<?> interfaceClass) {
055      interfaceClass(interfaceClass);
056      List<Bean> annotations = ClassInfo.of(interfaceClass).getAnnotations(Bean.class);
057
058      ListIterator<Bean> li = annotations.listIterator(annotations.size());
059      while (li.hasPrevious()) {
060         Bean b = li.previous();
061
062         if (! b.properties().isEmpty())
063            properties(split(b.properties()));
064
065         if (! b.typeName().isEmpty())
066            typeName(b.typeName());
067
068         if (b.sort())
069            sortProperties(true);
070
071         if (b.fluentSetters())
072            fluentSetters(true);
073
074         if (! b.excludeProperties().isEmpty())
075            excludeProperties(split(b.excludeProperties()));
076
077         try {
078            if (b.propertyNamer() != PropertyNamerDefault.class)
079               propertyNamer(b.propertyNamer());
080         } catch (Exception e) {
081            throw new RuntimeException(e);
082         }
083
084         if (b.interfaceClass() != Object.class)
085            interfaceClass(b.interfaceClass());
086
087         if (b.stopClass() != Object.class)
088            stopClass(b.stopClass());
089
090         if (b.beanDictionary().length > 0)
091            beanDictionary(b.beanDictionary());
092      }
093   }
094}