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.

ConnectorBundle.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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.server.widgetsetutils.metadata;
  17. import java.io.Serializable;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import com.google.gwt.core.ext.TreeLogger;
  28. import com.google.gwt.core.ext.TreeLogger.Type;
  29. import com.google.gwt.core.ext.UnableToCompleteException;
  30. import com.google.gwt.core.ext.typeinfo.JArrayType;
  31. import com.google.gwt.core.ext.typeinfo.JClassType;
  32. import com.google.gwt.core.ext.typeinfo.JEnumType;
  33. import com.google.gwt.core.ext.typeinfo.JMethod;
  34. import com.google.gwt.core.ext.typeinfo.JParameterizedType;
  35. import com.google.gwt.core.ext.typeinfo.JType;
  36. import com.google.gwt.core.ext.typeinfo.NotFoundException;
  37. import com.google.gwt.core.ext.typeinfo.TypeOracle;
  38. import com.google.gwt.json.client.JSONValue;
  39. import com.vaadin.client.ApplicationConnection;
  40. import com.vaadin.client.ComponentConnector;
  41. import com.vaadin.client.ServerConnector;
  42. import com.vaadin.client.communication.JSONSerializer;
  43. import com.vaadin.client.ui.UnknownComponentConnector;
  44. import com.vaadin.shared.communication.ClientRpc;
  45. import com.vaadin.shared.communication.ServerRpc;
  46. import com.vaadin.shared.ui.Connect;
  47. public class ConnectorBundle {
  48. private static final String FAIL_IF_NOT_SERIALIZABLE = "vFailIfNotSerializable";
  49. private final String name;
  50. private final ConnectorBundle previousBundle;
  51. private final Collection<TypeVisitor> visitors;
  52. private final Map<JType, JClassType> customSerializers;
  53. private final Set<JType> hasSerializeSupport = new HashSet<JType>();
  54. private final Set<JType> needsSerializeSupport = new HashSet<JType>();
  55. private final Map<JType, GeneratedSerializer> serializers = new HashMap<JType, GeneratedSerializer>();
  56. private final Set<JClassType> needsSuperClass = new HashSet<JClassType>();
  57. private final Set<JClassType> needsGwtConstructor = new HashSet<JClassType>();
  58. private final Set<JClassType> visitedTypes = new HashSet<JClassType>();
  59. private final Set<JClassType> needsProxySupport = new HashSet<JClassType>();
  60. private final Map<JClassType, Set<String>> identifiers = new HashMap<JClassType, Set<String>>();
  61. private final Map<JClassType, Set<JMethod>> needsReturnType = new HashMap<JClassType, Set<JMethod>>();
  62. private final Map<JClassType, Set<JMethod>> needsInvoker = new HashMap<JClassType, Set<JMethod>>();
  63. private final Map<JClassType, Set<JMethod>> needsParamTypes = new HashMap<JClassType, Set<JMethod>>();
  64. private final Map<JClassType, Set<JMethod>> needsDelayedInfo = new HashMap<JClassType, Set<JMethod>>();
  65. private final Map<JClassType, Set<JMethod>> needsOnStateChange = new HashMap<JClassType, Set<JMethod>>();
  66. private final Set<Property> needsProperty = new HashSet<Property>();
  67. private final Set<Property> needsDelegateToWidget = new HashSet<Property>();
  68. private ConnectorBundle(String name, ConnectorBundle previousBundle,
  69. Collection<TypeVisitor> visitors,
  70. Map<JType, JClassType> customSerializers) {
  71. this.name = name;
  72. this.previousBundle = previousBundle;
  73. this.visitors = visitors;
  74. this.customSerializers = customSerializers;
  75. }
  76. public ConnectorBundle(String name, ConnectorBundle previousBundle) {
  77. this(name, previousBundle, previousBundle.visitors,
  78. previousBundle.customSerializers);
  79. }
  80. public ConnectorBundle(String name, Collection<TypeVisitor> visitors,
  81. TypeOracle oracle) throws NotFoundException {
  82. this(name, null, visitors, findCustomSerializers(oracle));
  83. }
  84. private static Map<JType, JClassType> findCustomSerializers(
  85. TypeOracle oracle) throws NotFoundException {
  86. Map<JType, JClassType> serializers = new HashMap<JType, JClassType>();
  87. JClassType serializerInterface = oracle.findType(JSONSerializer.class
  88. .getName());
  89. JType[] deserializeParamTypes = new JType[] {
  90. oracle.findType(com.vaadin.client.metadata.Type.class.getName()),
  91. oracle.findType(JSONValue.class.getName()),
  92. oracle.findType(ApplicationConnection.class.getName()) };
  93. String deserializeMethodName = "deserialize";
  94. // Just test that the method exists
  95. serializerInterface.getMethod(deserializeMethodName,
  96. deserializeParamTypes);
  97. for (JClassType serializer : serializerInterface.getSubtypes()) {
  98. JMethod deserializeMethod = serializer.findMethod(
  99. deserializeMethodName, deserializeParamTypes);
  100. if (deserializeMethod == null) {
  101. continue;
  102. }
  103. JType returnType = deserializeMethod.getReturnType();
  104. serializers.put(returnType, serializer);
  105. }
  106. return serializers;
  107. }
  108. public void setNeedsGwtConstructor(JClassType type) {
  109. if (!needsGwtConstructor(type)) {
  110. needsGwtConstructor.add(type);
  111. }
  112. }
  113. private boolean needsGwtConstructor(JClassType type) {
  114. if (needsGwtConstructor.contains(type)) {
  115. return true;
  116. } else {
  117. return previousBundle != null
  118. && previousBundle.needsGwtConstructor(type);
  119. }
  120. }
  121. public void setIdentifier(JClassType type, String identifier) {
  122. if (!hasIdentifier(type, identifier)) {
  123. addMapping(identifiers, type, identifier);
  124. }
  125. }
  126. private boolean hasIdentifier(JClassType type, String identifier) {
  127. if (hasMapping(identifiers, type, identifier)) {
  128. return true;
  129. } else {
  130. return previousBundle != null
  131. && previousBundle.hasIdentifier(type, identifier);
  132. }
  133. }
  134. public ConnectorBundle getPreviousBundle() {
  135. return previousBundle;
  136. }
  137. public String getName() {
  138. return name;
  139. }
  140. public Map<JClassType, Set<String>> getIdentifiers() {
  141. return Collections.unmodifiableMap(identifiers);
  142. }
  143. public Set<JClassType> getGwtConstructors() {
  144. return Collections.unmodifiableSet(needsGwtConstructor);
  145. }
  146. public void processTypes(TreeLogger logger, Collection<JClassType> types)
  147. throws UnableToCompleteException {
  148. for (JClassType type : types) {
  149. processType(logger, type);
  150. }
  151. }
  152. public void processType(TreeLogger logger, JClassType type)
  153. throws UnableToCompleteException {
  154. if (!isTypeVisited(type)) {
  155. for (TypeVisitor typeVisitor : visitors) {
  156. invokeVisitor(logger, type, typeVisitor);
  157. }
  158. visitedTypes.add(type);
  159. purgeSerializeSupportQueue(logger);
  160. }
  161. }
  162. private boolean isTypeVisited(JClassType type) {
  163. if (visitedTypes.contains(type)) {
  164. return true;
  165. } else {
  166. return previousBundle != null && previousBundle.isTypeVisited(type);
  167. }
  168. }
  169. private void purgeSerializeSupportQueue(TreeLogger logger)
  170. throws UnableToCompleteException {
  171. while (!needsSerializeSupport.isEmpty()) {
  172. Iterator<JType> iterator = needsSerializeSupport.iterator();
  173. JType type = iterator.next();
  174. iterator.remove();
  175. if (hasSerializeSupport(type)) {
  176. continue;
  177. }
  178. addSerializeSupport(logger, type);
  179. }
  180. }
  181. private void addSerializeSupport(TreeLogger logger, JType type)
  182. throws UnableToCompleteException {
  183. hasSerializeSupport.add(type);
  184. JParameterizedType parametrized = type.isParameterized();
  185. if (parametrized != null) {
  186. for (JClassType parameterType : parametrized.getTypeArgs()) {
  187. setNeedsSerialize(parameterType);
  188. }
  189. }
  190. if (serializationHandledByFramework(type)) {
  191. return;
  192. }
  193. JClassType customSerializer = customSerializers.get(type);
  194. JClassType typeAsClass = type.isClass();
  195. JEnumType enumType = type.isEnum();
  196. JArrayType arrayType = type.isArray();
  197. if (customSerializer != null) {
  198. logger.log(Type.INFO, "Will serialize " + type + " using "
  199. + customSerializer.getName());
  200. setSerializer(type, new CustomSerializer(customSerializer));
  201. } else if (arrayType != null) {
  202. logger.log(Type.INFO, "Will serialize " + type + " as an array");
  203. setSerializer(type, new ArraySerializer(arrayType));
  204. setNeedsSerialize(arrayType.getComponentType());
  205. } else if (enumType != null) {
  206. logger.log(Type.INFO, "Will serialize " + type + " as an enum");
  207. setSerializer(type, new EnumSerializer(enumType));
  208. } else if (typeAsClass != null) {
  209. // Bean
  210. checkSerializable(logger, typeAsClass);
  211. logger.log(Type.INFO, "Will serialize " + type + " as a bean");
  212. JClassType needsSuperClass = typeAsClass;
  213. while (needsSuperClass != null) {
  214. if (needsSuperClass.isPublic()) {
  215. setNeedsSuperclass(needsSuperClass);
  216. }
  217. needsSuperClass = needsSuperClass.getSuperclass();
  218. }
  219. for (Property property : getProperties(typeAsClass)) {
  220. setNeedsGwtConstructor(property.getBeanType());
  221. setNeedsProperty(property);
  222. JType propertyType = property.getPropertyType();
  223. setNeedsSerialize(propertyType);
  224. }
  225. }
  226. }
  227. private void checkSerializable(TreeLogger logger, JClassType type)
  228. throws UnableToCompleteException {
  229. JClassType javaSerializable = type.getOracle().findType(
  230. Serializable.class.getName());
  231. boolean serializable = type.isAssignableTo(javaSerializable);
  232. if (!serializable) {
  233. boolean abortCompile = "true".equals(System
  234. .getProperty(FAIL_IF_NOT_SERIALIZABLE));
  235. logger.log(
  236. abortCompile ? Type.ERROR : Type.WARN,
  237. type
  238. + " is used in RPC or shared state but does not implement "
  239. + Serializable.class.getName()
  240. + ". Communication will work but the Application on server side cannot be serialized if it refers to objects of this type. "
  241. + "If the system property "
  242. + FAIL_IF_NOT_SERIALIZABLE
  243. + " is set to \"true\", this causes the compilation to fail instead of just emitting a warning.");
  244. if (abortCompile) {
  245. throw new UnableToCompleteException();
  246. }
  247. }
  248. }
  249. private void setSerializer(JType type, GeneratedSerializer serializer) {
  250. if (!hasSerializer(type)) {
  251. serializers.put(type, serializer);
  252. }
  253. }
  254. private boolean hasSerializer(JType type) {
  255. if (serializers.containsKey(type)) {
  256. return true;
  257. } else {
  258. return previousBundle != null && previousBundle.hasSerializer(type);
  259. }
  260. }
  261. public Map<JType, GeneratedSerializer> getSerializers() {
  262. return Collections.unmodifiableMap(serializers);
  263. }
  264. private void setNeedsSuperclass(JClassType typeAsClass) {
  265. if (!isNeedsSuperClass(typeAsClass)) {
  266. needsSuperClass.add(typeAsClass);
  267. }
  268. }
  269. private boolean isNeedsSuperClass(JClassType typeAsClass) {
  270. if (needsSuperClass.contains(typeAsClass)) {
  271. return true;
  272. } else {
  273. return previousBundle != null
  274. && previousBundle.isNeedsSuperClass(typeAsClass);
  275. }
  276. }
  277. public Set<JClassType> getNeedsSuperclass() {
  278. return Collections.unmodifiableSet(needsSuperClass);
  279. }
  280. private void setNeedsProperty(Property property) {
  281. if (!isNeedsProperty(property)) {
  282. needsProperty.add(property);
  283. }
  284. }
  285. private boolean isNeedsProperty(Property property) {
  286. if (needsProperty.contains(property)) {
  287. return true;
  288. } else {
  289. return previousBundle != null
  290. && previousBundle.isNeedsProperty(property);
  291. }
  292. }
  293. public Set<Property> getNeedsProperty() {
  294. return Collections.unmodifiableSet(needsProperty);
  295. }
  296. public Collection<Property> getProperties(JClassType type) {
  297. HashSet<Property> properties = new HashSet<Property>();
  298. properties.addAll(MethodProperty.findProperties(type));
  299. properties.addAll(FieldProperty.findProperties(type));
  300. return properties;
  301. }
  302. private void invokeVisitor(TreeLogger logger, JClassType type,
  303. TypeVisitor typeVisitor) throws UnableToCompleteException {
  304. TreeLogger subLogger = logger.branch(Type.TRACE,
  305. "Visiting " + type.getName() + " with "
  306. + typeVisitor.getClass().getSimpleName());
  307. if (isConnectedConnector(type)) {
  308. typeVisitor.visitConnector(subLogger, type, this);
  309. }
  310. if (isClientRpc(type)) {
  311. typeVisitor.visitClientRpc(subLogger, type, this);
  312. }
  313. if (isServerRpc(type)) {
  314. typeVisitor.visitServerRpc(subLogger, type, this);
  315. }
  316. }
  317. public void processSubTypes(TreeLogger logger, JClassType type)
  318. throws UnableToCompleteException {
  319. processTypes(logger, Arrays.asList(type.getSubtypes()));
  320. }
  321. public void setNeedsReturnType(JClassType type, JMethod method) {
  322. if (!isNeedsReturnType(type, method)) {
  323. addMapping(needsReturnType, type, method);
  324. }
  325. }
  326. private boolean isNeedsReturnType(JClassType type, JMethod method) {
  327. if (hasMapping(needsReturnType, type, method)) {
  328. return true;
  329. } else {
  330. return previousBundle != null
  331. && previousBundle.isNeedsReturnType(type, method);
  332. }
  333. }
  334. public Map<JClassType, Set<JMethod>> getMethodReturnTypes() {
  335. return Collections.unmodifiableMap(needsReturnType);
  336. }
  337. private static boolean isClientRpc(JClassType type) {
  338. return isInterfaceType(type, ClientRpc.class);
  339. }
  340. private static boolean isServerRpc(JClassType type) {
  341. return isInterfaceType(type, ServerRpc.class);
  342. }
  343. public static boolean isConnectedConnector(JClassType type) {
  344. return isConnected(type) && isType(type, ServerConnector.class);
  345. }
  346. private static boolean isConnected(JClassType type) {
  347. return type.isAnnotationPresent(Connect.class)
  348. || type.getQualifiedSourceName().equals(
  349. UnknownComponentConnector.class.getCanonicalName());
  350. }
  351. public static boolean isConnectedComponentConnector(JClassType type) {
  352. return isConnected(type) && isType(type, ComponentConnector.class);
  353. }
  354. private static boolean isInterfaceType(JClassType type, Class<?> class1) {
  355. return type.isInterface() != null && isType(type, class1);
  356. }
  357. private static boolean isType(JClassType type, Class<?> class1) {
  358. try {
  359. return type.getOracle().getType(class1.getName())
  360. .isAssignableFrom(type);
  361. } catch (NotFoundException e) {
  362. throw new RuntimeException("Could not find " + class1.getName(), e);
  363. }
  364. }
  365. public void setNeedsInvoker(JClassType type, JMethod method) {
  366. if (!isNeedsInvoker(type, method)) {
  367. addMapping(needsInvoker, type, method);
  368. }
  369. }
  370. private <K, V> void addMapping(Map<K, Set<V>> map, K key, V value) {
  371. Set<V> set = map.get(key);
  372. if (set == null) {
  373. set = new HashSet<V>();
  374. map.put(key, set);
  375. }
  376. set.add(value);
  377. }
  378. private <K, V> boolean hasMapping(Map<K, Set<V>> map, K key, V value) {
  379. return map.containsKey(key) && map.get(key).contains(value);
  380. }
  381. private boolean isNeedsInvoker(JClassType type, JMethod method) {
  382. if (hasMapping(needsInvoker, type, method)) {
  383. return true;
  384. } else {
  385. return previousBundle != null
  386. && previousBundle.isNeedsInvoker(type, method);
  387. }
  388. }
  389. public Map<JClassType, Set<JMethod>> getNeedsInvoker() {
  390. return Collections.unmodifiableMap(needsInvoker);
  391. }
  392. public void setNeedsParamTypes(JClassType type, JMethod method) {
  393. if (!isNeedsParamTypes(type, method)) {
  394. addMapping(needsParamTypes, type, method);
  395. }
  396. }
  397. private boolean isNeedsParamTypes(JClassType type, JMethod method) {
  398. if (hasMapping(needsParamTypes, type, method)) {
  399. return true;
  400. } else {
  401. return previousBundle != null
  402. && previousBundle.isNeedsParamTypes(type, method);
  403. }
  404. }
  405. public Map<JClassType, Set<JMethod>> getNeedsParamTypes() {
  406. return Collections.unmodifiableMap(needsParamTypes);
  407. }
  408. public void setNeedsProxySupport(JClassType type) {
  409. if (!isNeedsProxySupport(type)) {
  410. needsProxySupport.add(type);
  411. }
  412. }
  413. private boolean isNeedsProxySupport(JClassType type) {
  414. if (needsProxySupport.contains(type)) {
  415. return true;
  416. } else {
  417. return previousBundle != null
  418. && previousBundle.isNeedsProxySupport(type);
  419. }
  420. }
  421. public Set<JClassType> getNeedsProxySupport() {
  422. return Collections.unmodifiableSet(needsProxySupport);
  423. }
  424. public void setNeedsDelayedInfo(JClassType type, JMethod method) {
  425. if (!isNeedsDelayedInfo(type, method)) {
  426. addMapping(needsDelayedInfo, type, method);
  427. }
  428. }
  429. private boolean isNeedsDelayedInfo(JClassType type, JMethod method) {
  430. if (hasMapping(needsDelayedInfo, type, method)) {
  431. return true;
  432. } else {
  433. return previousBundle != null
  434. && previousBundle.isNeedsDelayedInfo(type, method);
  435. }
  436. }
  437. public Map<JClassType, Set<JMethod>> getNeedsDelayedInfo() {
  438. return Collections.unmodifiableMap(needsDelayedInfo);
  439. }
  440. public void setNeedsSerialize(JType type) {
  441. if (!hasSerializeSupport(type)) {
  442. needsSerializeSupport.add(type);
  443. }
  444. }
  445. private static Set<Class<?>> frameworkHandledTypes = new HashSet<Class<?>>();
  446. {
  447. frameworkHandledTypes.add(String.class);
  448. frameworkHandledTypes.add(Boolean.class);
  449. frameworkHandledTypes.add(Integer.class);
  450. frameworkHandledTypes.add(Float.class);
  451. frameworkHandledTypes.add(Double.class);
  452. frameworkHandledTypes.add(Long.class);
  453. frameworkHandledTypes.add(Enum.class);
  454. frameworkHandledTypes.add(String[].class);
  455. frameworkHandledTypes.add(Object[].class);
  456. frameworkHandledTypes.add(Map.class);
  457. frameworkHandledTypes.add(List.class);
  458. frameworkHandledTypes.add(Set.class);
  459. frameworkHandledTypes.add(Byte.class);
  460. frameworkHandledTypes.add(Character.class);
  461. }
  462. private boolean serializationHandledByFramework(JType setterType) {
  463. // Some types are handled by the framework at the moment. See #8449
  464. // This method should be removed at some point.
  465. if (setterType.isPrimitive() != null) {
  466. return true;
  467. }
  468. String qualifiedName = setterType.getQualifiedSourceName();
  469. for (Class<?> cls : frameworkHandledTypes) {
  470. if (qualifiedName.equals(cls.getName())) {
  471. return true;
  472. }
  473. }
  474. return false;
  475. }
  476. private boolean hasSerializeSupport(JType type) {
  477. if (hasSerializeSupport.contains(type)) {
  478. return true;
  479. } else {
  480. return previousBundle != null
  481. && previousBundle.hasSerializeSupport(type);
  482. }
  483. }
  484. public void setNeedsDelegateToWidget(Property property) {
  485. if (!isNeedsDelegateToWidget(property)) {
  486. needsDelegateToWidget.add(property);
  487. }
  488. }
  489. private boolean isNeedsDelegateToWidget(Property property) {
  490. if (needsDelegateToWidget.contains(property)) {
  491. return true;
  492. } else {
  493. return previousBundle != null
  494. && previousBundle.isNeedsDelegateToWidget(property);
  495. }
  496. }
  497. public Set<Property> getNeedsDelegateToWidget() {
  498. return Collections.unmodifiableSet(needsDelegateToWidget);
  499. }
  500. public void setNeedsOnStateChangeHandler(JClassType type, JMethod method) {
  501. if (!isNeedsOnStateChangeHandler(type, method)) {
  502. addMapping(needsOnStateChange, type, method);
  503. }
  504. }
  505. private boolean isNeedsOnStateChangeHandler(JClassType type, JMethod method) {
  506. if (hasMapping(needsOnStateChange, type, method)) {
  507. return true;
  508. } else {
  509. return previousBundle != null
  510. && previousBundle.isNeedsOnStateChangeHandler(type, method);
  511. }
  512. }
  513. public Map<JClassType, Set<JMethod>> getNeedsOnStateChangeHandler() {
  514. return Collections.unmodifiableMap(needsOnStateChange);
  515. }
  516. public static JMethod findInheritedMethod(JClassType type,
  517. String methodName, JType... params) {
  518. JClassType currentType = type;
  519. while (currentType != null) {
  520. JMethod method = currentType.findMethod(methodName, params);
  521. if (method != null) {
  522. return method;
  523. }
  524. currentType = currentType.getSuperclass();
  525. }
  526. JClassType[] interfaces = type.getImplementedInterfaces();
  527. for (JClassType iface : interfaces) {
  528. JMethod method = iface.findMethod(methodName, params);
  529. if (method != null) {
  530. return method;
  531. }
  532. }
  533. return null;
  534. }
  535. }