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


##########
playground/frontend/playground_components/tools/extract_symbols_java/src/main/java/com/playground/extract_symbols/Main.java:
##########
@@ -0,0 +1,120 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package com.playground.extract_symbols;
+
+import com.esotericsoftware.yamlbeans.YamlConfig;
+import com.esotericsoftware.yamlbeans.YamlException;
+import com.esotericsoftware.yamlbeans.YamlWriter;
+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.io.StringWriter;
+import java.nio.file.Files;
+import java.util.*;
+
+public class Main {
+    public static void main(String[] args) throws IOException {
+        var sdkPath = args[0];
+        HashMap<String, ClassInfo> classInfoMap = getDirSymbols(sdkPath);
+        String yamlString = buildYamlString(classInfoMap);
+        System.out.println(yamlString);
+    }
+
+    private static HashMap<String, ClassInfo> getDirSymbols(String 
sdkPathString) throws IOException {
+        var classInfoMap = new HashMap<String, ClassInfo>();
+        var sdkPath = new File(sdkPathString).toPath().toAbsolutePath();
+        Files.walk(sdkPath).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()) {
+                        addClassSymbols(classInfoMap, 
unit.getClassByName(fileName).get());
+                    }
+                } catch (IOException | ParseProblemException ignored) {
+                }
+            }
+        });
+
+        return classInfoMap;
+    }
+
+    private static void addClassSymbols(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> 
classInfoMap) throws YamlException {
+        var stringWriter = new StringWriter();
+        var yamlWriter = new YamlWriter(stringWriter);
+        
yamlWriter.getConfig().writeConfig.setWriteClassname(YamlConfig.WriteClassName.NEVER);
+        var yamlMap = new LinkedHashMap<String, Map<String, List<String>>>();
+
+        classInfoMap.forEach((key, value) -> yamlMap.put(key, value.toMap()));
+        var sortedMap = sortMap(yamlMap);
+
+        yamlWriter.write(sortedMap);
+
+        yamlWriter.close();
+        return stringWriter.toString();
+    }
+
+    private static LinkedHashMap<String, Map<String, List<String>>> 
sortMap(HashMap<String, Map<String, List<String>>> yamlMap) {
+        var comparator = new Comparator<Map.Entry<String, Map<String, 
List<String>>>>() {
+            @Override
+            public int compare(Map.Entry<String, Map<String, List<String>>> 
o1, Map.Entry<String, Map<String, List<String>>> o2) {

Review Comment:
   Can we break this line? Does Java support trailing commas for that?
   Also if the variables are renamed to `a` and `b`, we save two columns.



##########
playground/frontend/playground_components/tools/extract_symbols_java/settings.gradle:
##########
@@ -0,0 +1,19 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+ 
+ rootProject.name = 'extract_symbols_java'

Review Comment:
   Unused?



##########
playground/frontend/playground_components/tools/extract_symbols_java/build.gradle:
##########
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * License); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+plugins {
+    id 'java'
+}
+
+group 'com.playground.extract_symbols'
+
+repositories {
+    mavenCentral()
+}
+
+ext {
+    javaMainClass = "com.playground.extract_symbols.Main"
+}
+
+dependencies {
+    implementation group: 'com.github.javaparser', name: 'javaparser-core', 
version: '3.23.1'
+    implementation group: 'com.esotericsoftware.yamlbeans', name: 'yamlbeans', 
version: '1.15'
+    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
+    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'

Review Comment:
   We do not use JUnit.



##########
playground/frontend/playground_components/lib/src/services/symbols/loaders/map.dart:
##########
@@ -17,20 +17,33 @@
  */
 
 import 'package:highlight/languages/go.dart';
+import 'package:highlight/languages/java.dart';
 import 'package:highlight/languages/python.dart';
+import 'package:highlight/languages/scala.dart';
 
 import '../../../assets/assets.gen.dart';
 import '../../../playground_components.dart';
 import 'yaml.dart';
 
 final symbolLoadersByMode = {
+  //
   go: YamlSymbolsLoader(
     path: Assets.symbols.goG,
     package: PlaygroundComponents.packageName,
   ),
 
+  java: YamlSymbolsLoader(
+    path: Assets.symbols.javaG,
+    package: PlaygroundComponents.packageName,
+  ),
+

Review Comment:
   Lets reuse this object so we do not read the 12k lines twice.



##########
playground/frontend/playground_components/tools/extract_symbols_java/src/main/java/com/playground/extract_symbols/Main.java:
##########
@@ -0,0 +1,120 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package com.playground.extract_symbols;
+
+import com.esotericsoftware.yamlbeans.YamlConfig;
+import com.esotericsoftware.yamlbeans.YamlException;
+import com.esotericsoftware.yamlbeans.YamlWriter;
+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.io.StringWriter;
+import java.nio.file.Files;
+import java.util.*;
+
+public class Main {
+    public static void main(String[] args) throws IOException {
+        var sdkPath = args[0];
+        HashMap<String, ClassInfo> classInfoMap = getDirSymbols(sdkPath);
+        String yamlString = buildYamlString(classInfoMap);
+        System.out.println(yamlString);
+    }
+
+    private static HashMap<String, ClassInfo> getDirSymbols(String 
sdkPathString) throws IOException {
+        var classInfoMap = new HashMap<String, ClassInfo>();
+        var sdkPath = new File(sdkPathString).toPath().toAbsolutePath();
+        Files.walk(sdkPath).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()) {
+                        addClassSymbols(classInfoMap, 
unit.getClassByName(fileName).get());
+                    }
+                } catch (IOException | ParseProblemException ignored) {
+                }
+            }
+        });
+
+        return classInfoMap;
+    }
+
+    private static void addClassSymbols(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> 
classInfoMap) throws YamlException {
+        var stringWriter = new StringWriter();
+        var yamlWriter = new YamlWriter(stringWriter);
+        
yamlWriter.getConfig().writeConfig.setWriteClassname(YamlConfig.WriteClassName.NEVER);
+        var yamlMap = new LinkedHashMap<String, Map<String, List<String>>>();
+
+        classInfoMap.forEach((key, value) -> yamlMap.put(key, value.toMap()));
+        var sortedMap = sortMap(yamlMap);
+
+        yamlWriter.write(sortedMap);
+
+        yamlWriter.close();
+        return stringWriter.toString();
+    }
+
+    private static LinkedHashMap<String, Map<String, List<String>>> 
sortMap(HashMap<String, Map<String, List<String>>> yamlMap) {
+        var comparator = new Comparator<Map.Entry<String, Map<String, 
List<String>>>>() {
+            @Override
+            public int compare(Map.Entry<String, Map<String, List<String>>> 
o1, Map.Entry<String, Map<String, List<String>>> o2) {
+                return o1.getKey().compareTo(o2.getKey());
+            }
+        };
+        var array = new ArrayList<>(yamlMap.entrySet());

Review Comment:
   `ArrayList` of what?



-- 
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