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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright 2000-2013 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.client.metadata;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import com.google.gwt.core.client.JavaScriptObject;
  20. import com.google.gwt.core.client.JsArrayString;
  21. import com.vaadin.client.FastStringMap;
  22. import com.vaadin.client.FastStringSet;
  23. import com.vaadin.client.JsArrayObject;
  24. import com.vaadin.client.communication.JSONSerializer;
  25. public class TypeDataStore {
  26. private static final String CONSTRUCTOR_NAME = "!new";
  27. private final FastStringMap<Class<?>> identifiers = FastStringMap.create();
  28. private final FastStringMap<Invoker> serializerFactories = FastStringMap
  29. .create();
  30. private final FastStringMap<ProxyHandler> proxyHandlers = FastStringMap
  31. .create();
  32. private final FastStringMap<JsArrayObject<Property>> properties = FastStringMap
  33. .create();
  34. private final FastStringMap<JsArrayString> delegateToWidgetProperties = FastStringMap
  35. .create();
  36. private final FastStringSet delayedMethods = FastStringSet.create();
  37. private final FastStringSet lastOnlyMethods = FastStringSet.create();
  38. private final FastStringMap<Type> returnTypes = FastStringMap.create();
  39. private final FastStringMap<Invoker> invokers = FastStringMap.create();
  40. private final FastStringMap<Type[]> paramTypes = FastStringMap.create();
  41. private final FastStringMap<Type> propertyTypes = FastStringMap.create();
  42. private final FastStringMap<Invoker> setters = FastStringMap.create();
  43. private final FastStringMap<Invoker> getters = FastStringMap.create();
  44. private final FastStringMap<String> delegateToWidget = FastStringMap
  45. .create();
  46. public static TypeDataStore get() {
  47. return ConnectorBundleLoader.get().getTypeDataStore();
  48. }
  49. public void setClass(String identifier, Class<?> type) {
  50. identifiers.put(identifier, type);
  51. }
  52. public static Class<?> getClass(String identifier) throws NoDataException {
  53. Class<?> class1 = get().identifiers.get(identifier);
  54. if (class1 == null) {
  55. throw new NoDataException("There is not class for identifier "
  56. + identifier);
  57. }
  58. return class1;
  59. }
  60. public static Type getType(Class<?> clazz) {
  61. return new Type(clazz);
  62. }
  63. public static Type getReturnType(Method method) throws NoDataException {
  64. Type type = get().returnTypes.get(method.getSignature());
  65. if (type == null) {
  66. throw new NoDataException("There is no return type for "
  67. + method.getSignature());
  68. }
  69. return type;
  70. }
  71. public static Invoker getInvoker(Method method) throws NoDataException {
  72. Invoker invoker = get().invokers.get(method.getSignature());
  73. if (invoker == null) {
  74. throw new NoDataException("There is no invoker for "
  75. + method.getSignature());
  76. }
  77. return invoker;
  78. }
  79. public static Invoker getConstructor(Type type) throws NoDataException {
  80. Invoker invoker = get().invokers.get(new Method(type, CONSTRUCTOR_NAME)
  81. .getSignature());
  82. if (invoker == null) {
  83. throw new NoDataException("There is no constructor for "
  84. + type.getSignature());
  85. }
  86. return invoker;
  87. }
  88. public static Invoker getGetter(Property property) throws NoDataException {
  89. Invoker getter = get().getters.get(property.getSignature());
  90. if (getter == null) {
  91. throw new NoDataException("There is no getter for "
  92. + property.getSignature());
  93. }
  94. return getter;
  95. }
  96. public void setGetter(Class<?> clazz, String propertyName, Invoker invoker) {
  97. getters.put(new Property(getType(clazz), propertyName).getSignature(),
  98. invoker);
  99. }
  100. public static String getDelegateToWidget(Property property) {
  101. return get().delegateToWidget.get(property.getSignature());
  102. }
  103. public static JsArrayString getDelegateToWidgetProperites(Type type) {
  104. return get().delegateToWidgetProperties.get(type.getSignature());
  105. }
  106. public void setDelegateToWidget(Class<?> clazz, String propertyName,
  107. String delegateValue) {
  108. Type type = getType(clazz);
  109. delegateToWidget.put(new Property(type, propertyName).getSignature(),
  110. delegateValue);
  111. JsArrayString typeProperties = delegateToWidgetProperties.get(type
  112. .getSignature());
  113. if (typeProperties == null) {
  114. typeProperties = JavaScriptObject.createArray().cast();
  115. delegateToWidgetProperties.put(type.getSignature(), typeProperties);
  116. }
  117. typeProperties.push(propertyName);
  118. }
  119. public void setReturnType(Class<?> type, String methodName, Type returnType) {
  120. returnTypes.put(new Method(getType(type), methodName).getSignature(),
  121. returnType);
  122. }
  123. public void setConstructor(Class<?> type, Invoker constructor) {
  124. setInvoker(type, CONSTRUCTOR_NAME, constructor);
  125. }
  126. public void setInvoker(Class<?> type, String methodName, Invoker invoker) {
  127. invokers.put(new Method(getType(type), methodName).getSignature(),
  128. invoker);
  129. }
  130. public static Type[] getParamTypes(Method method) throws NoDataException {
  131. Type[] types = get().paramTypes.get(method.getSignature());
  132. if (types == null) {
  133. throw new NoDataException("There are no parameter type data for "
  134. + method.getSignature());
  135. }
  136. return types;
  137. }
  138. public void setParamTypes(Class<?> type, String methodName,
  139. Type[] paramTypes) {
  140. this.paramTypes.put(
  141. new Method(getType(type), methodName).getSignature(),
  142. paramTypes);
  143. }
  144. public static boolean hasIdentifier(String identifier) {
  145. return get().identifiers.containsKey(identifier);
  146. }
  147. public static ProxyHandler getProxyHandler(Type type)
  148. throws NoDataException {
  149. ProxyHandler proxyHandler = get().proxyHandlers
  150. .get(type.getSignature());
  151. if (proxyHandler == null) {
  152. throw new NoDataException("No proxy handler for "
  153. + type.getSignature());
  154. }
  155. return proxyHandler;
  156. }
  157. public void setProxyHandler(Class<?> type, ProxyHandler proxyHandler) {
  158. proxyHandlers.put(getType(type).getSignature(), proxyHandler);
  159. }
  160. public static boolean isDelayed(Method method) {
  161. return get().delayedMethods.contains(method.getSignature());
  162. }
  163. public void setDelayed(Class<?> type, String methodName) {
  164. delayedMethods.add(getType(type).getMethod(methodName).getSignature());
  165. }
  166. public static boolean isLastOnly(Method method) {
  167. return get().lastOnlyMethods.contains(method.getSignature());
  168. }
  169. public void setLastOnly(Class<?> clazz, String methodName) {
  170. lastOnlyMethods
  171. .add(getType(clazz).getMethod(methodName).getSignature());
  172. }
  173. /**
  174. * @param type
  175. * @return
  176. * @throws NoDataException
  177. *
  178. * @deprecated As of 7.0.1, use {@link #getPropertiesAsArray(Type)} instead
  179. * for improved performance
  180. */
  181. @Deprecated
  182. public static Collection<Property> getProperties(Type type)
  183. throws NoDataException {
  184. JsArrayObject<Property> propertiesArray = getPropertiesAsArray(type);
  185. int size = propertiesArray.size();
  186. ArrayList<Property> properties = new ArrayList<Property>(size);
  187. for (int i = 0; i < size; i++) {
  188. properties.add(propertiesArray.get(i));
  189. }
  190. return properties;
  191. }
  192. public static JsArrayObject<Property> getPropertiesAsArray(Type type)
  193. throws NoDataException {
  194. JsArrayObject<Property> properties = get().properties.get(type
  195. .getSignature());
  196. if (properties == null) {
  197. throw new NoDataException("No property list for "
  198. + type.getSignature());
  199. }
  200. return properties;
  201. }
  202. public void setProperties(Class<?> clazz, String[] propertyNames) {
  203. JsArrayObject<Property> properties = JavaScriptObject.createArray()
  204. .cast();
  205. Type type = getType(clazz);
  206. for (String name : propertyNames) {
  207. properties.add(new Property(type, name));
  208. }
  209. this.properties.put(type.getSignature(), properties);
  210. }
  211. public static Type getType(Property property) throws NoDataException {
  212. Type type = get().propertyTypes.get(property.getSignature());
  213. if (type == null) {
  214. throw new NoDataException("No return type for "
  215. + property.getSignature());
  216. }
  217. return type;
  218. }
  219. public void setPropertyType(Class<?> clazz, String propertName, Type type) {
  220. propertyTypes.put(
  221. new Property(getType(clazz), propertName).getSignature(), type);
  222. }
  223. public static Invoker getSetter(Property property) throws NoDataException {
  224. Invoker setter = get().setters.get(property.getSignature());
  225. if (setter == null) {
  226. throw new NoDataException("No setter for "
  227. + property.getSignature());
  228. }
  229. return setter;
  230. }
  231. public void setSetter(Class<?> clazz, String propertyName, Invoker setter) {
  232. setters.put(new Property(getType(clazz), propertyName).getSignature(),
  233. setter);
  234. }
  235. public void setSerializerFactory(Class<?> clazz, Invoker factory) {
  236. serializerFactories.put(getType(clazz).getSignature(), factory);
  237. }
  238. public static JSONSerializer<?> findSerializer(Type type) {
  239. Invoker factoryCreator = get().serializerFactories.get(type
  240. .getSignature());
  241. if (factoryCreator == null) {
  242. return null;
  243. }
  244. return (JSONSerializer<?>) factoryCreator.invoke(null);
  245. }
  246. public static boolean hasProperties(Type type) {
  247. return get().properties.containsKey(type.getSignature());
  248. }
  249. }