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

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