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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.metadata;
  5. import java.util.Collection;
  6. import java.util.Collections;
  7. import java.util.HashMap;
  8. import java.util.HashSet;
  9. import java.util.Map;
  10. import java.util.Set;
  11. import com.vaadin.terminal.gwt.client.communication.JSONSerializer;
  12. public class TypeDataStore {
  13. private static final String CONSTRUCTOR_NAME = "!new";
  14. private final Map<String, Class<?>> identifiers = new HashMap<String, Class<?>>();
  15. private final Map<Type, Invoker> serializerFactories = new HashMap<Type, Invoker>();
  16. private final Map<Type, ProxyHandler> proxyHandlers = new HashMap<Type, ProxyHandler>();
  17. private final Map<Type, Collection<Property>> properties = new HashMap<Type, Collection<Property>>();
  18. private final Set<Method> delayedMethods = new HashSet<Method>();
  19. private final Set<Method> lastonlyMethods = new HashSet<Method>();
  20. private final Map<Method, Type> returnTypes = new HashMap<Method, Type>();
  21. private final Map<Method, Invoker> invokers = new HashMap<Method, Invoker>();
  22. private final Map<Method, Type[]> paramTypes = new HashMap<Method, Type[]>();
  23. private final Map<Property, Type> propertyTypes = new HashMap<Property, Type>();
  24. private final Map<Property, Invoker> setters = new HashMap<Property, Invoker>();
  25. private final Map<Property, Invoker> getters = new HashMap<Property, Invoker>();
  26. private final Map<Property, String> delegateToWidget = new HashMap<Property, String>();
  27. public static TypeDataStore get() {
  28. return ConnectorBundleLoader.get().getTypeDataStore();
  29. }
  30. public void setClass(String identifier, Class<?> type) {
  31. identifiers.put(identifier, type);
  32. }
  33. public static Class<?> getClass(String identifier) throws NoDataException {
  34. Class<?> class1 = get().identifiers.get(identifier);
  35. if (class1 == null) {
  36. throw new NoDataException("There is not class for identifier "
  37. + identifier);
  38. }
  39. return class1;
  40. }
  41. public static Type getType(Class<?> clazz) {
  42. return new Type(clazz);
  43. }
  44. public static Type getReturnType(Method method) throws NoDataException {
  45. Type type = get().returnTypes.get(method);
  46. if (type == null) {
  47. throw new NoDataException("There is return type for "
  48. + method.getSignature());
  49. }
  50. return type;
  51. }
  52. public static Invoker getInvoker(Method method) throws NoDataException {
  53. Invoker invoker = get().invokers.get(method);
  54. if (invoker == null) {
  55. throw new NoDataException("There is invoker for "
  56. + method.getSignature());
  57. }
  58. return invoker;
  59. }
  60. public static Invoker getConstructor(Type type) throws NoDataException {
  61. Invoker invoker = get().invokers
  62. .get(new Method(type, CONSTRUCTOR_NAME));
  63. if (invoker == null) {
  64. throw new NoDataException("There is constructor for "
  65. + type.getSignature());
  66. }
  67. return invoker;
  68. }
  69. public static Invoker getGetter(Property property) throws NoDataException {
  70. Invoker getter = get().getters.get(property);
  71. if (getter == null) {
  72. throw new NoDataException("There is getter for "
  73. + property.getSignature());
  74. }
  75. return getter;
  76. }
  77. public void setGetter(Class<?> clazz, String propertyName, Invoker invoker) {
  78. getters.put(new Property(getType(clazz), propertyName), invoker);
  79. }
  80. public static String getDelegateToWidget(Property property) {
  81. return get().delegateToWidget.get(property);
  82. }
  83. public void setReturnType(Class<?> type, String methodName, Type returnType) {
  84. returnTypes.put(new Method(getType(type), methodName), returnType);
  85. }
  86. public void setConstructor(Class<?> type, Invoker constructor) {
  87. setInvoker(type, CONSTRUCTOR_NAME, constructor);
  88. }
  89. public void setInvoker(Class<?> type, String methodName, Invoker invoker) {
  90. invokers.put(new Method(getType(type), methodName), invoker);
  91. }
  92. public static Type[] getParamTypes(Method method) throws NoDataException {
  93. Type[] types = get().paramTypes.get(method);
  94. if (types == null) {
  95. throw new NoDataException("There are no parameter type data for "
  96. + method.getSignature());
  97. }
  98. return types;
  99. }
  100. public void setParamTypes(Class<?> type, String methodName,
  101. Type[] paramTypes) {
  102. this.paramTypes.put(new Method(getType(type), methodName), paramTypes);
  103. }
  104. public static boolean hasIdentifier(String identifier) {
  105. return get().identifiers.containsKey(identifier);
  106. }
  107. public static ProxyHandler getProxyHandler(Type type)
  108. throws NoDataException {
  109. ProxyHandler proxyHandler = get().proxyHandlers.get(type);
  110. if (proxyHandler == null) {
  111. throw new NoDataException("No proxy handler for "
  112. + type.getSignature());
  113. }
  114. return proxyHandler;
  115. }
  116. public void setProxyHandler(Class<?> type, ProxyHandler proxyHandler) {
  117. proxyHandlers.put(getType(type), proxyHandler);
  118. }
  119. public static boolean isDelayed(Method method) {
  120. return get().delayedMethods.contains(method);
  121. }
  122. public void setDelayed(Class<?> type, String methodName) {
  123. delayedMethods.add(getType(type).getMethod(methodName));
  124. }
  125. public static boolean isLastonly(Method method) {
  126. return get().lastonlyMethods.contains(method);
  127. }
  128. public void setLastonly(Class<?> clazz, String methodName) {
  129. lastonlyMethods.add(getType(clazz).getMethod(methodName));
  130. }
  131. public static Collection<Property> getProperties(Type type)
  132. throws NoDataException {
  133. Collection<Property> properties = get().properties.get(type);
  134. if (properties == null) {
  135. throw new NoDataException("No property list for "
  136. + type.getSignature());
  137. }
  138. return properties;
  139. }
  140. public void setProperties(Class<?> clazz, String[] propertyNames) {
  141. Set<Property> properties = new HashSet<Property>();
  142. Type type = getType(clazz);
  143. for (String name : propertyNames) {
  144. properties.add(new Property(type, name));
  145. }
  146. this.properties.put(type, Collections.unmodifiableSet(properties));
  147. }
  148. public static Type getType(Property property) throws NoDataException {
  149. Type type = get().propertyTypes.get(property);
  150. if (type == null) {
  151. throw new NoDataException("No return type for "
  152. + property.getSignature());
  153. }
  154. return type;
  155. }
  156. public void setPropertyType(Class<?> clazz, String propertName, Type type) {
  157. propertyTypes.put(new Property(getType(clazz), propertName), type);
  158. }
  159. public static Invoker getSetter(Property property) throws NoDataException {
  160. Invoker setter = get().setters.get(property);
  161. if (setter == null) {
  162. throw new NoDataException("No setter for "
  163. + property.getSignature());
  164. }
  165. return setter;
  166. }
  167. public void setSetter(Class<?> clazz, String propertyName, Invoker setter) {
  168. setters.put(new Property(getType(clazz), propertyName), setter);
  169. }
  170. public void setSerializerFactory(Class<?> clazz, Invoker factory) {
  171. serializerFactories.put(getType(clazz), factory);
  172. }
  173. public static JSONSerializer<?> findSerializer(Type type) {
  174. Invoker factoryCreator = get().serializerFactories.get(type);
  175. if (factoryCreator == null) {
  176. return null;
  177. }
  178. return (JSONSerializer<?>) factoryCreator.invoke(null);
  179. }
  180. }