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

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