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

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