Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. public class Main {
  2.  
  3. public static void main(String[] args) throws IOException {
  4. File dir = new File("C:/Users/Lucia/Desktop/test");
  5. HashMap<String, HashMap<String, String>> paramsHash = new HashMap<>();
  6. for (File file : Objects.requireNonNull(dir.listFiles())) {
  7. String fileToString = readFile(file.toString());
  8. String ctorPattern = "(public)(\\s+)((?:[a-z][a-z0-9_]*))(\\(.*\\))";
  9. Pattern p = Pattern.compile(ctorPattern, Pattern.CASE_INSENSITIVE);
  10. Matcher m = p.matcher(fileToString);
  11. if (m.find()) {
  12. ArrayList<String> paramTypes = new ArrayList<>();
  13. ArrayList<String> paramNames = new ArrayList<>();
  14. String ctor = m.group().replaceFirst("public ", "");
  15. ctor = ctor.replace(",", "");
  16. ctor = ctor.replace(")", "");
  17. String[] ctorArr = ctor.split("\\(");
  18. String className = ctorArr[0];
  19. String params = ctorArr[1];
  20. String[] paramsArr = params.split(" ");
  21. ArrayList<String> paramsArrList = new ArrayList<>(Arrays.asList(paramsArr));
  22. for (String s : paramsArrList) {
  23. if (paramsArrList.indexOf(s) % 2 == 0) {
  24. paramTypes.add(s);
  25. } else {
  26. paramNames.add(s);
  27. }
  28. }
  29. HashMap<String, String> paramsPairing = new HashMap<>();
  30. for (String s : paramTypes) {
  31. paramsPairing.put(s, paramNames.get(paramTypes.indexOf(s)));
  32. }
  33. paramsHash.put(className, paramsPairing);
  34. }
  35. }
  36. JSONObject actionsJSON = new JSONObject();
  37. for (Map.Entry<String, HashMap<String, String>> e : paramsHash.entrySet()) {
  38. String key = e.getKey();
  39. HashMap<String, String> value = e.getValue();
  40. actionsJSON.put(key, value);
  41. }
  42. try (FileWriter file = new FileWriter("C:/Users/Lucia/Desktop/stsmods/STSRegex/test.json")) {
  43. file.write(actionsJSON.toJSONString());
  44. System.out.println("Successfully Copied JSON Object to File...");
  45. }
  46. }
  47.  
  48. private static String readFile(String path) throws IOException {
  49. byte[] encoded = Files.readAllBytes(Paths.get(path));
  50. return new String(encoded, StandardCharsets.UTF_8);
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement