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.

MethodPropertyDescriptor.java 5.1KB

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