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

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