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

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