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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.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. w.println("%s.@%s::%s = %s;", beanVariable,
  41. getBeanType().getQualifiedSourceName(), getName(),
  42. unboxValue(valueVariable));
  43. }
  44. @Override
  45. public void writeGetterBody(TreeLogger logger, SourceWriter w,
  46. String beanVariable) {
  47. String value = String.format("%s.@%s::%s", beanVariable,
  48. getBeanType().getQualifiedSourceName(), getName());
  49. w.print("return ");
  50. w.print(boxValue(value));
  51. w.println(";");
  52. }
  53. public static Collection<FieldProperty> findProperties(JClassType type) {
  54. Collection<FieldProperty> properties = new ArrayList<>();
  55. List<JField> fields = getPublicFields(type);
  56. for (JField field : fields) {
  57. properties.add(new FieldProperty(field.getEnclosingType(), field));
  58. }
  59. return properties;
  60. }
  61. private static List<JField> getPublicFields(JClassType type) {
  62. Set<String> names = new HashSet<>();
  63. List<JField> fields = new ArrayList<>();
  64. for (JClassType subType : type.getFlattenedSupertypeHierarchy()) {
  65. JField[] subFields = subType.getFields();
  66. for (JField field : subFields) {
  67. if (field.isPublic() && !field.isStatic()
  68. && names.add(field.getName())) {
  69. fields.add(field);
  70. }
  71. }
  72. }
  73. return fields;
  74. }
  75. @Override
  76. public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
  77. return field.getAnnotation(annotationClass);
  78. }
  79. }