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.

Property.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2000-2016 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 com.google.gwt.core.ext.TreeLogger;
  19. import com.google.gwt.core.ext.typeinfo.JClassType;
  20. import com.google.gwt.core.ext.typeinfo.JPrimitiveType;
  21. import com.google.gwt.core.ext.typeinfo.JType;
  22. import com.google.gwt.user.rebind.SourceWriter;
  23. public abstract class Property implements Comparable<Property> {
  24. private final String name;
  25. private final JClassType beanType;
  26. private final JType propertyType;
  27. protected Property(String name, JClassType beanType, JType propertyType) {
  28. this.name = name;
  29. this.beanType = beanType;
  30. this.propertyType = propertyType;
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. public JType getPropertyType() {
  36. return propertyType;
  37. }
  38. public String getUnboxedPropertyTypeName() {
  39. JType propertyType = getPropertyType();
  40. JPrimitiveType primitive = propertyType.isPrimitive();
  41. if (primitive != null) {
  42. return primitive.getQualifiedBoxedSourceName();
  43. } else {
  44. return propertyType.getQualifiedSourceName();
  45. }
  46. }
  47. public String boxValue(String codeSnippet) {
  48. JPrimitiveType primitive = propertyType.isPrimitive();
  49. if (primitive == null) {
  50. return codeSnippet;
  51. } else {
  52. return String.format("@%s::valueOf(%s)(%s)",
  53. primitive.getQualifiedBoxedSourceName(),
  54. propertyType.getJNISignature(), codeSnippet);
  55. }
  56. }
  57. public String unboxValue(String codeSnippet) {
  58. JPrimitiveType primitive = propertyType.isPrimitive();
  59. if (primitive == null) {
  60. return codeSnippet;
  61. } else {
  62. return String.format("%s.@%s::%sValue()()", codeSnippet,
  63. primitive.getQualifiedBoxedSourceName(),
  64. primitive.getSimpleSourceName());
  65. }
  66. }
  67. public JClassType getBeanType() {
  68. return beanType;
  69. }
  70. public abstract void writeSetterBody(TreeLogger logger, SourceWriter w,
  71. String beanVariable, String valueVariable);
  72. public abstract void writeGetterBody(TreeLogger logger, SourceWriter w,
  73. String beanVariable);
  74. public abstract boolean hasAccessorMethods();
  75. @Override
  76. public boolean equals(Object obj) {
  77. if (this == obj) {
  78. return true;
  79. } else if (obj instanceof Property) {
  80. Property other = (Property) obj;
  81. return other.getClass() == getClass()
  82. && other.getBeanType().equals(getBeanType())
  83. && other.getName().equals(getName());
  84. } else {
  85. return false;
  86. }
  87. }
  88. @Override
  89. public int hashCode() {
  90. return getClass().hashCode() * 31
  91. ^ 2 + getBeanType().hashCode() * 31 + getName().hashCode();
  92. }
  93. @Override
  94. public int compareTo(Property o) {
  95. int comp = getName().compareTo(o.getName());
  96. if (comp == 0) {
  97. comp = getBeanType().getQualifiedSourceName()
  98. .compareTo(o.getBeanType().getQualifiedSourceName());
  99. }
  100. if (comp == 0) {
  101. comp = getClass().getCanonicalName()
  102. .compareTo(o.getClass().getCanonicalName());
  103. }
  104. return comp;
  105. }
  106. public abstract <T extends Annotation> T getAnnotation(
  107. Class<T> annotationClass);
  108. }