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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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.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.annotations.OnStateChange;
  25. import com.vaadin.client.communication.JSONSerializer;
  26. import com.vaadin.shared.annotations.NoLayout;
  27. public class TypeDataStore {
  28. public static enum MethodAttribute {
  29. DELAYED, LAST_ONLY, NO_LAYOUT;
  30. }
  31. private static final String CONSTRUCTOR_NAME = "!new";
  32. private final FastStringMap<Class<?>> identifiers = FastStringMap.create();
  33. private final FastStringMap<Invoker> serializerFactories = FastStringMap
  34. .create();
  35. private final FastStringMap<ProxyHandler> proxyHandlers = FastStringMap
  36. .create();
  37. private final FastStringMap<JsArrayString> delegateToWidgetProperties = FastStringMap
  38. .create();
  39. private final FastStringMap<Type> presentationTypes = FastStringMap
  40. .create();
  41. /**
  42. * Maps connector class -> state property name -> hander method data
  43. */
  44. private final FastStringMap<FastStringMap<JsArrayObject<OnStateChangeMethod>>> onStateChangeMethods = FastStringMap
  45. .create();
  46. private final FastStringMap<FastStringSet> methodAttributes = FastStringMap
  47. .create();
  48. private final FastStringMap<Type> returnTypes = FastStringMap.create();
  49. private final FastStringMap<Invoker> invokers = FastStringMap.create();
  50. private final FastStringMap<Type[]> paramTypes = FastStringMap.create();
  51. private final FastStringMap<String> delegateToWidget = FastStringMap
  52. .create();
  53. private final JavaScriptObject jsTypeData = JavaScriptObject.createObject();
  54. public static TypeDataStore get() {
  55. return ConnectorBundleLoader.get().getTypeDataStore();
  56. }
  57. public void setClass(String identifier, Class<?> type) {
  58. identifiers.put(identifier, type);
  59. }
  60. public static Class<?> getClass(String identifier) throws NoDataException {
  61. Class<?> class1 = get().identifiers.get(identifier);
  62. if (class1 == null) {
  63. throw new NoDataException("There is not class for identifier "
  64. + identifier);
  65. }
  66. return class1;
  67. }
  68. // this is a very inefficient implementation for getting all the identifiers
  69. // for a class
  70. public FastStringSet findIdentifiersFor(Class<?> type) {
  71. FastStringSet result = FastStringSet.create();
  72. JsArrayString keys = identifiers.getKeys();
  73. for (int i = 0; i < keys.length(); i++) {
  74. String key = keys.get(i);
  75. if (identifiers.get(key) == type) {
  76. result.add(key);
  77. }
  78. }
  79. return result;
  80. }
  81. public static Type getType(Class<?> clazz) {
  82. return new Type(clazz);
  83. }
  84. public static Type getReturnType(Method method) throws NoDataException {
  85. Type type = get().returnTypes.get(method.getLookupKey());
  86. if (type == null) {
  87. throw new NoDataException("There is no return type for "
  88. + method.getSignature());
  89. }
  90. return type;
  91. }
  92. public static Invoker getInvoker(Method method) throws NoDataException {
  93. Invoker invoker = get().invokers.get(method.getLookupKey());
  94. if (invoker == null) {
  95. throw new NoDataException("There is no invoker for "
  96. + method.getSignature());
  97. }
  98. return invoker;
  99. }
  100. public static Invoker getConstructor(Type type) throws NoDataException {
  101. Invoker invoker = get().invokers.get(new Method(type, CONSTRUCTOR_NAME)
  102. .getLookupKey());
  103. if (invoker == null) {
  104. throw new NoDataException("There is no constructor for "
  105. + type.getSignature());
  106. }
  107. return invoker;
  108. }
  109. public static Object getValue(Property property, Object target)
  110. throws NoDataException {
  111. return getJsPropertyValue(get().jsTypeData, property.getBeanType()
  112. .getBaseTypeName(), property.getName(), target);
  113. }
  114. public static String getDelegateToWidget(Property property) {
  115. return get().delegateToWidget.get(property.getLookupKey());
  116. }
  117. public static JsArrayString getDelegateToWidgetProperites(Type type) {
  118. return get().delegateToWidgetProperties.get(type.getBaseTypeName());
  119. }
  120. public static Type getPresentationType(Class<?> type) {
  121. return get().presentationTypes.get(getType(type).getBaseTypeName());
  122. }
  123. public void setDelegateToWidget(Class<?> clazz, String propertyName,
  124. String delegateValue) {
  125. Type type = getType(clazz);
  126. delegateToWidget.put(new Property(type, propertyName).getLookupKey(),
  127. delegateValue);
  128. JsArrayString typeProperties = delegateToWidgetProperties.get(type
  129. .getBaseTypeName());
  130. if (typeProperties == null) {
  131. typeProperties = JavaScriptObject.createArray().cast();
  132. delegateToWidgetProperties.put(type.getBaseTypeName(),
  133. typeProperties);
  134. }
  135. typeProperties.push(propertyName);
  136. }
  137. public void setPresentationType(Class<?> type, Class<?> presentationType) {
  138. presentationTypes.put(getType(type).getBaseTypeName(),
  139. getType(presentationType));
  140. }
  141. public void setReturnType(Class<?> type, String methodName, Type returnType) {
  142. returnTypes.put(new Method(getType(type), methodName).getLookupKey(),
  143. returnType);
  144. }
  145. public void setConstructor(Class<?> type, Invoker constructor) {
  146. setInvoker(type, CONSTRUCTOR_NAME, constructor);
  147. }
  148. public void setInvoker(Class<?> type, String methodName, Invoker invoker) {
  149. invokers.put(new Method(getType(type), methodName).getLookupKey(),
  150. invoker);
  151. }
  152. public static Type[] getParamTypes(Method method) throws NoDataException {
  153. Type[] types = get().paramTypes.get(method.getLookupKey());
  154. if (types == null) {
  155. throw new NoDataException("There are no parameter type data for "
  156. + method.getSignature());
  157. }
  158. return types;
  159. }
  160. public void setParamTypes(Class<?> type, String methodName,
  161. Type[] paramTypes) {
  162. this.paramTypes.put(
  163. new Method(getType(type), methodName).getLookupKey(),
  164. paramTypes);
  165. }
  166. public static boolean hasIdentifier(String identifier) {
  167. return get().identifiers.containsKey(identifier);
  168. }
  169. public static ProxyHandler getProxyHandler(Type type)
  170. throws NoDataException {
  171. ProxyHandler proxyHandler = get().proxyHandlers.get(type
  172. .getBaseTypeName());
  173. if (proxyHandler == null) {
  174. throw new NoDataException("No proxy handler for "
  175. + type.getSignature());
  176. }
  177. return proxyHandler;
  178. }
  179. public void setProxyHandler(Class<?> type, ProxyHandler proxyHandler) {
  180. proxyHandlers.put(getType(type).getBaseTypeName(), proxyHandler);
  181. }
  182. public static boolean isDelayed(Method method) {
  183. return hasMethodAttribute(method, MethodAttribute.DELAYED);
  184. }
  185. private static boolean hasMethodAttribute(Method method,
  186. MethodAttribute attribute) {
  187. FastStringSet attributes = get().methodAttributes.get(method
  188. .getLookupKey());
  189. return attributes != null && attributes.contains(attribute.name());
  190. }
  191. public void setMethodAttribute(Class<?> type, String methodName,
  192. MethodAttribute attribute) {
  193. String key = getType(type).getMethod(methodName).getLookupKey();
  194. FastStringSet attributes = methodAttributes.get(key);
  195. if (attributes == null) {
  196. attributes = FastStringSet.create();
  197. methodAttributes.put(key, attributes);
  198. }
  199. attributes.add(attribute.name());
  200. }
  201. public static boolean isLastOnly(Method method) {
  202. return hasMethodAttribute(method, MethodAttribute.LAST_ONLY);
  203. }
  204. /**
  205. * @param type
  206. * @return
  207. * @throws NoDataException
  208. *
  209. * @deprecated As of 7.0.1, use {@link #getPropertiesAsArray(Type)} instead
  210. * for improved performance
  211. */
  212. @Deprecated
  213. public static Collection<Property> getProperties(Type type)
  214. throws NoDataException {
  215. JsArrayObject<Property> propertiesArray = getPropertiesAsArray(type);
  216. int size = propertiesArray.size();
  217. ArrayList<Property> properties = new ArrayList<Property>(size);
  218. for (int i = 0; i < size; i++) {
  219. properties.add(propertiesArray.get(i));
  220. }
  221. return properties;
  222. }
  223. public static JsArrayObject<Property> getPropertiesAsArray(Type type)
  224. throws NoDataException {
  225. JsArrayString names = getJsPropertyNames(get().jsTypeData,
  226. type.getBaseTypeName());
  227. // Create Property instances for each property name
  228. JsArrayObject<Property> properties = JavaScriptObject.createArray()
  229. .cast();
  230. for (int i = 0; i < names.length(); i++) {
  231. properties.add(new Property(type, names.get(i)));
  232. }
  233. return properties;
  234. }
  235. public static Type getType(Property property) throws NoDataException {
  236. return getJsPropertyType(get().jsTypeData, property.getBeanType()
  237. .getBaseTypeName(), property.getName());
  238. }
  239. public void setPropertyType(Class<?> clazz, String propertyName, Type type) {
  240. setJsPropertyType(jsTypeData, clazz.getName(), propertyName, type);
  241. }
  242. public static void setValue(Property property, Object target, Object value) {
  243. setJsPropertyValue(get().jsTypeData, property.getBeanType()
  244. .getBaseTypeName(), property.getName(), target, value);
  245. }
  246. public void setSerializerFactory(Class<?> clazz, Invoker factory) {
  247. serializerFactories.put(getType(clazz).getBaseTypeName(), factory);
  248. }
  249. public static JSONSerializer<?> findSerializer(Type type) {
  250. Invoker factoryCreator = get().serializerFactories.get(type
  251. .getBaseTypeName());
  252. if (factoryCreator == null) {
  253. return null;
  254. }
  255. return (JSONSerializer<?>) factoryCreator.invoke(null);
  256. }
  257. public static boolean hasProperties(Type type) {
  258. return hasJsProperties(get().jsTypeData, type.getBaseTypeName());
  259. }
  260. public void setSuperClass(Class<?> baseClass, Class<?> superClass) {
  261. String superClassName = superClass == null ? null : superClass
  262. .getName();
  263. setSuperClass(jsTypeData, baseClass.getName(), superClassName);
  264. }
  265. public void setPropertyData(Class<?> type, String propertyName,
  266. JavaScriptObject propertyData) {
  267. setPropertyData(jsTypeData, type.getName(), propertyName, propertyData);
  268. }
  269. private static native void setPropertyData(JavaScriptObject typeData,
  270. String className, String propertyName, JavaScriptObject propertyData)
  271. /*-{
  272. typeData[className][propertyName] = propertyData;
  273. }-*/;
  274. /*
  275. * This method sets up prototypes chain for <code>baseClassName</code>.
  276. * Precondition is : <code>superClassName</code> had to be handled before
  277. * its child <code>baseClassName</code>.
  278. *
  279. * It makes all properties defined in the <code>superClassName</code>
  280. * available for <code>baseClassName</code> as well.
  281. */
  282. private static native void setSuperClass(JavaScriptObject typeData,
  283. String baseClassName, String superClassName)
  284. /*-{
  285. var parentType = typeData[superClassName];
  286. if (parentType !== undefined ){
  287. var ctor = function () {};
  288. ctor.prototype = parentType;
  289. typeData[baseClassName] = new ctor;
  290. }
  291. else {
  292. typeData[baseClassName] = {};
  293. }
  294. }-*/;
  295. private static native boolean hasGetter(JavaScriptObject typeData,
  296. String beanName, String propertyName)
  297. /*-{
  298. return typeData[beanName][propertyName].getter !== undefined;
  299. }-*/;
  300. private static native boolean hasSetter(JavaScriptObject typeData,
  301. String beanName, String propertyName)
  302. /*-{
  303. return typeData[beanName][propertyName].setter !== undefined;
  304. }-*/;
  305. private static native boolean hasNoLayout(JavaScriptObject typeData,
  306. String beanName, String propertyName)
  307. /*-{
  308. return typeData[beanName][propertyName].noLayout !== undefined;
  309. }-*/;
  310. private static native Object getJsPropertyValue(JavaScriptObject typeData,
  311. String beanName, String propertyName, Object beanInstance)
  312. /*-{
  313. return typeData[beanName][propertyName].getter(beanInstance);
  314. }-*/;
  315. private static native void setJsPropertyValue(JavaScriptObject typeData,
  316. String beanName, String propertyName, Object beanInstance,
  317. Object value)
  318. /*-{
  319. typeData[beanName][propertyName].setter(beanInstance, value);
  320. }-*/;
  321. private static native Type getJsPropertyType(JavaScriptObject typeData,
  322. String beanName, String propertyName)
  323. /*-{
  324. return typeData[beanName][propertyName].type;
  325. }-*/;
  326. private static native void setJsPropertyType(JavaScriptObject typeData,
  327. String beanName, String propertyName, Type type)
  328. /*-{
  329. typeData[beanName][propertyName].type = type;
  330. }-*/;
  331. private static native JsArrayString getJsPropertyNames(
  332. JavaScriptObject typeData, String beanName)
  333. /*-{
  334. var names = [];
  335. for(var name in typeData[beanName]) {
  336. names.push(name);
  337. }
  338. return names;
  339. }-*/;
  340. private static native boolean hasJsProperties(JavaScriptObject typeData,
  341. String beanName)
  342. /*-{
  343. return typeData[beanName] !== undefined ;
  344. }-*/;
  345. /**
  346. * Gets data for all methods annotated with {@link OnStateChange} in the
  347. * given connector type.
  348. *
  349. * @since 7.2
  350. * @param type
  351. * the connector type
  352. * @return a map of state property names to handler method data
  353. */
  354. public static FastStringMap<JsArrayObject<OnStateChangeMethod>> getOnStateChangeMethods(
  355. Class<?> type) {
  356. return get().onStateChangeMethods.get(getType(type).getSignature());
  357. }
  358. /**
  359. * Adds data about a method annotated with {@link OnStateChange} for the
  360. * given connector type.
  361. *
  362. * @since 7.2
  363. * @param clazz
  364. * the connector type
  365. * @param method
  366. * the state change method data
  367. */
  368. public void addOnStateChangeMethod(Class<?> clazz,
  369. OnStateChangeMethod method) {
  370. FastStringMap<JsArrayObject<OnStateChangeMethod>> handlers = getOnStateChangeMethods(clazz);
  371. if (handlers == null) {
  372. handlers = FastStringMap.create();
  373. onStateChangeMethods.put(getType(clazz).getSignature(), handlers);
  374. }
  375. for (String property : method.getProperties()) {
  376. JsArrayObject<OnStateChangeMethod> propertyHandlers = handlers
  377. .get(property);
  378. if (propertyHandlers == null) {
  379. propertyHandlers = JsArrayObject.createArray().cast();
  380. handlers.put(property, propertyHandlers);
  381. }
  382. propertyHandlers.add(method);
  383. }
  384. }
  385. /**
  386. * Checks whether the provided method is annotated with {@link NoLayout}.
  387. *
  388. * @param method
  389. * the rpc method to check
  390. *
  391. * @since 7.4
  392. *
  393. * @return <code>true</code> if the method has a NoLayout annotation;
  394. * otherwise <code>false</code>
  395. */
  396. public static boolean isNoLayoutRpcMethod(Method method) {
  397. return hasMethodAttribute(method, MethodAttribute.NO_LAYOUT);
  398. }
  399. /**
  400. * Checks whether the provided property is annotated with {@link NoLayout}.
  401. *
  402. * @param property
  403. * the property to check
  404. *
  405. * @since 7.4
  406. *
  407. * @return <code>true</code> if the property has a NoLayout annotation;
  408. * otherwise <code>false</code>
  409. */
  410. public static boolean isNoLayoutProperty(Property property) {
  411. return hasNoLayout(get().jsTypeData, property.getBeanType()
  412. .getSignature(), property.getName());
  413. }
  414. }