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#interfaceClass(Class, 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, BeanContext.DEFAULT);
042   }
043
044   /**
045    * Constructor.
046    *
047    * @param interfaceClass The class to use as an interface on all child classes.
048    * @param bc The bean context to use for looking up annotations.
049    */
050   public InterfaceBeanFilterBuilder(Class<T> interfaceClass, BeanContext bc) {
051      super(interfaceClass);
052      init(interfaceClass, bc);
053   }
054
055   @SuppressWarnings("deprecation")
056   private void init(Class<?> interfaceClass, BeanContext bc) {
057      interfaceClass(interfaceClass);
058      List<Bean> annotations = ClassInfo.of(interfaceClass).getAnnotations(Bean.class, bc);
059
060      for (Bean b : annotations) {
061
062         if (! b.properties().isEmpty())
063            bpi(split(b.properties()));
064
065         if (! b.excludeProperties().isEmpty())
066            bpx(split(b.excludeProperties()));
067
068         if (! b.bpi().isEmpty())
069            bpi(split(b.bpi()));
070
071         if (! b.bpx().isEmpty())
072            bpx(split(b.bpx()));
073
074         if (! b.bpro().isEmpty())
075            bpro(split(b.bpro()));
076
077         if (! b.bpwo().isEmpty())
078            bpwo(split(b.bpwo()));
079
080         if (! b.typeName().isEmpty())
081            typeName(b.typeName());
082
083         if (b.sort())
084            sortProperties(true);
085
086         if (b.fluentSetters())
087            fluentSetters(true);
088
089         try {
090            if (b.propertyNamer() != PropertyNamerDefault.class)
091               propertyNamer(b.propertyNamer());
092         } catch (Exception e) {
093            throw new RuntimeException(e);
094         }
095
096         if (b.interfaceClass() != Object.class)
097            interfaceClass(b.interfaceClass());
098
099         if (b.stopClass() != Object.class)
100            stopClass(b.stopClass());
101
102         if (b.beanDictionary().length > 0)
103            dictionary(b.beanDictionary());
104
105         if (b.dictionary().length > 0)
106            dictionary(b.dictionary());
107      }
108   }
109}