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