1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaソースコードのPropertyを獲得する

Last updated at Posted at 2021-11-15
package org.tei.mvcbinding;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@SpringBootApplication
public class MvcBindingApplication {

    public static void main(String[] args) throws IOException {
        String originalFilePath = "/Users/XXX/IdeaProjects/mvc-binding/src/main/java/org/XXX/mvcbinding/controller/Admin.java";
        Map<String, MyJavaField> originalMap = getFieldMap(originalFilePath);
        String targetFilePath = "/Users/XXX/IdeaProjects/mvc-binding/src/main/java/org/XXX/mvcbinding/controller/User.java";
        Map<String, MyJavaField> targetMap = getFieldMap(targetFilePath);


        Set<String> originalSet = new HashSet<>(originalMap.keySet());
        Set<String> targetSet = new HashSet<>(targetMap.keySet());
        targetSet.removeAll(originalSet);
        // display result
        for (String key : targetSet) {
            MyJavaField myJavaField = targetMap.get(key);
            System.out.print(myJavaField.getComment());
            System.out.println(myJavaField.getContent());
        }
    }

    private static Map<String, MyJavaField> getFieldMap(String filePath) throws IOException {
        FileInputStream inputStream = new FileInputStream(filePath);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        boolean readyToParse = false;
        Map<String, MyJavaField> propertyMap = new HashMap<>();
        StringBuilder comment = new StringBuilder();
        while ((line = bufferedReader.readLine()) != null) {
            line = line.trim();
            if (line.isBlank()) {
                continue;
            }
            if (line.contains("}")) {
                break;
            }
            if (readyToParse) {
                if (!line.startsWith("//") && !line.startsWith("/*") && !line.startsWith("*")) {
                    int endIndex = line.indexOf(";");
                    if (endIndex > 0) {
                        int j = 0;
                        for (int i = endIndex; i >= 0; i--) {
                            if (' ' == (line.charAt(i))) {
                                j = i;
                                break;
                            }
                        }
                        String fieldName = line.substring(j, endIndex).trim();
                        MyJavaField field = new MyJavaField(line, comment.toString());
                        comment.delete(0, comment.length());
                        propertyMap.put(fieldName, field);
                    }

                } else {
                    comment.append(line).append("\n");
                }

            }
            if (line.contains("{")) {
                readyToParse = true;
            }
        }
        //todo debug
        // System.out.println("propertyMap => " + propertyMap);
        //close
        inputStream.close();
        bufferedReader.close();
        return propertyMap;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MyJavaField {
        private String content;
        private String comment;

    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?