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.0KB

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