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

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