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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2000-2014 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.findInheritedMethod(
  32. type, "createWidget").getEnclosingType();
  33. JMethod getWidget = ConnectorBundle.findInheritedMethod(type,
  34. "getWidget");
  35. JClassType widgetType = getWidget.getReturnType().isClass();
  36. // Needs GWT constructor if createWidget is not overridden
  37. if (createWidgetClass.getQualifiedSourceName().equals(
  38. AbstractComponentConnector.class.getCanonicalName())) {
  39. if (getWidget
  40. .getEnclosingType()
  41. .getQualifiedSourceName()
  42. .equals(AbstractComponentConnector.class
  43. .getCanonicalName())) {
  44. logger.log(Type.ERROR, type.getQualifiedSourceName()
  45. + " must override either createWidget or getWidget");
  46. throw new UnableToCompleteException();
  47. }
  48. bundle.setNeedsGwtConstructor(widgetType);
  49. // Also needs widget type to find the right GWT constructor
  50. bundle.setNeedsReturnType(type, getWidget);
  51. }
  52. // Check state properties for @DelegateToWidget
  53. JMethod getState = ConnectorBundle.findInheritedMethod(type,
  54. "getState");
  55. JClassType stateType = getState.getReturnType().isClass();
  56. Collection<Property> properties = bundle.getProperties(stateType);
  57. for (Property property : properties) {
  58. DelegateToWidget delegateToWidget = property
  59. .getAnnotation(DelegateToWidget.class);
  60. if (delegateToWidget != null) {
  61. // Generate meta data required for @DelegateToWidget
  62. bundle.setNeedsDelegateToWidget(property, stateType);
  63. // Find the delegate target method
  64. String methodName = DelegateToWidget.Helper
  65. .getDelegateTarget(property.getName(),
  66. delegateToWidget.value());
  67. JMethod delegatedSetter = ConnectorBundle
  68. .findInheritedMethod(widgetType, methodName,
  69. property.getPropertyType());
  70. if (delegatedSetter == null) {
  71. logger.log(
  72. Type.ERROR,
  73. widgetType.getName()
  74. + "."
  75. + methodName
  76. + "("
  77. + property.getPropertyType()
  78. .getSimpleSourceName()
  79. + ") required by @DelegateToWidget for "
  80. + stateType.getName() + "."
  81. + property.getName()
  82. + " can not be found.");
  83. throw new UnableToCompleteException();
  84. }
  85. bundle.setNeedsInvoker(widgetType, delegatedSetter);
  86. // GWT code needs widget type to find the target method
  87. bundle.setNeedsReturnType(type, getWidget);
  88. }
  89. }
  90. }
  91. }
  92. }