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 static org.apache.juneau.internal.ClassUtils.*; 017 018import org.apache.juneau.*; 019import org.apache.juneau.annotation.*; 020 021/** 022 * Parent class for all bean filters. 023 * 024 * <p> 025 * Bean filters are used to control aspects of how beans are handled during serialization and parsing. 026 * 027 * <p> 028 * Bean filters are created by {@link BeanFilterBuilder} which is the programmatic equivalent to the {@link Bean @Bean} 029 * annotation. 030 * 031 * <ul class='seealso'> 032 * <li class='link'>{@doc juneau-marshall.Transforms.BeanFilters} 033 * </ul> 034 */ 035public final class BeanFilter { 036 037 private final Class<?> beanClass; 038 private final String[] properties, excludeProperties; 039 private final PropertyNamer propertyNamer; 040 private final Class<?> interfaceClass, stopClass; 041 private final boolean sortProperties, fluentSetters; 042 private final String typeName; 043 private final Class<?>[] beanDictionary; 044 private final PropertyFilter propertyFilter; 045 046 /** 047 * Constructor. 048 */ 049 BeanFilter(BeanFilterBuilder<?> builder) { 050 this.beanClass = builder.beanClass; 051 this.typeName = builder.typeName; 052 this.properties = split(builder.includeProperties, ','); 053 this.excludeProperties = split(builder.excludeProperties, ','); 054 this.interfaceClass = builder.interfaceClass; 055 this.stopClass = builder.stopClass; 056 this.sortProperties = builder.sortProperties; 057 this.fluentSetters = builder.fluentSetters; 058 this.propertyNamer = castOrCreate(PropertyNamer.class, builder.propertyNamer); 059 this.beanDictionary = 060 builder.beanDictionary == null 061 ? null 062 : builder.beanDictionary.toArray(new Class<?>[builder.beanDictionary.size()]); 063 this.propertyFilter = 064 builder.propertyFilter == null 065 ? PropertyFilter.DEFAULT 066 : castOrCreate(PropertyFilter.class, builder.propertyFilter); 067 } 068 069 /** 070 * Returns the bean class that this filter applies to. 071 * 072 * @return The bean class that this filter applies to. 073 */ 074 public Class<?> getBeanClass() { 075 return beanClass; 076 } 077 078 /** 079 * Returns the dictionary name associated with this bean. 080 * 081 * @return The dictionary name associated with this bean, or <jk>null</jk> if no name is defined. 082 */ 083 public String getTypeName() { 084 return typeName; 085 } 086 087 /** 088 * Returns the set and order of names of properties associated with a bean class. 089 * 090 * @return 091 * The name of the properties associated with a bean class, or <jk>null</jk> if all bean properties should 092 * be used. 093 */ 094 public String[] getProperties() { 095 return properties; 096 } 097 098 /** 099 * Returns the bean dictionary defined on this bean. 100 * 101 * @return The bean dictionary defined on this bean, or <jk>null</jk> if no bean dictionary is defined. 102 */ 103 public Class<?>[] getBeanDictionary() { 104 return beanDictionary; 105 } 106 107 /** 108 * Returns <jk>true</jk> if the properties defined on this bean class should be ordered alphabetically. 109 * 110 * <p> 111 * This method is only used when the {@link #getProperties()} method returns <jk>null</jk>. 112 * Otherwise, the ordering of the properties in the returned value is used. 113 * 114 * @return <jk>true</jk> if bean properties should be sorted. 115 */ 116 public boolean isSortProperties() { 117 return sortProperties; 118 } 119 120 /** 121 * Returns <jk>true</jk> if we should find fluent setters. 122 * 123 * @return <jk>true</jk> if fluent setters should be found. 124 */ 125 public boolean isFluentSetters() { 126 return fluentSetters; 127 } 128 129 /** 130 * Returns the list of properties to ignore on a bean. 131 * 132 * @return The name of the properties to ignore on a bean, or <jk>null</jk> to not ignore any properties. 133 */ 134 public String[] getExcludeProperties() { 135 return excludeProperties; 136 } 137 138 /** 139 * Returns the {@link PropertyNamer} associated with the bean to tailor the names of bean properties. 140 * 141 * @return The property namer class, or <jk>null</jk> if no property namer is associated with this bean property. 142 */ 143 public PropertyNamer getPropertyNamer() { 144 return propertyNamer; 145 } 146 147 /** 148 * Returns the interface class associated with this class. 149 * 150 * @return The interface class associated with this class, or <jk>null</jk> if no interface class is associated. 151 */ 152 public Class<?> getInterfaceClass() { 153 return interfaceClass; 154 } 155 156 /** 157 * Returns the stop class associated with this class. 158 * 159 * @return The stop class associated with this class, or <jk>null</jk> if no stop class is associated. 160 */ 161 public Class<?> getStopClass() { 162 return stopClass; 163 } 164 165 /** 166 * Calls the {@link PropertyFilter#readProperty(Object, String, Object)} method on the registered property filters. 167 * 168 * @param bean The bean from which the property was read. 169 * @param name The property name. 170 * @param value The value just extracted from calling the bean getter. 171 * @return The value to serialize. Default is just to return the existing value. 172 */ 173 public Object readProperty(Object bean, String name, Object value) { 174 return propertyFilter.readProperty(bean, name, value); 175 } 176 177 /** 178 * Calls the {@link PropertyFilter#writeProperty(Object, String, Object)} method on the registered property filters. 179 * 180 * @param bean The bean from which the property was read. 181 * @param name The property name. 182 * @param value The value just parsed. 183 * @return The value to serialize. Default is just to return the existing value. 184 */ 185 public Object writeProperty(Object bean, String name, Object value) { 186 return propertyFilter.writeProperty(bean, name, value); 187 } 188}