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