Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

MethodPropertyDescriptor.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.io.IOException;
  6. import java.lang.reflect.Method;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import com.vaadin.data.Property;
  10. import com.vaadin.util.SerializerHelper;
  11. /**
  12. * Property descriptor that is able to create simple {@link MethodProperty}
  13. * instances for a bean, using given accessors.
  14. *
  15. * @param <BT>
  16. * bean type
  17. *
  18. * @since 6.6
  19. */
  20. public class MethodPropertyDescriptor<BT> implements
  21. VaadinPropertyDescriptor<BT> {
  22. private final String name;
  23. private Class<?> propertyType;
  24. private transient Method readMethod;
  25. private transient Method writeMethod;
  26. /**
  27. * Creates a property descriptor that can create MethodProperty instances to
  28. * access the underlying bean property.
  29. *
  30. * @param name
  31. * of the property
  32. * @param propertyType
  33. * type (class) of the property
  34. * @param readMethod
  35. * getter {@link Method} for the property
  36. * @param writeMethod
  37. * setter {@link Method} for the property or null if read-only
  38. * property
  39. */
  40. public MethodPropertyDescriptor(String name, Class<?> propertyType,
  41. Method readMethod, Method writeMethod) {
  42. this.name = name;
  43. this.propertyType = propertyType;
  44. this.readMethod = readMethod;
  45. this.writeMethod = writeMethod;
  46. }
  47. /* Special serialization to handle method references */
  48. private void writeObject(java.io.ObjectOutputStream out) throws IOException {
  49. out.defaultWriteObject();
  50. SerializerHelper.writeClass(out, propertyType);
  51. if (writeMethod != null) {
  52. out.writeObject(writeMethod.getName());
  53. SerializerHelper.writeClass(out, writeMethod.getDeclaringClass());
  54. SerializerHelper.writeClassArray(out,
  55. writeMethod.getParameterTypes());
  56. } else {
  57. out.writeObject(null);
  58. out.writeObject(null);
  59. out.writeObject(null);
  60. }
  61. if (readMethod != null) {
  62. out.writeObject(readMethod.getName());
  63. SerializerHelper.writeClass(out, readMethod.getDeclaringClass());
  64. SerializerHelper.writeClassArray(out,
  65. readMethod.getParameterTypes());
  66. } else {
  67. out.writeObject(null);
  68. out.writeObject(null);
  69. out.writeObject(null);
  70. }
  71. }
  72. /* Special serialization to handle method references */
  73. private void readObject(java.io.ObjectInputStream in) throws IOException,
  74. ClassNotFoundException {
  75. in.defaultReadObject();
  76. try {
  77. @SuppressWarnings("unchecked")
  78. // business assumption; type parameters not checked at runtime
  79. Class<BT> class1 = (Class<BT>) SerializerHelper.readClass(in);
  80. propertyType = class1;
  81. String name = (String) in.readObject();
  82. Class<?> writeMethodClass = SerializerHelper.readClass(in);
  83. Class<?>[] paramTypes = SerializerHelper.readClassArray(in);
  84. if (name != null) {
  85. writeMethod = writeMethodClass.getMethod(name, paramTypes);
  86. } else {
  87. writeMethod = null;
  88. }
  89. name = (String) in.readObject();
  90. Class<?> readMethodClass = SerializerHelper.readClass(in);
  91. paramTypes = SerializerHelper.readClassArray(in);
  92. if (name != null) {
  93. readMethod = readMethodClass.getMethod(name, paramTypes);
  94. } else {
  95. readMethod = null;
  96. }
  97. } catch (SecurityException e) {
  98. getLogger().log(Level.SEVERE, "Internal deserialization error", e);
  99. } catch (NoSuchMethodException e) {
  100. getLogger().log(Level.SEVERE, "Internal deserialization error", e);
  101. }
  102. };
  103. @Override
  104. public String getName() {
  105. return name;
  106. }
  107. @Override
  108. public Class<?> getPropertyType() {
  109. return propertyType;
  110. }
  111. @Override
  112. public Property<?> createProperty(Object bean) {
  113. return new MethodProperty<Object>(propertyType, bean, readMethod,
  114. writeMethod);
  115. }
  116. private static final Logger getLogger() {
  117. return Logger.getLogger(MethodPropertyDescriptor.class.getName());
  118. }
  119. }