Repository: kythe/kythe
Pull request #4861 (2021-03-16 18:05:35)

fix(java): emit "static" facts for classes and methods


Patch:
diff --git a/kythe/java/com/google/devtools/kythe/analyzers/java/KytheTreeScanner.java b/kythe/java/com/google/devtools/kythe/analyzers/java/KytheTreeScanner.java
index 75927fe44..a79b61725 100644
--- a/kythe/java/com/google/devtools/kythe/analyzers/java/KytheTreeScanner.java
+++ b/kythe/java/com/google/devtools/kythe/analyzers/java/KytheTreeScanner.java
@@ -337,6 +337,10 @@ public class KytheTreeScanner extends JCTreeScanner<JavaNode, TreeContext> {
     // initializers. But there's no harm in emitting the same fact twice!
     getScope(ctx).forEach(scope -> entrySets.emitEdge(classNode, EdgeKind.CHILDOF, scope));
 
+    if (classDef.getModifiers().getFlags().contains(Modifier.STATIC)) {
+      emitStatic(classNode);
+    }
+
     NestingKind nestingKind = classDef.sym.getNestingKind();
     if (nestingKind != NestingKind.LOCAL
         && nestingKind != NestingKind.ANONYMOUS
@@ -534,6 +538,9 @@ public class KytheTreeScanner extends JCTreeScanner<JavaNode, TreeContext> {
     VName methodNode =
         entrySets.getNode(signatureGenerator, methodDef.sym, signature.get(), markedSource, null);
     visitAnnotations(methodNode, methodDef.getModifiers().getAnnotations(), ctx);
+    if (methodDef.getModifiers().getFlags().contains(Modifier.STATIC)) {
+      emitStatic(methodNode);
+    }
 
     EntrySet absNode =
         defineTypeParameters(
@@ -730,7 +737,7 @@ public class KytheTreeScanner extends JCTreeScanner<JavaNode, TreeContext> {
     visitAnnotations(varNode, varDef.getModifiers().getAnnotations(), ctx);
 
     if (varDef.getModifiers().getFlags().contains(Modifier.STATIC)) {
-      entrySets.getEmitter().emitFact(varNode, "/kythe/tag/static", "");
+      emitStatic(varNode);
 
       if (varDef.sym.getKind().isField() && owner.getNode().getClassInit().isPresent()) {
         ctx.setNode(new JavaNode(owner.getNode().getClassInit().get()));
@@ -1432,6 +1439,10 @@ public class KytheTreeScanner extends JCTreeScanner<JavaNode, TreeContext> {
     deprecation.ifPresent(d -> entrySets.getEmitter().emitFact(node, "/kythe/tag/deprecated", d));
   }
 
+  private void emitStatic(VName node) {
+    entrySets.getEmitter().emitFact(node, "/kythe/tag/static", "");
+  }
+
   // Unwraps the target EntrySet and emits an edge to it from the sourceNode
   private void emitEdge(EntrySet sourceNode, EdgeKind kind, JavaNode target) {
     entrySets.emitEdge(sourceNode.getVName(), kind, target.getVName());


Test patch:
diff --git a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/Classes.java b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/Classes.java
index 6e463625c..e1d228720 100644
--- a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/Classes.java
+++ b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/Classes.java
@@ -17,7 +17,8 @@ public class Classes {
   //- DefaultCtorType param.1 N
   //- DefaultCtrAnchor defines DefaultCtor
   //- !{ _DefaultCtorBindingAnchor defines/binding DefaultCtor }
-  
+
+  //- @Subclass defines/binding SubclassOne
   private static class Subclass extends Classes {
     //- ImplicitSuperCall ref/call DefaultCtor
     //- ImplicitSuperCall.loc/start @^"{}"
@@ -75,16 +76,24 @@ public class Classes {
   private void localFunc() {
     //- @LocalClass defines/binding LC
     //- LC childof LF
-    class LocalClass {};
+    class LocalClass {}
+    ;
   }
 
   //- ClassInit.node/kind function
   //- ClassInit childof N
   // !{ ClassInit.subkind "constructor" }
-  
+
   static {
     //- @LocalClassInStaticInitializer defines/binding LCISI
     //- LCISI childof ClassInit
-    class LocalClassInStaticInitializer {};
+    class LocalClassInStaticInitializer {}
+    ;
   }
 }
+
+//- SubclassOne.tag/static _
+//- SubclassTwo.tag/static _
+//- SI.tag/static _
+//- E.tag/static _
+//- !{I.tag/static _}
diff --git a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/StaticMethods.java b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/StaticMethods.java
index 7e2d9cafe..c450e0f94 100644
--- a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/StaticMethods.java
+++ b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/StaticMethods.java
@@ -7,19 +7,27 @@ public class StaticMethods {
   private int member;
 
   //- @member defines/binding MemberFunc
-  public static int member() { return 0; }
+  public static int member() {
+    return 0;
+  }
 
   //- @staticMember defines/binding PrivateStaticMember
   private static int staticMember;
 
   //- @staticMember defines/binding ProtectedStaticMemberFunc
-  protected static int staticMember(int x) { return x; }
+  protected static int staticMember(int x) {
+    return x;
+  }
 
   //- @staticMember defines/binding PackageStaticMemberFunc
-  static int staticMember(boolean b) { return b ? 1: 0; }
+  static int staticMember(boolean b) {
+    return b ? 1 : 0;
+  }
 
   //- @staticMember defines/binding StaticMemberFunc
-  public static int staticMember() { return 0; }
+  public static int staticMember() {
+    return 0;
+  }
 
   //- @staticMethod defines/binding StaticBool
   public static void staticMethod(boolean b) {}
@@ -27,3 +35,10 @@ public class StaticMethods {
   //- @staticMethod defines/binding StaticInt
   public static void staticMethod(int b) {}
 }
+
+//- MemberFunc.tag/static _
+//- ProtectedStaticMemberFunc.tag/static _
+//- PackageStaticMemberFunc.tag/static _
+//- StaticMemberFunc.tag/static _
+//- StaticBool.tag/static _
+//- StaticInt.tag/static _
diff --git a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/Variables.java b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/Variables.java
index 74c7265e5..1fcdb5a3e 100644
--- a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/Variables.java
+++ b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/Variables.java
@@ -70,3 +70,6 @@ public class Variables {
     int localInStaticInitializer = 0;
   }
 }
+
+//- !{V.tag/static _}
+//- SF.tag/static _