Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

MethodProperty.java 5.0KB

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