You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractBeanPropertyDefinition.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import java.beans.PropertyDescriptor;
  18. import com.vaadin.shared.util.SharedUtil;
  19. import com.vaadin.util.ReflectTools;
  20. /**
  21. * Abstract base class for PropertyDefinition implementations for beans.
  22. *
  23. * @author Vaadin Ltd
  24. * @since 8.2
  25. *
  26. * @param <T>
  27. * the type of the property set
  28. * @param <V>
  29. * the property type
  30. */
  31. public abstract class AbstractBeanPropertyDefinition<T, V>
  32. implements PropertyDefinition<T, V> {
  33. private final PropertyDescriptor descriptor;
  34. private final BeanPropertySet<T> propertySet;
  35. private final Class<?> propertyHolderType;
  36. /**
  37. * Constructor for setting the immutable descriptor, property set and
  38. * property holder type used by this instance.
  39. *
  40. * @param propertySet
  41. * property set this property belongs to
  42. * @param parent
  43. * parent property for this nested property
  44. * @param descriptor
  45. * property descriptor
  46. */
  47. public AbstractBeanPropertyDefinition(BeanPropertySet<T> propertySet,
  48. Class<?> propertyHolderType, PropertyDescriptor descriptor) {
  49. this.propertySet = propertySet;
  50. this.propertyHolderType = propertyHolderType;
  51. this.descriptor = descriptor;
  52. if (descriptor.getReadMethod() == null) {
  53. throw new IllegalArgumentException(
  54. "Bean property has no accessible getter: "
  55. + propertySet.getBeanType() + "."
  56. + descriptor.getName());
  57. }
  58. }
  59. @SuppressWarnings("unchecked")
  60. @Override
  61. public Class<V> getType() {
  62. return (Class<V>) ReflectTools
  63. .convertPrimitiveType(descriptor.getPropertyType());
  64. }
  65. @Override
  66. public String getName() {
  67. return descriptor.getName();
  68. }
  69. @Override
  70. public String getCaption() {
  71. return SharedUtil.propertyIdToHumanFriendly(getName());
  72. }
  73. @Override
  74. public BeanPropertySet<T> getPropertySet() {
  75. return propertySet;
  76. }
  77. /**
  78. * Gets the property descriptor of this instance.
  79. *
  80. * @return the property descriptor
  81. */
  82. protected PropertyDescriptor getDescriptor() {
  83. return descriptor;
  84. }
  85. @Override
  86. public Class<?> getPropertyHolderType() {
  87. return propertyHolderType;
  88. }
  89. }