Repository: kythe/kythe
Pull request #5814 (2023-08-25 18:47:57)

fix(java_indexer): fix ctor/generic signatures


Patch:
diff --git a/kythe/java/com/google/devtools/kythe/analyzers/java/MarkedSources.java b/kythe/java/com/google/devtools/kythe/analyzers/java/MarkedSources.java
index 09d08af49..653a7355b 100644
--- a/kythe/java/com/google/devtools/kythe/analyzers/java/MarkedSources.java
+++ b/kythe/java/com/google/devtools/kythe/analyzers/java/MarkedSources.java
@@ -132,7 +132,7 @@ public final class MarkedSources {
       @Nullable Iterable<MarkedSource> postChildren,
       @Nullable MarkedSource markedType) {
     MarkedSource.Builder markedSource = msBuilder == null ? MarkedSource.newBuilder() : msBuilder;
-    if (markedType != null) {
+    if (markedType != null && sym.getKind() != ElementKind.CONSTRUCTOR) {
       markedSource.addChild(markedType);
     }
     String identToken = buildContext(markedSource.addChildBuilder(), sym, signatureGenerator);
@@ -141,7 +141,7 @@ public final class MarkedSources {
         markedSource
             .addChildBuilder()
             .setKind(MarkedSource.Kind.IDENTIFIER)
-            .setPreText("<" + sym.getSimpleName() + ">");
+            .setPreText("" + sym.getSimpleName());
         break;
       case CONSTRUCTOR:
       case METHOD:
@@ -160,6 +160,19 @@ public final class MarkedSources {
             .setPostChildText(", ")
             .setPostText(")");
         break;
+      case ENUM:
+      case CLASS:
+      case INTERFACE:
+        markedSource.addChildBuilder().setKind(MarkedSource.Kind.IDENTIFIER).setPreText(identToken);
+        if (!sym.getTypeParameters().isEmpty()) {
+          markedSource
+              .addChildBuilder()
+              .setKind(MarkedSource.Kind.PARAMETER_LOOKUP_BY_TPARAM)
+              .setPreText("<")
+              .setPostText(">")
+              .setPostChildText(", ");
+        }
+        break;
       default:
         markedSource.addChildBuilder().setKind(MarkedSource.Kind.IDENTIFIER).setPreText(identToken);
         break;


Test patch:
diff --git a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/BUILD b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/BUILD
index 48210d2cf..3e6ac6f5b 100644
--- a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/BUILD
+++ b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/BUILD
@@ -248,6 +248,17 @@ java_verifier_test(
     ],
 )
 
+java_verifier_test(
+    name = "rendered_code_tests",
+    size = "small",
+    srcs = ["RenderedCode.java"],
+    resolve_code_facts = True,
+    verifier_opts = [
+        "--ignore_dups",
+        "--convert_marked_source",
+    ],
+)
+
 java_verifier_test(
     name = "methods_tests",
     size = "small",
diff --git a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/MarkedSource.java b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/MarkedSource.java
index 90103ea14..bfe7e6234 100644
--- a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/MarkedSource.java
+++ b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/MarkedSource.java
@@ -65,12 +65,9 @@ public class MarkedSource {
   //- Ctor typed CType
   //- CType param.1 Class
   //- Ctor code CtorTypeCxtId
-  //- CtorTypeCxtId child.0 CtorType
-  //- CtorTypeCxtId child.1 CtorCxt
-  //- CtorTypeCxtId child.2 CtorTok
-  //- CtorTypeCxtId child.3 CtorParams
-  //- CtorType child.0 CtorVoid
-  //- CtorVoid.pre_text "void"
+  //- CtorTypeCxtId child.0 CtorCxt
+  //- CtorTypeCxtId child.1 CtorTok
+  //- CtorTypeCxtId child.2 CtorParams
   //- CtorCxt child.1 CtorCxtClass
   //- CtorCxtClass.pre_text "MarkedSource"
   //- CtorTok.pre_text "MarkedSource"
@@ -230,7 +227,9 @@ public class MarkedSource {
 
   // Ensure documentation is emitted for nodes referenced before their definitions.
   //- @Inner ref InnerClass
-  static Inner refClassBeforeDef() { return null; }
+  static Inner refClassBeforeDef() {
+    return null;
+  }
 
   //- @Inner defines/binding InnerClass
   //- InnerClass code _
@@ -311,51 +310,54 @@ public class MarkedSource {
   //- TAppParams.lookup_index "1"
   static InnerStatic<MarkedSource> staticTApplyVar;
 
-  Object o = new Object() {
-    //- @field defines/binding AnonField
-    //- AnonField childof AnonClass
-    //- AnonClass code AnonId
-    //- AnonId child.0 AnonCxt
-    //- AnonCxt.kind "CONTEXT"
-    //- AnonCxt child.0 PkgToken
-    //- PkgToken.pre_text pkg
-    //- AnonCxt child.1 MksToken
-    //- MksToken.pre_text "MarkedSource"
-    //- AnonId child.1 AnonToken
-    //- AnonToken.kind "IDENTIFIER"
-    //- AnonToken.pre_text "(anon 1)"
-    int field;
-  };
+  Object o =
+      new Object() {
+        //- @field defines/binding AnonField
+        //- AnonField childof AnonClass
+        //- AnonClass code AnonId
+        //- AnonId child.0 AnonCxt
+        //- AnonCxt.kind "CONTEXT"
+        //- AnonCxt child.0 PkgToken
+        //- PkgToken.pre_text pkg
+        //- AnonCxt child.1 MksToken
+        //- MksToken.pre_text "MarkedSource"
+        //- AnonId child.1 AnonToken
+        //- AnonToken.kind "IDENTIFIER"
+        //- AnonToken.pre_text "(anon 1)"
+        int field;
+      };
 
   void methodWithInnerAnon() {
-    Object o = new Object() {
-      //- @field defines/binding IAField
-      //- IAField code CIAField
-      //- CIAField child.1 CIACxt
-      //- CIACxt child.0 CIAPkg
-      //- CIACxt child.1 CIAMs
-      //- CIACxt child.2 CIAMethod
-      //- CIACxt child.3 CIAAnon
-      //- !{CIACxt child.4 _}
-      //- CIAPkg.pre_text "pkg"
-      //- CIAMs.pre_text "MarkedSource"
-      //- CIAMethod.pre_text "methodWithInnerAnon"
-      //- CIAAnon.pre_text "(anon 2)"
-      int field;
-    };
+    Object o =
+        new Object() {
+          //- @field defines/binding IAField
+          //- IAField code CIAField
+          //- CIAField child.1 CIACxt
+          //- CIACxt child.0 CIAPkg
+          //- CIACxt child.1 CIAMs
+          //- CIACxt child.2 CIAMethod
+          //- CIACxt child.3 CIAAnon
+          //- !{CIACxt child.4 _}
+          //- CIAPkg.pre_text "pkg"
+          //- CIAMs.pre_text "MarkedSource"
+          //- CIAMethod.pre_text "methodWithInnerAnon"
+          //- CIAAnon.pre_text "(anon 2)"
+          int field;
+        };
   }
 
   public static class InnerAnon {
-    Object o = new Object() {
-      //- @field defines/binding IIAField
-      //- IIAField code CIIAField
-      //- CIIAField child.1 CIIACxt
-      //- CIIACxt child.2 CIIAInnerCls
-      //- CIIACxt child.3 CIIAInnerAnon
-      //- CIIAInnerCls.pre_text "InnerAnon"
-      //- CIIAInnerAnon.pre_text "(anon 1)"
-      int field;
-    };
+    Object o =
+        new Object() {
+          //- @field defines/binding IIAField
+          //- IIAField code CIIAField
+          //- CIIAField child.1 CIIACxt
+          //- CIIACxt child.2 CIIAInnerCls
+          //- CIIACxt child.3 CIIAInnerAnon
+          //- CIIAInnerCls.pre_text "InnerAnon"
+          //- CIIAInnerAnon.pre_text "(anon 1)"
+          int field;
+        };
   }
 
   //- @func defines/binding Func
@@ -417,7 +419,7 @@ public class MarkedSource {
   //- TVarContext.kind "CONTEXT"
   //- TVarCode child.1 TVarIdent
   //- TVarIdent.kind "IDENTIFIER"
-  //- TVarIdent.pre_text "<T>"
+  //- TVarIdent.pre_text "T"
   static class Generic<T> {}
 
   //- Void code VoidId
diff --git a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/RenderedCode.java b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/RenderedCode.java
new file mode 100644
index 000000000..29dadc7da
--- /dev/null
+++ b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/RenderedCode.java
@@ -0,0 +1,18 @@
+package pkg;
+
+// - @RenderedCode defines/binding RenderedCode
+// - RenderedCode.code/rendered/qualified_name "pkg.RenderedCode"
+public final class RenderedCode {
+  // - @RenderedCode defines/binding Constructor
+  // - Constructor.code/rendered/qualified_name "pkg.RenderedCode.RenderedCode"
+  // - Constructor.code/rendered/signature "RenderedCode()"
+  // - Constructor.code/rendered/callsite_signature "RenderedCode()"
+  private RenderedCode() {}
+
+  // - @Inner defines/binding Inner
+  // - Inner.code/rendered/qualified_name "pkg.RenderedCode.Inner"
+  // - Inner.code/rendered/signature "Inner<T, U>"
+  // - Inner tparam.0 T
+  // - T.code/rendered/qualified_name "pkg.RenderedCode.Inner.T"
+  public static class Inner<T, U> {}
+}
diff --git a/tools/build_rules/verifier_test/java_verifier_test.bzl b/tools/build_rules/verifier_test/java_verifier_test.bzl
index 04b595b81..6dd232799 100644
--- a/tools/build_rules/verifier_test/java_verifier_test.bzl
+++ b/tools/build_rules/verifier_test/java_verifier_test.bzl
@@ -150,6 +150,7 @@ def java_verifier_test(
         timeout = None,
         tags = [],
         extractor = None,
+        resolve_code_facts = False,
         extractor_opts = _default_java_extractor_opts,
         indexer_opts = ["--verbose"],
         verifier_opts = ["--ignore_dups"],
@@ -227,9 +228,12 @@ def java_verifier_test(
         size = size,
         timeout = timeout,
         srcs = goals,
-        opts = verifier_opts,
+        opts = verifier_opts + [
+            "--goal_regex='\\s*//\\s*-(.*)'",
+        ],
         tags = tags,
         visibility = visibility,
+        resolve_code_facts = resolve_code_facts,
         deps = [entries] + [dep + "_entries" for dep in verifier_deps],
     )