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

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