PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/etlunit-feature-compiler/src/main/java/org/bitbucket/bradleysmithllc/etlunit/maven/util/JavaProperty.java

https://bitbucket.org/bradleysmithllc/etl-unit
Java | 174 lines | 123 code | 31 blank | 20 comment | 15 complexity | f5c39744347b74a6a049c78bc43d621d MD5 | raw file
  1. package org.bitbucket.bradleysmithllc.etlunit.maven.util;
  2. /*
  3. * #%L
  4. * etlunit-feature-compiler Maven Mojo
  5. * %%
  6. * Copyright (C) 2010 - 2014 bradleysmithllc
  7. * %%
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * #L%
  20. */
  21. import com.sun.codemodel.*;
  22. import org.bitbucket.bradleysmithllc.json.validator.JsonSchemaObjectNode;
  23. import java.util.List;
  24. import java.util.Map;
  25. public class JavaProperty
  26. {
  27. private final String name;
  28. private final List<String> enumValues;
  29. private final Class dataTypeClass;
  30. private final boolean required;
  31. public JavaProperty(String name, JsonSchemaObjectNode.valid_type type, List<String> enumValues, boolean required)
  32. {
  33. this.name = name;
  34. this.required = required;
  35. this.enumValues = enumValues;
  36. if (enumValues == null)
  37. {
  38. switch (type)
  39. {
  40. case t_string:
  41. dataTypeClass = String.class;
  42. break;
  43. case t_number:
  44. dataTypeClass = String.class;
  45. break;
  46. case t_integer:
  47. dataTypeClass = int.class;
  48. break;
  49. case t_boolean:
  50. dataTypeClass = boolean.class;
  51. break;
  52. case t_object:
  53. dataTypeClass = Map.class;
  54. break;
  55. case t_array:
  56. dataTypeClass = List.class;
  57. break;
  58. case t_any:
  59. dataTypeClass = Object.class;
  60. break;
  61. case t_null:
  62. throw new IllegalArgumentException("Data type null not supported");
  63. default:
  64. throw new IllegalArgumentException("Data type not supported");
  65. }
  66. }
  67. else
  68. {
  69. this.dataTypeClass = null;
  70. }
  71. }
  72. public void append(JDefinedClass buffer) throws JClassAlreadyExistsException
  73. {
  74. JFieldVar field = buffer.field(JMod.FINAL | JMod.PUBLIC | JMod.STATIC, String.class, getProperPropertyName().toUpperCase() + "_JSON_NAME");
  75. field.init(JExpr.lit(name));
  76. JType dt = null;
  77. if (dataTypeClass == null)
  78. {
  79. // this is an enum
  80. JDefinedClass en = buffer._enum(getPropertyName() + "_enum");
  81. for (int i = 0; i < enumValues.size(); i++)
  82. {
  83. String e = enumValues.get(i);
  84. en.enumConstant(makePropertyName(e));
  85. }
  86. dt = en;
  87. }
  88. else
  89. {
  90. dt = buffer.getPackage().owner()._ref(dataTypeClass);
  91. }
  92. JFieldVar pfield = buffer.field(JMod.PRIVATE, dt, getPropertyName());
  93. JMethod meth = buffer.method(JMod.PUBLIC | JMod.FINAL, boolean.class, "has" + getProperPropertyName());
  94. JBlock bod = meth.body();
  95. bod.directStatement("return " + getPropertyName() + " != null;");
  96. JMethod getMth = buffer.method(JMod.PUBLIC | JMod.FINAL, dt, "get" + getProperPropertyName());
  97. if (required)
  98. {
  99. getMth.body().block().directStatement("if (!has" + getProperPropertyName() + "()) {throw new IllegalStateException(\"Property '" + getPropertyName() + "' never assigned\");}");
  100. }
  101. getMth.body().block()._return(pfield);
  102. }
  103. public String getProperPropertyName()
  104. {
  105. return makeProperPropertyName(name);
  106. }
  107. public static String makeProperPropertyName(String property)
  108. {
  109. String base = makePropertyName(property);
  110. base = Character.toUpperCase(base.charAt(0)) + base.substring(1);
  111. return base;
  112. }
  113. public String getPropertyName()
  114. {
  115. return makePropertyName(name);
  116. }
  117. public static String makePropertyName(String property)
  118. {
  119. StringBuffer buffer = new StringBuffer();
  120. char[] array = property.toCharArray();
  121. boolean capNext = false;
  122. for (char ch : array)
  123. {
  124. if (ch == '-' || ch == '_' || ch == '.')
  125. {
  126. capNext = true;
  127. }
  128. else
  129. {
  130. if (capNext)
  131. {
  132. buffer.append(Character.toUpperCase(ch));
  133. capNext = false;
  134. }
  135. else
  136. {
  137. buffer.append(ch);
  138. }
  139. }
  140. }
  141. return buffer.toString();
  142. }
  143. }