Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) throws IOException {
- File dir = new File("C:/Users/Lucia/Desktop/test");
- HashMap<String, HashMap<String, String>> paramsHash = new HashMap<>();
- for (File file : Objects.requireNonNull(dir.listFiles())) {
- String fileToString = readFile(file.toString());
- String ctorPattern = "(public)(\\s+)((?:[a-z][a-z0-9_]*))(\\(.*\\))";
- Pattern p = Pattern.compile(ctorPattern, Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(fileToString);
- if (m.find()) {
- ArrayList<String> paramTypes = new ArrayList<>();
- ArrayList<String> paramNames = new ArrayList<>();
- String ctor = m.group().replaceFirst("public ", "");
- ctor = ctor.replace(",", "");
- ctor = ctor.replace(")", "");
- String[] ctorArr = ctor.split("\\(");
- String className = ctorArr[0];
- String params = ctorArr[1];
- String[] paramsArr = params.split(" ");
- ArrayList<String> paramsArrList = new ArrayList<>(Arrays.asList(paramsArr));
- for (String s : paramsArrList) {
- if (paramsArrList.indexOf(s) % 2 == 0) {
- paramTypes.add(s);
- } else {
- paramNames.add(s);
- }
- }
- HashMap<String, String> paramsPairing = new HashMap<>();
- for (String s : paramTypes) {
- paramsPairing.put(s, paramNames.get(paramTypes.indexOf(s)));
- }
- paramsHash.put(className, paramsPairing);
- }
- }
- JSONObject actionsJSON = new JSONObject();
- for (Map.Entry<String, HashMap<String, String>> e : paramsHash.entrySet()) {
- String key = e.getKey();
- HashMap<String, String> value = e.getValue();
- actionsJSON.put(key, value);
- }
- try (FileWriter file = new FileWriter("C:/Users/Lucia/Desktop/stsmods/STSRegex/test.json")) {
- file.write(actionsJSON.toJSONString());
- System.out.println("Successfully Copied JSON Object to File...");
- }
- }
- private static String readFile(String path) throws IOException {
- byte[] encoded = Files.readAllBytes(Paths.get(path));
- return new String(encoded, StandardCharsets.UTF_8);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement