Browse Source

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.
tags/8.3.0.alpha1
Aleksi Hietanen 6 years ago
parent
commit
0fbeb0a626
No account linked to committer's email address

+ 101
- 0
server/src/main/java/com/vaadin/data/AbstractBeanPropertyDefinition.java View File

@@ -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 <T>
* the type of the property set
* @param <V>
* the property type
*/
public abstract class AbstractBeanPropertyDefinition<T, V>
implements PropertyDefinition<T, V> {

private final PropertyDescriptor descriptor;
private final BeanPropertySet<T> 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<T> 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<V> getType() {
return (Class<V>) ReflectTools
.convertPrimitiveType(descriptor.getPropertyType());
}

@Override
public String getName() {
return descriptor.getName();
}

@Override
public String getCaption() {
return SharedUtil.propertyIdToHumanFriendly(getName());
}

@Override
public BeanPropertySet<T> 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;
}
}

+ 14
- 116
server/src/main/java/com/vaadin/data/BeanPropertySet.java View File

@@ -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<T> implements PropertySet<T> {
}
}

private abstract static class AbstractBeanPropertyDefinition<T, V>
implements PropertyDefinition<T, V> {
private final PropertyDescriptor descriptor;
private final BeanPropertySet<T> propertySet;
private final Class<?> propertyHolderType;

public AbstractBeanPropertyDefinition(BeanPropertySet<T> 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<V> getType() {
return (Class<V>) ReflectTools
.convertPrimitiveType(descriptor.getPropertyType());
}

@Override
public String getName() {
return descriptor.getName();
}

@Override
public String getCaption() {
return SharedUtil.propertyIdToHumanFriendly(getName());
}

@Override
public BeanPropertySet<T> getPropertySet() {
return propertySet;
}

protected PropertyDescriptor getDescriptor() {
return descriptor;
}

@Override
public Class<?> getPropertyHolderType() {
return propertyHolderType;
}
}

private static class BeanPropertyDefinition<T, V>
extends AbstractBeanPropertyDefinition<T, V> {

@@ -223,63 +168,6 @@ public class BeanPropertySet<T> implements PropertySet<T> {
*/
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<String> 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<String> 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<String> 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<T, ?> parent;

private boolean useLongFormName = false;
@@ -295,7 +183,7 @@ public class BeanPropertySet<T> implements PropertySet<T> {
* 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<T> implements PropertySet<T> {
}

private BeanPropertySet(Class<T> beanType, boolean checkNestedDefinitions,
NestedBeanPropertyDefinition.PropertyFilterDefinition propertyFilterDefinition) {
PropertyFilterDefinition propertyFilterDefinition) {
this(beanType);
if (checkNestedDefinitions) {
Objects.requireNonNull(propertyFilterDefinition,
@@ -474,7 +362,7 @@ public class BeanPropertySet<T> implements PropertySet<T> {

private void findNestedDefinitions(
Map<String, PropertyDefinition<T, ?>> parentDefinitions, int depth,
NestedBeanPropertyDefinition.PropertyFilterDefinition filterCallback) {
PropertyFilterDefinition filterCallback) {
if (depth >= filterCallback.getMaxNestingDepth()) {
return;
}
@@ -554,7 +442,7 @@ public class BeanPropertySet<T> implements PropertySet<T> {
@SuppressWarnings("unchecked")
public static <T> PropertySet<T> get(Class<? extends T> 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<T> implements PropertySet<T> {
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<T> getBeanType() {
return beanType;
}

private static boolean hasNonObjectReadMethod(
PropertyDescriptor descriptor) {
Method readMethod = descriptor.getReadMethod();

+ 0
- 1
server/src/main/java/com/vaadin/data/Binder.java View File

@@ -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;

+ 77
- 0
server/src/main/java/com/vaadin/data/PropertyFilterDefinition.java View File

@@ -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<String> 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<String> 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<String> 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"));
}
}

+ 0
- 1
server/src/test/java/com/vaadin/data/BinderInstanceFieldTest.java View File

@@ -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;

Loading…
Cancel
Save