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.

WidgetInitVisitor.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.util.Collection;
  18. import com.google.gwt.core.ext.TreeLogger;
  19. import com.google.gwt.core.ext.TreeLogger.Type;
  20. import com.google.gwt.core.ext.UnableToCompleteException;
  21. import com.google.gwt.core.ext.typeinfo.JClassType;
  22. import com.google.gwt.core.ext.typeinfo.JMethod;
  23. import com.vaadin.client.ui.AbstractComponentConnector;
  24. import com.vaadin.shared.annotations.DelegateToWidget;
  25. public class WidgetInitVisitor extends TypeVisitor {
  26. @Override
  27. public void visitConnector(TreeLogger logger, JClassType type,
  28. ConnectorBundle bundle) throws UnableToCompleteException {
  29. if (ConnectorBundle.isConnectedComponentConnector(type)) {
  30. // The class in which createWidget is implemented
  31. JClassType createWidgetClass = ConnectorBundle
  32. .findInheritedMethod(type, "createWidget")
  33. .getEnclosingType();
  34. JMethod getWidget = ConnectorBundle.findInheritedMethod(type,
  35. "getWidget");
  36. JClassType widgetType = getWidget.getReturnType().isClass();
  37. // Needs GWT constructor if createWidget is not overridden
  38. if (createWidgetClass.getQualifiedSourceName().equals(
  39. AbstractComponentConnector.class.getCanonicalName())) {
  40. if (getWidget.getEnclosingType().getQualifiedSourceName()
  41. .equals(AbstractComponentConnector.class
  42. .getCanonicalName())) {
  43. logger.log(Type.ERROR, type.getQualifiedSourceName()
  44. + " must override either createWidget or getWidget");
  45. throw new UnableToCompleteException();
  46. }
  47. bundle.setNeedsGwtConstructor(widgetType);
  48. // Also needs widget type to find the right GWT constructor
  49. bundle.setNeedsReturnType(type, getWidget);
  50. }
  51. // Check state properties for @DelegateToWidget
  52. JMethod getState = ConnectorBundle.findInheritedMethod(type,
  53. "getState");
  54. JClassType stateType = getState.getReturnType().isClass();
  55. Collection<Property> properties = bundle.getProperties(stateType);
  56. for (Property property : properties) {
  57. DelegateToWidget delegateToWidget = property
  58. .getAnnotation(DelegateToWidget.class);
  59. if (delegateToWidget != null) {
  60. // Generate meta data required for @DelegateToWidget
  61. bundle.setNeedsDelegateToWidget(property, stateType);
  62. // Find the delegate target method
  63. String methodName = DelegateToWidget.Helper
  64. .getDelegateTarget(property.getName(),
  65. delegateToWidget.value());
  66. JMethod delegatedSetter = ConnectorBundle
  67. .findInheritedMethod(widgetType, methodName,
  68. property.getPropertyType());
  69. if (delegatedSetter == null) {
  70. logger.log(Type.ERROR,
  71. widgetType.getName() + "." + methodName + "("
  72. + property.getPropertyType()
  73. .getSimpleSourceName()
  74. + ") required by @DelegateToWidget for "
  75. + stateType.getName() + "."
  76. + property.getName()
  77. + " can not be found.");
  78. throw new UnableToCompleteException();
  79. }
  80. bundle.setNeedsInvoker(widgetType, delegatedSetter);
  81. // GWT code needs widget type to find the target method
  82. bundle.setNeedsReturnType(type, getWidget);
  83. }
  84. }
  85. }
  86. }
  87. }