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.

TypeDataStore.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.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.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 setDelegateToWidget(Class<?> clazz, String propertyName,
  84. String delegateValue) {
  85. delegateToWidget.put(new Property(getType(clazz), propertyName),
  86. delegateValue);
  87. }
  88. public void setReturnType(Class<?> type, String methodName, Type returnType) {
  89. returnTypes.put(new Method(getType(type), methodName), returnType);
  90. }
  91. public void setConstructor(Class<?> type, Invoker constructor) {
  92. setInvoker(type, CONSTRUCTOR_NAME, constructor);
  93. }
  94. public void setInvoker(Class<?> type, String methodName, Invoker invoker) {
  95. invokers.put(new Method(getType(type), methodName), invoker);
  96. }
  97. public static Type[] getParamTypes(Method method) throws NoDataException {
  98. Type[] types = get().paramTypes.get(method);
  99. if (types == null) {
  100. throw new NoDataException("There are no parameter type data for "
  101. + method.getSignature());
  102. }
  103. return types;
  104. }
  105. public void setParamTypes(Class<?> type, String methodName,
  106. Type[] paramTypes) {
  107. this.paramTypes.put(new Method(getType(type), methodName), paramTypes);
  108. }
  109. public static boolean hasIdentifier(String identifier) {
  110. return get().identifiers.containsKey(identifier);
  111. }
  112. public static ProxyHandler getProxyHandler(Type type)
  113. throws NoDataException {
  114. ProxyHandler proxyHandler = get().proxyHandlers.get(type);
  115. if (proxyHandler == null) {
  116. throw new NoDataException("No proxy handler for "
  117. + type.getSignature());
  118. }
  119. return proxyHandler;
  120. }
  121. public void setProxyHandler(Class<?> type, ProxyHandler proxyHandler) {
  122. proxyHandlers.put(getType(type), proxyHandler);
  123. }
  124. public static boolean isDelayed(Method method) {
  125. return get().delayedMethods.contains(method);
  126. }
  127. public void setDelayed(Class<?> type, String methodName) {
  128. delayedMethods.add(getType(type).getMethod(methodName));
  129. }
  130. public static boolean isLastonly(Method method) {
  131. return get().lastonlyMethods.contains(method);
  132. }
  133. public void setLastonly(Class<?> clazz, String methodName) {
  134. lastonlyMethods.add(getType(clazz).getMethod(methodName));
  135. }
  136. public static Collection<Property> getProperties(Type type)
  137. throws NoDataException {
  138. Collection<Property> properties = get().properties.get(type);
  139. if (properties == null) {
  140. throw new NoDataException("No property list for "
  141. + type.getSignature());
  142. }
  143. return properties;
  144. }
  145. public void setProperties(Class<?> clazz, String[] propertyNames) {
  146. Set<Property> properties = new HashSet<Property>();
  147. Type type = getType(clazz);
  148. for (String name : propertyNames) {
  149. properties.add(new Property(type, name));
  150. }
  151. this.properties.put(type, Collections.unmodifiableSet(properties));
  152. }
  153. public static Type getType(Property property) throws NoDataException {
  154. Type type = get().propertyTypes.get(property);
  155. if (type == null) {
  156. throw new NoDataException("No return type for "
  157. + property.getSignature());
  158. }
  159. return type;
  160. }
  161. public void setPropertyType(Class<?> clazz, String propertName, Type type) {
  162. propertyTypes.put(new Property(getType(clazz), propertName), type);
  163. }
  164. public static Invoker getSetter(Property property) throws NoDataException {
  165. Invoker setter = get().setters.get(property);
  166. if (setter == null) {
  167. throw new NoDataException("No setter for "
  168. + property.getSignature());
  169. }
  170. return setter;
  171. }
  172. public void setSetter(Class<?> clazz, String propertyName, Invoker setter) {
  173. setters.put(new Property(getType(clazz), propertyName), setter);
  174. }
  175. public void setSerializerFactory(Class<?> clazz, Invoker factory) {
  176. serializerFactories.put(getType(clazz), factory);
  177. }
  178. public static JSONSerializer<?> findSerializer(Type type) {
  179. Invoker factoryCreator = get().serializerFactories.get(type);
  180. if (factoryCreator == null) {
  181. return null;
  182. }
  183. return (JSONSerializer<?>) factoryCreator.invoke(null);
  184. }
  185. public static boolean hasProperties(Type type) {
  186. return get().properties.containsKey(type);
  187. }
  188. }