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

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