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

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