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.

FieldProperty.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.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.JField;
  26. import com.google.gwt.user.rebind.SourceWriter;
  27. public class FieldProperty extends Property {
  28. private final JField field;
  29. private FieldProperty(JClassType beanType, JField field) {
  30. super(field.getName(), beanType, field.getType());
  31. this.field = field;
  32. }
  33. @Override
  34. public boolean hasAccessorMethods() {
  35. return true;
  36. }
  37. @Override
  38. public void writeSetterBody(TreeLogger logger, SourceWriter w,
  39. String beanVariable, String valueVariable) {
  40. // Don't try to unbox Longs in javascript, as it's not supported.
  41. // (#13692)
  42. boolean shouldUnbox = !"long".equals(field.getType()
  43. .getSimpleSourceName());
  44. w.println("%s.@%s::%s = %s;", beanVariable, getBeanType()
  45. .getQualifiedSourceName(), getName(),
  46. shouldUnbox ? unboxValue(valueVariable) : valueVariable);
  47. }
  48. @Override
  49. public void writeGetterBody(TreeLogger logger, SourceWriter w,
  50. String beanVariable) {
  51. // Longs are not unboxed, as it's not supported. (#13692)
  52. boolean shouldBox = !"long".equals(field.getType()
  53. .getSimpleSourceName());
  54. String value = String.format("%s.@%s::%s", beanVariable, getBeanType()
  55. .getQualifiedSourceName(), getName());
  56. w.print("return ");
  57. w.print(shouldBox ? boxValue(value) : value);
  58. w.println(";");
  59. }
  60. public static Collection<FieldProperty> findProperties(JClassType type) {
  61. Collection<FieldProperty> properties = new ArrayList<FieldProperty>();
  62. List<JField> fields = getPublicFields(type);
  63. for (JField field : fields) {
  64. properties.add(new FieldProperty(field.getEnclosingType(), field));
  65. }
  66. return properties;
  67. }
  68. private static List<JField> getPublicFields(JClassType type) {
  69. Set<String> names = new HashSet<String>();
  70. ArrayList<JField> fields = new ArrayList<JField>();
  71. for (JClassType subType : type.getFlattenedSupertypeHierarchy()) {
  72. JField[] subFields = subType.getFields();
  73. for (JField field : subFields) {
  74. if (field.isPublic() && !field.isStatic()
  75. && names.add(field.getName())) {
  76. fields.add(field);
  77. }
  78. }
  79. }
  80. return fields;
  81. }
  82. @Override
  83. public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
  84. return field.getAnnotation(annotationClass);
  85. }
  86. }