drccrd commented on code in PR #3965:
URL: 
https://github.com/apache/incubator-kie-tools/pull/3965#discussion_r3842352156


##########
packages/drools-lsp/drools-completion/src/main/java/org/drools/completion/JavaSourceTypeParser.java:
##########
@@ -0,0 +1,435 @@
+/*
+ * 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 org.drools.completion;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Logger;
+
+import org.antlr.v4.runtime.BaseErrorListener;
+import org.antlr.v4.runtime.CharStreams;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.RecognitionException;
+import org.antlr.v4.runtime.Recognizer;
+import org.drools.drl.parser.antlr4.JavaLexer;
+import org.drools.drl.parser.antlr4.JavaParser;
+
+/**
+ * Parses {@code .java} source into {@link JavaSourceType}s using the ANTLR 
Java
+ * grammar generated into the {@code drools-parser} jar
+ * ({@code org.drools.drl.parser.antlr4.JavaParser}). Only top-level types are
+ * indexed; nested types are skipped. Best-effort: syntax errors are silenced 
so
+ * partial/edited buffers still yield whatever parsed cleanly, and the parser
+ * never throws.
+ *
+ * <p>Known limits (acceptable for typing/hover/lint): nested types are not
+ * indexed; interface member extraction is name-first (fields/constants may be
+ * partial); generic type arguments and array dimensions are erased to the raw
+ * simple name.
+ */
+public final class JavaSourceTypeParser {
+
+    private static final Logger logger = 
Logger.getLogger(JavaSourceTypeParser.class.getName());
+
+    private static final BaseErrorListener SILENT = new BaseErrorListener() {
+        @Override
+        public void syntaxError(Recognizer<?, ?> r, Object sym, int line, int 
col,
+                                String msg, RecognitionException e) {
+        }
+    };
+
+    private JavaSourceTypeParser() {
+    }
+
+    public static List<JavaSourceType> parse(String source) {
+        if (source == null || source.isBlank()) {
+            return Collections.emptyList();
+        }
+        try {
+            JavaLexer lexer = new JavaLexer(CharStreams.fromString(source));
+            lexer.removeErrorListeners();
+            lexer.addErrorListener(SILENT);
+            JavaParser parser = new JavaParser(new CommonTokenStream(lexer));
+            parser.removeErrorListeners();
+            parser.addErrorListener(SILENT);
+
+            JavaParser.CompilationUnitContext cu = parser.compilationUnit();
+            if (cu == null) {
+                return Collections.emptyList();
+            }
+            String pkg = (cu.packageDeclaration() != null
+                    && cu.packageDeclaration().qualifiedName() != null)
+                    ? cu.packageDeclaration().qualifiedName().getText() : "";
+
+            List<JavaSourceType> out = new ArrayList<>();
+            for (JavaParser.TypeDeclarationContext td : cu.typeDeclaration()) {
+                try {
+                    JavaSourceType t = fromTypeDeclaration(td, pkg);
+                    if (t != null) {
+                        out.add(t);
+                    }
+                } catch (Exception e) {
+                    logger.fine(() -> "Skipping malformed top-level type: " + 
e.getMessage());
+                }
+            }
+            return out;
+        } catch (Exception e) {
+            logger.fine(() -> "Failed to parse Java source: " + 
e.getMessage());
+            return Collections.emptyList();
+        }
+    }
+
+    private static JavaSourceType 
fromTypeDeclaration(JavaParser.TypeDeclarationContext td, String pkg) {
+        if (td.classDeclaration() != null) {
+            return fromClass(td.classDeclaration(), pkg);
+        }
+        if (td.enumDeclaration() != null) {
+            return fromEnum(td.enumDeclaration(), pkg);
+        }
+        if (td.interfaceDeclaration() != null) {
+            return fromInterface(td.interfaceDeclaration(), pkg);
+        }
+        if (td.recordDeclaration() != null) {
+            return fromRecord(td.recordDeclaration(), pkg);
+        }
+        return null; // annotation type / bare ';'
+    }
+
+    private static JavaSourceType fromClass(JavaParser.ClassDeclarationContext 
cd, String pkg) {
+        String simpleName = cd.identifier().getText();
+        String extendsName = extendsSimpleNameOf(cd.typeType());
+        List<String> interfaces = (cd.IMPLEMENTS() != null && 
!cd.typeList().isEmpty())
+                ? simplifyAll(cd.typeList(0)) : List.of();
+
+        List<Field> fields = new ArrayList<>();
+        List<Field> getters = new ArrayList<>();
+        List<String> ctors = new ArrayList<>();
+        if (cd.classBody() != null) {
+            collectBodyMembers(cd.classBody().classBodyDeclaration(), fields, 
getters, ctors, simpleName);
+        }

Review Comment:
   Same as 
https://github.com/apache/incubator-kie-tools/pull/3965#discussion_r3842312235 
- I don't think building in an empty constructor display in hover prior to 
class compilation adds any real value



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to