Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MethodProperty.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.lang.annotation.Annotation;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.List;
  21. import com.google.gwt.core.ext.TreeLogger;
  22. import com.google.gwt.core.ext.typeinfo.JClassType;
  23. import com.google.gwt.core.ext.typeinfo.JMethod;
  24. import com.google.gwt.core.ext.typeinfo.JType;
  25. import com.google.gwt.user.rebind.SourceWriter;
  26. public class MethodProperty extends Property {
  27. private final JMethod setter;
  28. private MethodProperty(JClassType beanType, JMethod setter) {
  29. super(getTransportFieldName(setter), beanType, setter
  30. .getParameterTypes()[0]);
  31. this.setter = setter;
  32. }
  33. public static Collection<MethodProperty> findProperties(JClassType type) {
  34. Collection<MethodProperty> properties = new ArrayList<MethodProperty>();
  35. List<JMethod> setters = getSetters(type);
  36. for (JMethod setter : setters) {
  37. properties.add(new MethodProperty(type, setter));
  38. }
  39. return properties;
  40. }
  41. /**
  42. * Returns a list of all setters found in the beanType or its parent class
  43. *
  44. * @param beanType
  45. * The type to check
  46. * @return A list of setter methods from the class and its parents
  47. */
  48. private static List<JMethod> getSetters(JClassType beanType) {
  49. List<JMethod> setterMethods = new ArrayList<JMethod>();
  50. while (beanType != null
  51. && !beanType.getQualifiedSourceName().equals(
  52. Object.class.getName())) {
  53. for (JMethod method : beanType.getMethods()) {
  54. // Process all setters that have corresponding fields
  55. if (!method.isPublic() || method.isStatic()
  56. || !method.getName().startsWith("set")
  57. || method.getParameterTypes().length != 1) {
  58. // Not setter, skip to next method
  59. continue;
  60. }
  61. setterMethods.add(method);
  62. }
  63. beanType = beanType.getSuperclass();
  64. }
  65. return setterMethods;
  66. }
  67. @Override
  68. public void writeSetterBody(TreeLogger logger, SourceWriter w,
  69. String beanVariable, String valueVariable) {
  70. w.print("((");
  71. w.print(getBeanType().getQualifiedSourceName());
  72. w.print(") ");
  73. w.print(beanVariable);
  74. w.print(").");
  75. w.print(setter.getName());
  76. w.print("((");
  77. w.print(getUnboxedPropertyTypeName());
  78. w.print(") ");
  79. w.print(valueVariable);
  80. w.println(");");
  81. }
  82. @Override
  83. public void writeGetterBody(TreeLogger logger, SourceWriter w,
  84. String beanVariable) {
  85. w.print("return ((");
  86. w.print(getBeanType().getQualifiedSourceName());
  87. w.print(") ");
  88. w.print(beanVariable);
  89. w.print(").");
  90. w.print(findGetter(getBeanType(), setter));
  91. w.print("();");
  92. }
  93. private String findGetter(JClassType beanType, JMethod setterMethod) {
  94. JType setterParameterType = setterMethod.getParameterTypes()[0];
  95. String fieldName = setterMethod.getName().substring(3);
  96. if (setterParameterType.getQualifiedSourceName().equals(
  97. boolean.class.getName())) {
  98. return "is" + fieldName;
  99. } else {
  100. return "get" + fieldName;
  101. }
  102. }
  103. private static String getTransportFieldName(JMethod setter) {
  104. String baseName = setter.getName().substring(3);
  105. return Character.toLowerCase(baseName.charAt(0))
  106. + baseName.substring(1);
  107. }
  108. @Override
  109. public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
  110. return setter.getAnnotation(annotationClass);
  111. }
  112. }