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 2.8KB

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