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.

Helper.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarqube.wsgenerator;
  21. import com.google.common.base.CaseFormat;
  22. import com.google.gson.JsonPrimitive;
  23. import java.io.BufferedReader;
  24. import java.io.InputStream;
  25. import java.io.InputStreamReader;
  26. import java.util.Collections;
  27. import java.util.HashSet;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import java.util.stream.Collectors;
  32. import java.util.stream.Stream;
  33. import static java.util.Arrays.asList;
  34. public class Helper {
  35. static final Set<String> PATH_EXCLUSIONS = new HashSet<>(asList("api/orchestrator"));
  36. private static final String OUTPUT_DIR = "build/generated-sources/results";
  37. private final Map<String, List<String[]>> responseTypes;
  38. public Helper() {
  39. InputStream inputStream = Helper.class.getResourceAsStream("/responseClasses.config");
  40. responseTypes = new BufferedReader(new InputStreamReader(inputStream))
  41. .lines()
  42. .map(line -> line.split("\\s+"))
  43. .collect(Collectors.groupingBy(arr -> arr[0]));
  44. }
  45. public boolean isIncluded(String path) {
  46. return !PATH_EXCLUSIONS.contains(path);
  47. }
  48. public String packageName() {
  49. return "org.sonarqube.ws.client";
  50. }
  51. public String packageName(String path) {
  52. return packageName() + "." + rawName(path).toLowerCase();
  53. }
  54. private String rawName(String path) {
  55. String x = path.replaceFirst("^api\\/", "");
  56. if (x.contains("_")) {
  57. return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, x.toLowerCase());
  58. }
  59. return capitalizeFirstLetter(x);
  60. }
  61. public String className(String path) {
  62. String name = rawName(path);
  63. return capitalizeFirstLetter(name) + "Service";
  64. }
  65. public String defaultWsClientFieldName(String path) {
  66. String name = rawName(path);
  67. return lowercaseFirstLetter(name) + "Service";
  68. }
  69. public String defaultWsClientMethodName(String path) {
  70. String name = rawName(path);
  71. return lowercaseFirstLetter(name);
  72. }
  73. public String webserviceTypeImport(String path) {
  74. return "import " + packageName(path) + "." + className(path) + ";";
  75. }
  76. private String capitalizeFirstLetter(String name) {
  77. return name.substring(0, 1).toUpperCase() + name.substring(1);
  78. }
  79. private String lowercaseFirstLetter(String name) {
  80. return name.substring(0, 1).toLowerCase() + name.substring(1);
  81. }
  82. public String responseType(String path, String action) {
  83. return responseTypeFullyQualified(path, action).replaceFirst("^.*\\.", "");
  84. }
  85. private String responseTypeFullyQualified(String path, String action) {
  86. String fullPath = path + "/" + action;
  87. List<String[]> responseTypesConfig = responseTypes.get(fullPath);
  88. String fullyQualified;
  89. if (responseTypesConfig == null) {
  90. fullyQualified = guessResponseType(path, action);
  91. responseTypes.put(fullPath, Collections.singletonList(new String[] {fullPath, fullyQualified}));
  92. } else {
  93. fullyQualified = responseTypesConfig.get(0)[1];
  94. }
  95. return fullyQualified;
  96. }
  97. private String guessResponseType(String path, String action) {
  98. return guessResponseOuterClassName(path).flatMap(
  99. potentialClassName -> guessResponseInnerClassName(action).flatMap(potentialInnerClassName -> {
  100. try {
  101. String guess = "org.sonarqube.ws." + potentialClassName + "$" + potentialInnerClassName;
  102. Helper.class.forName(guess);
  103. return Stream.of(guess.replaceFirst("\\$", "."));
  104. } catch (ClassNotFoundException e) {
  105. }
  106. return Stream.empty();
  107. })).findFirst().orElseGet(() -> {
  108. return "String";
  109. });
  110. }
  111. private Stream<String> guessResponseInnerClassName(String action) {
  112. return Stream.of(
  113. rawName(action) + "Response",
  114. rawName(action) + "WsResponse",
  115. "Ws" + rawName(action) + "Response");
  116. }
  117. private Stream<String> guessResponseOuterClassName(String path) {
  118. return Stream.of(
  119. rawName(path),
  120. "Ws" + rawName(path),
  121. rawName(path) + "Ws");
  122. }
  123. public String responseTypeImport(String path, String action) {
  124. String fullyQualified = responseTypeFullyQualified(path, action);
  125. if ("String".equals(fullyQualified)) {
  126. return null;
  127. }
  128. return "import " + fullyQualified + ";";
  129. }
  130. public String methodName(String path, String action) {
  131. return lowercaseFirstLetter(rawName(action));
  132. }
  133. public String requestType(String path, String action) {
  134. return rawName(action) + "Request";
  135. }
  136. public String parameterGetter(String parameter) {
  137. return "get" + rawName(parameter);
  138. }
  139. public String parameterSetter(String parameter) {
  140. return "set" + rawName(parameter);
  141. }
  142. public String setterParameter(String parameter) {
  143. return lowercaseFirstLetter(rawName(parameter));
  144. }
  145. public String setterParameterType(String parameter, JsonPrimitive parameterDescription) {
  146. if (parameter.equals("values") || parameter.equals("fieldValues") || parameter.equals("keys")) {
  147. return "List<String>";
  148. }
  149. if (parameterDescription != null && parameterDescription.getAsString().matches(".*[Cc]omma.?separated.*|.*[Ll]ist of.*")) {
  150. return "List<String>";
  151. }
  152. return "String";
  153. }
  154. public String apiDocUrl(String path) {
  155. return "https://next.sonarqube.com/sonarqube/web_api/" + path;
  156. }
  157. public String apiDocUrl(String path, String action) {
  158. return apiDocUrl(path) + "/" + action;
  159. }
  160. public String file(String path) {
  161. return OUTPUT_DIR + "/org/sonarqube/ws/client/" + rawName(path).toLowerCase() + "/" + className(path) + ".java";
  162. }
  163. public String defaultWsClientFile() {
  164. return OUTPUT_DIR + "/org/sonarqube/ws/client/DefaultWsClient.java";
  165. }
  166. public String wsClientFile() {
  167. return OUTPUT_DIR + "/org/sonarqube/ws/client/WsClient.java";
  168. }
  169. public String packageInfoFile() {
  170. return OUTPUT_DIR + "/org/sonarqube/ws/client/package-info.java";
  171. }
  172. public String packageInfoFile(String path) {
  173. return OUTPUT_DIR + "/org/sonarqube/ws/client/" + rawName(path).toLowerCase() + "/package-info.java";
  174. }
  175. public String requestFile(String path, String action) {
  176. return OUTPUT_DIR + "/org/sonarqube/ws/client/" + rawName(path).toLowerCase() + "/" + requestType(path, action) + ".java";
  177. }
  178. }