From: Aleksi Hietanen Date: Tue, 21 Nov 2017 12:24:10 +0000 (+0200) Subject: Extract AbstractBeanPropertyDefinition and PropertyFilterDefinition (#10344) X-Git-Tag: 8.3.0.alpha1~45 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0fbeb0a6261b5a9f6485928965f0932fea0e624f;p=vaadin-framework.git Extract AbstractBeanPropertyDefinition and PropertyFilterDefinition (#10344) This commit extracts the private nested class AbstractBeanPropertyDefinition from BeanPropertySet to a public class of package com.vaadin.data. Additionally, the nested protected class PropertyFilterDefinition is extracted from NestedBeanPropertyDefinition to a public class in package com.vaadin.data. --- diff --git a/server/src/main/java/com/vaadin/data/AbstractBeanPropertyDefinition.java b/server/src/main/java/com/vaadin/data/AbstractBeanPropertyDefinition.java new file mode 100644 index 0000000000..3d8bd7ad0b --- /dev/null +++ b/server/src/main/java/com/vaadin/data/AbstractBeanPropertyDefinition.java @@ -0,0 +1,101 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.data; + +import java.beans.PropertyDescriptor; + +import com.vaadin.shared.util.SharedUtil; +import com.vaadin.util.ReflectTools; + +/** + * Abstract base class for PropertyDefinition implementations for beans. + * + * @author Vaadin Ltd + * @since 8.2 + * + * @param + * the type of the property set + * @param + * the property type + */ +public abstract class AbstractBeanPropertyDefinition + implements PropertyDefinition { + + private final PropertyDescriptor descriptor; + private final BeanPropertySet propertySet; + private final Class propertyHolderType; + + /** + * Constructor for setting the immutable descriptor, property set and + * property holder type used by this instance. + * + * @param propertySet + * property set this property belongs to + * @param parent + * parent property for this nested property + * @param descriptor + * property descriptor + */ + public AbstractBeanPropertyDefinition(BeanPropertySet propertySet, + Class propertyHolderType, PropertyDescriptor descriptor) { + this.propertySet = propertySet; + this.propertyHolderType = propertyHolderType; + this.descriptor = descriptor; + + if (descriptor.getReadMethod() == null) { + throw new IllegalArgumentException( + "Bean property has no accessible getter: " + + propertySet.getBeanType() + "." + + descriptor.getName()); + } + } + + @SuppressWarnings("unchecked") + @Override + public Class getType() { + return (Class) ReflectTools + .convertPrimitiveType(descriptor.getPropertyType()); + } + + @Override + public String getName() { + return descriptor.getName(); + } + + @Override + public String getCaption() { + return SharedUtil.propertyIdToHumanFriendly(getName()); + } + + @Override + public BeanPropertySet getPropertySet() { + return propertySet; + } + + /** + * Gets the property descriptor of this instance. + * + * @return the property descriptor + */ + protected PropertyDescriptor getDescriptor() { + return descriptor; + } + + @Override + public Class getPropertyHolderType() { + return propertyHolderType; + } +} diff --git a/server/src/main/java/com/vaadin/data/BeanPropertySet.java b/server/src/main/java/com/vaadin/data/BeanPropertySet.java index fbd60c8563..b302c83709 100644 --- a/server/src/main/java/com/vaadin/data/BeanPropertySet.java +++ b/server/src/main/java/com/vaadin/data/BeanPropertySet.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -35,8 +34,6 @@ import java.util.stream.Stream; import com.vaadin.data.util.BeanUtil; import com.vaadin.server.Setter; -import com.vaadin.shared.util.SharedUtil; -import com.vaadin.util.ReflectTools; /** * A {@link PropertySet} that uses reflection to find bean properties. @@ -106,58 +103,6 @@ public class BeanPropertySet implements PropertySet { } } - private abstract static class AbstractBeanPropertyDefinition - implements PropertyDefinition { - private final PropertyDescriptor descriptor; - private final BeanPropertySet propertySet; - private final Class propertyHolderType; - - public AbstractBeanPropertyDefinition(BeanPropertySet propertySet, - Class propertyHolderType, PropertyDescriptor descriptor) { - this.propertySet = propertySet; - this.propertyHolderType = propertyHolderType; - this.descriptor = descriptor; - - if (descriptor.getReadMethod() == null) { - throw new IllegalArgumentException( - "Bean property has no accessible getter: " - + propertySet.beanType + "." - + descriptor.getName()); - } - } - - @SuppressWarnings("unchecked") - @Override - public Class getType() { - return (Class) ReflectTools - .convertPrimitiveType(descriptor.getPropertyType()); - } - - @Override - public String getName() { - return descriptor.getName(); - } - - @Override - public String getCaption() { - return SharedUtil.propertyIdToHumanFriendly(getName()); - } - - @Override - public BeanPropertySet getPropertySet() { - return propertySet; - } - - protected PropertyDescriptor getDescriptor() { - return descriptor; - } - - @Override - public Class getPropertyHolderType() { - return propertyHolderType; - } - } - private static class BeanPropertyDefinition extends AbstractBeanPropertyDefinition { @@ -223,63 +168,6 @@ public class BeanPropertySet implements PropertySet { */ protected static final int MAX_PROPERTY_NESTING_DEPTH = 10; - /** - * Class containing the constraints for filtering nested properties. - * - * @since 8.2 - * - */ - protected static class PropertyFilterDefinition - implements Serializable { - private int maxNestingDepth; - private List ignorePackageNamesStartingWith; - - /** - * Create a property filter with max nesting depth and package names - * to ignore. - * - * @param maxNestingDepth - * The maximum amount of nesting levels for - * sub-properties. - * @param ignorePackageNamesStartingWith - * Ignore package names that start with this string, for - * example "java.lang". - */ - public PropertyFilterDefinition(int maxNestingDepth, - List ignorePackageNamesStartingWith) { - this.maxNestingDepth = maxNestingDepth; - this.ignorePackageNamesStartingWith = ignorePackageNamesStartingWith; - } - - /** - * Returns the maximum amount of nesting levels for sub-properties. - * - * @return maximum nesting depth - */ - public int getMaxNestingDepth() { - return maxNestingDepth; - } - - /** - * Returns a list of package name prefixes to ignore. - * - * @return list of strings that - */ - public List getIgnorePackageNamesStartingWith() { - return ignorePackageNamesStartingWith; - } - - /** - * Get the default nested property filtering conditions. - * - * @return default property filter - */ - public static PropertyFilterDefinition getDefaultFilter() { - return new PropertyFilterDefinition(MAX_PROPERTY_NESTING_DEPTH, - Arrays.asList("java")); - } - } - private final PropertyDefinition parent; private boolean useLongFormName = false; @@ -295,7 +183,7 @@ public class BeanPropertySet implements PropertySet { * Create nested property definition. Allows use of a long form name. * * @param propertySet - * property set this property belongs is. + * property set this property belongs to * @param parent * parent property for this nested property * @param descriptor @@ -463,7 +351,7 @@ public class BeanPropertySet implements PropertySet { } private BeanPropertySet(Class beanType, boolean checkNestedDefinitions, - NestedBeanPropertyDefinition.PropertyFilterDefinition propertyFilterDefinition) { + PropertyFilterDefinition propertyFilterDefinition) { this(beanType); if (checkNestedDefinitions) { Objects.requireNonNull(propertyFilterDefinition, @@ -474,7 +362,7 @@ public class BeanPropertySet implements PropertySet { private void findNestedDefinitions( Map> parentDefinitions, int depth, - NestedBeanPropertyDefinition.PropertyFilterDefinition filterCallback) { + PropertyFilterDefinition filterCallback) { if (depth >= filterCallback.getMaxNestingDepth()) { return; } @@ -554,7 +442,7 @@ public class BeanPropertySet implements PropertySet { @SuppressWarnings("unchecked") public static PropertySet get(Class beanType, boolean checkNestedDefinitions, - NestedBeanPropertyDefinition.PropertyFilterDefinition filterDefinition) { + PropertyFilterDefinition filterDefinition) { Objects.requireNonNull(beanType, "Bean type cannot be null"); InstanceKey key = new InstanceKey(beanType, false, filterDefinition.getMaxNestingDepth(), @@ -608,6 +496,16 @@ public class BeanPropertySet implements PropertySet { return definition; } + /** + * Gets the bean type of this bean property set. + * + * @since 8.2 + * @return the bean type of this bean property set + */ + public Class getBeanType() { + return beanType; + } + private static boolean hasNonObjectReadMethod( PropertyDescriptor descriptor) { Method readMethod = descriptor.getReadMethod(); diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index 96739f175d..0fe357e368 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -38,7 +38,6 @@ import java.util.stream.Stream; import com.googlecode.gentyref.GenericTypeReflector; import com.vaadin.annotations.PropertyId; -import com.vaadin.data.BeanPropertySet.NestedBeanPropertyDefinition.PropertyFilterDefinition; import com.vaadin.data.HasValue.ValueChangeEvent; import com.vaadin.data.HasValue.ValueChangeListener; import com.vaadin.data.converter.StringToIntegerConverter; diff --git a/server/src/main/java/com/vaadin/data/PropertyFilterDefinition.java b/server/src/main/java/com/vaadin/data/PropertyFilterDefinition.java new file mode 100644 index 0000000000..47d5812735 --- /dev/null +++ b/server/src/main/java/com/vaadin/data/PropertyFilterDefinition.java @@ -0,0 +1,77 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.data; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.List; + +/** + * Class containing the constraints for filtering nested properties. + * + * @author Vaadin Ltd + * @since 8.2 + */ +public class PropertyFilterDefinition + implements Serializable { + private int maxNestingDepth; + private List ignorePackageNamesStartingWith; + + /** + * Create a property filter with max nesting depth and package names + * to ignore. + * + * @param maxNestingDepth + * The maximum amount of nesting levels for + * sub-properties. + * @param ignorePackageNamesStartingWith + * Ignore package names that start with this string, for + * example "java.lang". + */ + public PropertyFilterDefinition(int maxNestingDepth, + List ignorePackageNamesStartingWith) { + this.maxNestingDepth = maxNestingDepth; + this.ignorePackageNamesStartingWith = ignorePackageNamesStartingWith; + } + + /** + * Returns the maximum amount of nesting levels for sub-properties. + * + * @return maximum nesting depth + */ + public int getMaxNestingDepth() { + return maxNestingDepth; + } + + /** + * Returns a list of package name prefixes to ignore. + * + * @return list of strings that + */ + public List getIgnorePackageNamesStartingWith() { + return ignorePackageNamesStartingWith; + } + + /** + * Get the default nested property filtering conditions. + * + * @return default property filter + */ + public static PropertyFilterDefinition getDefaultFilter() { + return new PropertyFilterDefinition(BeanPropertySet.NestedBeanPropertyDefinition.MAX_PROPERTY_NESTING_DEPTH, + Arrays.asList("java")); + } +} diff --git a/server/src/test/java/com/vaadin/data/BinderInstanceFieldTest.java b/server/src/test/java/com/vaadin/data/BinderInstanceFieldTest.java index 03f98bdc08..dc6e79879f 100644 --- a/server/src/test/java/com/vaadin/data/BinderInstanceFieldTest.java +++ b/server/src/test/java/com/vaadin/data/BinderInstanceFieldTest.java @@ -26,7 +26,6 @@ import org.junit.Assert; import org.junit.Test; import com.vaadin.annotations.PropertyId; -import com.vaadin.data.BeanPropertySet.NestedBeanPropertyDefinition.PropertyFilterDefinition; import com.vaadin.data.converter.StringToIntegerConverter; import com.vaadin.data.validator.StringLengthValidator; import com.vaadin.tests.data.bean.Address;