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

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