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

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