Malarg commented on code in PR #24588:
URL: https://github.com/apache/beam/pull/24588#discussion_r1046753685


##########
playground/frontend/playground_components/tools/extract_symbols_java/settings.gradle:
##########
@@ -0,0 +1 @@
+rootProject.name = 'extract_java_symbols'

Review Comment:
   done



##########
playground/frontend/playground_components/tools/extract_symbols_java/src/main/java/com/playground/extract_symbols/Main.java:
##########
@@ -0,0 +1,93 @@
+package com.playground.extract_symbols;
+
+import com.github.javaparser.ParseProblemException;
+import com.github.javaparser.StaticJavaParser;
+import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
+import com.github.javaparser.ast.body.FieldDeclaration;
+import com.github.javaparser.ast.body.MethodDeclaration;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.HashMap;
+import java.util.Map;
+
+public class Main {
+    public static void main(String[] args) throws IOException {
+        var sdkPath = args[0];
+        HashMap<String, ClassInfo> classInfoMap = collectClassInfo(sdkPath);
+        String yamlString = buildYamlString(classInfoMap);
+        System.out.println(yamlString);
+    }
+
+    private static HashMap<String, ClassInfo> collectClassInfo(String sdkPath) 
throws IOException {
+        var classInfoList = new HashMap<String, ClassInfo>();
+        var paths = new File(sdkPath).toPath().toAbsolutePath();
+        Files.walk(paths).forEach(path -> {
+            var stringPath = path.toString();
+            if (stringPath.endsWith(".java") && !stringPath.contains("test")) {
+                var fileName = 
stringPath.substring(stringPath.lastIndexOf("/") + 1).replace(".java", "");
+                try {
+                    var unit = StaticJavaParser.parse(path);
+                    if (unit.getClassByName(fileName).isPresent()) {
+                        buildClass(classInfoList, 
unit.getClassByName(fileName).get());
+                    }
+                } catch (IOException | ParseProblemException ignored) {
+                }
+            }
+        });
+
+        return classInfoList;
+    }
+
+    private static void buildClass(HashMap<String, ClassInfo> classInfoList, 
ClassOrInterfaceDeclaration cl) {
+        if (!cl.isPublic()) {
+            return;
+        }
+
+        ClassInfo classInfo;
+        if (classInfoList.containsKey(cl.getNameAsString())) {
+            classInfo = classInfoList.get(cl.getNameAsString());
+        } else {
+            classInfo = new ClassInfo();
+            classInfoList.put(cl.getNameAsString(), classInfo);
+        }
+
+        cl.findAll(MethodDeclaration.class).forEach(method -> {
+            if (method.isPublic()) {
+                classInfo.publicMethods.add(method.getNameAsString());
+            }
+        });
+        cl.findAll(FieldDeclaration.class).forEach(field -> {
+            if (field.isPublic()) {
+                
classInfo.publicFields.add(field.getVariable(0).getNameAsString());
+            }
+        });
+    }
+
+    private static String buildYamlString(HashMap<String, ClassInfo> 
classInfoList) {
+        final String[] resultList = new String[classInfoList.size()];
+        var i = 0;
+        for (Map.Entry<String, ClassInfo> entry : classInfoList.entrySet()) {
+            String name = entry.getKey();
+            ClassInfo classInfo = entry.getValue();
+            var yaml = toYaml(name, classInfo);
+            resultList[i] = yaml;
+            i++;
+        }
+        return String.join("\n", resultList);
+    }
+
+    private static String toYaml(String name, ClassInfo classInfo) {
+        var methods = classInfo.publicMethods;
+        var properties = classInfo.publicFields;
+
+        var methodsString = methods.isEmpty() ? "" : String.join("\n  - ", 
methods);
+        var propertiesString = properties.isEmpty() ? "" : String.join("\n  - 
", properties);
+
+        var methodsListString = methodsString.isEmpty() ? "" : " \n  methods: 
\n  - " + methodsString;
+        var propertiesListString = propertiesString.isEmpty() ? "" : " \n  
properties: \n  - " + propertiesString;
+
+        return String.format("%s: %s%s", name, methodsListString, 
propertiesListString);
+    }

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to