From c0559170861933b2896642167e5773b19095a369 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Thu, 15 Aug 2024 15:25:30 +0100 Subject: [PATCH 1/9] investigation --- .../SemanticdbTaskListener.java | 1 + .../semanticdb_javac/SemanticdbVisitor.java | 9 ++++ .../src/main/java/minimized/Interfaces.java | 12 +++++ .../src/main/java/minimized/Interfaces.java | 46 +++++++++++++------ 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java index f2594c76f..969d0679a 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java @@ -93,6 +93,7 @@ private void onFinishedAnalyze(TaskEvent e) { Result path = semanticdbOutputPath(options, e); if (path != null) { if (path.isOk()) { + System.out.println("Triggering for " + path.getOrThrow() + e.toString()); Semanticdb.TextDocument textDocument = new SemanticdbVisitor(globals, e.getCompilationUnit(), options, types, trees, elements) .buildTextDocument(e.getCompilationUnit()); diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java index cebd32f95..bd872b037 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java @@ -93,6 +93,9 @@ public Semanticdb.TextDocument buildTextDocument(CompilationUnitTree tree) { resolveNodes(); + System.out.println( + "Finally: " + symbolInfos.stream().map(e -> e.getSymbol()).collect(Collectors.toList())); + return Semanticdb.TextDocument.newBuilder() .setSchema(Semanticdb.Schema.SEMANTICDB4) .setLanguage(Semanticdb.Language.JAVA) @@ -185,6 +188,8 @@ private void emitSymbolInformation(Element sym, Tree tree) { Semanticdb.SymbolInformation info = builder.build(); + // System.out.println(info.toString()); + symbolInfos.add(info); } @@ -223,6 +228,10 @@ void resolveNodes() { resolveMemberSelectTree((MemberSelectTree) node, entry.getValue()); } else if (node instanceof NewClassTree) { resolveNewClassTree((NewClassTree) node, entry.getValue()); + } else { + // System.out.println(node.getClass()); + + // System.out.println(entry.getValue().getLeaf()); } } } diff --git a/tests/minimized/src/main/java/minimized/Interfaces.java b/tests/minimized/src/main/java/minimized/Interfaces.java index c06faea6e..370db5d14 100644 --- a/tests/minimized/src/main/java/minimized/Interfaces.java +++ b/tests/minimized/src/main/java/minimized/Interfaces.java @@ -9,3 +9,15 @@ default String defaultInterfaceMethod() { return "default"; } } + +interface BookService { + void checkPages(); +} + +interface MyService { + BookService bookService(); + + default void example() { + bookService().checkPages(); + } +} diff --git a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/Interfaces.java b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/Interfaces.java index ccab9b4ea..eb57c174b 100644 --- a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/Interfaces.java +++ b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/Interfaces.java @@ -6,25 +6,41 @@ public interface Interfaces { // signature_documentation java public interface Interfaces // kind Interface static void staticInterfaceMethod() {} -// ^^^^^^^^^^^^^^^^^^^^^ definition semanticdb maven . . minimized/Interfaces#staticInterfaceMethod(). -// display_name staticInterfaceMethod -// signature_documentation java public static void staticInterfaceMethod() -// kind StaticMethod String abstractInterfaceMethod(); -//^^^^^^ reference semanticdb maven jdk 11 java/lang/String# -// ^^^^^^^^^^^^^^^^^^^^^^^ definition semanticdb maven . . minimized/Interfaces#abstractInterfaceMethod(). -// display_name abstractInterfaceMethod -// signature_documentation java public abstract String abstractInterfaceMethod() -// kind AbstractMethod -// relationship is_reference is_implementation semanticdb maven . . minimized/SubClasses#abstractInterfaceMethod(). default String defaultInterfaceMethod() { -// ^^^^^^ reference semanticdb maven jdk 11 java/lang/String# -// ^^^^^^^^^^^^^^^^^^^^^^ definition semanticdb maven . . minimized/Interfaces#defaultInterfaceMethod(). -// display_name defaultInterfaceMethod -// signature_documentation java public default String defaultInterfaceMethod() -// kind Method return "default"; } } + +interface BookService { +// ^^^^^^^^^^^ definition semanticdb maven . . minimized/BookService# +// display_name BookService +// signature_documentation java interface BookService +// kind Interface + void checkPages(); +} + +interface MyService { +// ^^^^^^^^^ definition semanticdb maven . . minimized/MyService# +// display_name MyService +// signature_documentation java interface MyService +// kind Interface + BookService bookService(); +//^^^^^^^^^^^ reference semanticdb maven . . minimized/BookService# +// ^^^^^^^^^^^ definition semanticdb maven . . minimized/MyService#bookService(). +// display_name bookService +// signature_documentation java public abstract BookService bookService() +// kind AbstractMethod + + default void example() { +// ^^^^^^^ definition semanticdb maven . . minimized/MyService#example(). +// display_name example +// signature_documentation java public default void example() +// kind Method + bookService().checkPages(); +// ^^^^^^^^^^^ reference semanticdb maven . . minimized/MyService#bookService(). +// ^^^^^^^^^^ reference semanticdb maven . . minimized/BookService#checkPages(). + } +} From c02e4d400e83394934b61d8aa7aeae23f35beb54 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Mon, 19 Aug 2024 16:33:38 +0100 Subject: [PATCH 2/9] WIP: fix by appending, not overwriting --- build.sbt | 1 + .../SemanticdbTaskListener.java | 78 +++++++++++++++---- .../semanticdb_javac/SemanticdbVisitor.java | 6 +- .../java/minimized/AnnotationParameters.java | 13 ++++ .../AnnotationsOnParameterizedTypes.java | 72 ++++++++++++++++- .../src/main/java/minimized/Interfaces.java | 19 +++++ .../main/java/minimized/LombokBuilder.java | 34 -------- .../MinimizedSnapshotScipGenerator.scala | 3 + 8 files changed, 171 insertions(+), 55 deletions(-) diff --git a/build.sbt b/build.sbt index 7549aee0e..eaa153c82 100644 --- a/build.sbt +++ b/build.sbt @@ -545,6 +545,7 @@ lazy val docs = project .enablePlugins(DocusaurusPlugin) lazy val javaOnlySettings = List[Def.Setting[_]]( + javafmtOnCompile := false, autoScalaLibrary := false, incOptions ~= { old => old.withEnabled(false).withApiDebug(true) diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java index 969d0679a..8f3cec3c4 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java @@ -8,13 +8,14 @@ import javax.lang.model.util.Types; import javax.tools.JavaFileObject; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintWriter; +import java.io.*; import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; /** * Callback hook that generates SemanticDB when the compiler has completed typechecking a Java @@ -48,6 +49,7 @@ public void started(TaskEvent e) {} @Override public void finished(TaskEvent e) { + // System.out.println("Finishing " + e.getKind() + " on " + e.getSourceFile()); if (e.getKind() != TaskEvent.Kind.ANALYZE) return; if (!options.errors.isEmpty()) { if (!options.alreadyReportedErrors) { @@ -78,8 +80,10 @@ public void finished(TaskEvent e) { } } - // Uses reporter.error with the full stack trace of the exception instead of reporter.exception - // because reporter.exception doesn't seem to print any meaningful information about the + // Uses reporter.error with the full stack trace of the exception instead of + // reporter.exception + // because reporter.exception doesn't seem to print any meaningful information + // about the // exception, it just prints the location with an empty message. private void reportException(Throwable exception, TaskEvent e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -93,7 +97,7 @@ private void onFinishedAnalyze(TaskEvent e) { Result path = semanticdbOutputPath(options, e); if (path != null) { if (path.isOk()) { - System.out.println("Triggering for " + path.getOrThrow() + e.toString()); + // System.out.println("Triggering for " + path.getOrThrow() + e.toString()); Semanticdb.TextDocument textDocument = new SemanticdbVisitor(globals, e.getCompilationUnit(), options, types, trees, elements) .buildTextDocument(e.getCompilationUnit()); @@ -105,13 +109,54 @@ private void onFinishedAnalyze(TaskEvent e) { } private void writeSemanticdb(TaskEvent event, Path output, Semanticdb.TextDocument textDocument) { - try { + if (output.toFile().exists()) { + // APPEND + Semanticdb.TextDocuments documents = null; + try (InputStream is = Files.newInputStream(output.toFile().toPath())) { + + documents = Semanticdb.TextDocuments.parseFrom(is); + + } catch (IOException e) { + this.reportException(e, event); + System.out.println(e); + return; + } + + Semanticdb.TextDocument document = documents.getDocuments(0); + + Set symbols = new HashSet<>(document.getSymbolsList()); + Set occurrences = new HashSet<>(document.getOccurrencesList()); + + symbols.addAll(textDocument.getSymbolsList()); + occurrences.addAll(textDocument.getOccurrencesList()); + + Semanticdb.TextDocument finalDocument = + document + .toBuilder() + .clearOccurrences() + .addAllOccurrences(occurrences) + .clearSymbols() + .addAllSymbols(symbols) + .build(); + byte[] bytes = - Semanticdb.TextDocuments.newBuilder().addDocuments(textDocument).build().toByteArray(); - Files.createDirectories(output.getParent()); - Files.write(output, bytes); - } catch (IOException e) { - this.reportException(e, event); + Semanticdb.TextDocuments.newBuilder().addDocuments(finalDocument).build().toByteArray(); + + try { + Files.createDirectories(output.getParent()); + Files.write(output, bytes); + } catch (IOException e) { + this.reportException(e, event); + } + } else { // OVERWRITE + try { + byte[] bytes = + Semanticdb.TextDocuments.newBuilder().addDocuments(textDocument).build().toByteArray(); + Files.createDirectories(output.getParent()); + Files.write(output, bytes); + } catch (IOException e) { + this.reportException(e, event); + } } } @@ -211,10 +256,13 @@ private Result semanticdbOutputPath(SemanticdbJavacOptions options switch (options.noRelativePath) { case INDEX_ANYWAY: - // Come up with a unique relative path for this file even if it's not under the sourceroot. - // By indexing auto-generated files, we collect SymbolInformation for auto-generated symbol, + // Come up with a unique relative path for this file even if it's not under the + // sourceroot. + // By indexing auto-generated files, we collect SymbolInformation for + // auto-generated symbol, // which results in more useful hover tooltips in the editor. - // In the future, we may want to additionally embed the full text contents of these files + // In the future, we may want to additionally embed the full text contents of + // these files // so that it's possible to browse generated files with precise code navigation. String uniqueFilename = String.format("%d.%s.semanticdb", ++noRelativePathCounter, absolutePath.getFileName()); diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java index bd872b037..0657b0f6c 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java @@ -93,8 +93,10 @@ public Semanticdb.TextDocument buildTextDocument(CompilationUnitTree tree) { resolveNodes(); - System.out.println( - "Finally: " + symbolInfos.stream().map(e -> e.getSymbol()).collect(Collectors.toList())); + // System.out.println( + // "Finally: " + // + tree.getKind() + // + symbolInfos.stream().map(e -> e.getSymbol()).collect(Collectors.toList())); return Semanticdb.TextDocument.newBuilder() .setSchema(Semanticdb.Schema.SEMANTICDB4) diff --git a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java index 7704629ca..d2c068fae 100644 --- a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java +++ b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java @@ -7,6 +7,10 @@ // kind Interface // relationship is_implementation semanticdb maven jdk 11 java/lang/annotation/Annotation# →double value(); +// ^^^^^ definition semanticdb maven . . minimized/Bar#value(). +// display_name value +// signature_documentation java public abstract double value() +// kind AbstractMethod } @interface BarB { @@ -16,6 +20,10 @@ // kind Interface // relationship is_implementation semanticdb maven jdk 11 java/lang/annotation/Annotation# →boolean value(); +// ^^^^^ definition semanticdb maven . . minimized/BarB#value(). +// display_name value +// signature_documentation java public abstract boolean value() +// kind AbstractMethod } @interface Nullable { @@ -25,6 +33,11 @@ // kind Interface // relationship is_implementation semanticdb maven jdk 11 java/lang/annotation/Annotation# →String value() default ""; +//^^^^^ reference semanticdb maven jdk 11 java/lang/String# +// ^^^^^ definition semanticdb maven . . minimized/Nullable#value(). +// display_name value +// signature_documentation java public abstract String value() +// kind AbstractMethod } interface Foo { diff --git a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationsOnParameterizedTypes.java b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationsOnParameterizedTypes.java index 40c1222e0..3f00e3cd3 100644 --- a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationsOnParameterizedTypes.java +++ b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationsOnParameterizedTypes.java @@ -33,10 +33,45 @@ public interface AnnotationsOnParameterizedTypes { // kind Interface public static AnnotationsOnParameterizedTypes getInstance() { +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypes# +// ^^^^^^^^^^^ definition semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#getInstance(). +// display_name getInstance +// signature_documentation java public static AnnotationsOnParameterizedTypes getInstance() +// kind StaticMethod return new AnnotationsOnParameterizedTypesImpl(); +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#``(). } Function adapter(Class contract, Class wrappedClass); +// ^ definition semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter().[C] +// display_name C +// signature_documentation java C +// kind TypeParameter +// ^ definition semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter().[W] +// display_name W +// signature_documentation java W +// kind TypeParameter +// ^^^^^^^^ reference semanticdb maven jdk 11 java/util/function/Function# +// ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter().[W] +// ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter().[C] +// ^^^^^^^ definition semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter(). +// display_name adapter +// signature_documentation java public abstract Function adapter(Class contract, Class wrappedClass) +// kind AbstractMethod +// relationship is_reference is_implementation semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). +// relationship is_reference is_implementation semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). +// ^^^^^ reference semanticdb maven jdk 11 java/lang/Class# +// ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter().[C] +// ^^^^^^^^ definition local 0 +// display_name contract +// signature_documentation java Class contract +// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter(). +// ^^^^^ reference semanticdb maven jdk 11 java/lang/Class# +// ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter().[W] +// ^^^^^^^^^^^^ definition local 1 +// display_name wrappedClass +// signature_documentation java Class wrappedClass +// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter(). } @@ -77,7 +112,7 @@ public Function adapter(Class contract, Class wrappedClass) { // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter().[C] // ^^^^^^^ definition semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). // display_name adapter -// signature_documentation java @Override\npublic Function adapter(Class contract, Class wrappedClass) +// signature_documentation java @Override\npublic Function adapter(Class contract, Class contract) // kind Method // relationship is_reference is_implementation semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter(). // ^^^^^ reference semanticdb maven jdk 11 java/lang/Class# @@ -85,31 +120,45 @@ public Function adapter(Class contract, Class wrappedClass) { // ^^^^^^^^ definition local 0 // display_name contract // signature_documentation java Class contract +// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter(). +// ^^^^^^^^ definition local 2 +// display_name contract +// signature_documentation java Class contract // enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). // ^^^^^ reference semanticdb maven jdk 11 java/lang/Class# // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter().[W] // ^^^^^^^^^^^^ definition local 1 // display_name wrappedClass // signature_documentation java Class wrappedClass -// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). +// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter(). +// ^^^^^^^^^^^^ definition local 3 +// display_name contract +// signature_documentation java Class contract +// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). Function constructor = getConstructor(contract); // ^^^^^^^^ reference semanticdb maven jdk 11 java/util/function/Function# // ^^^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/reflect/InvocationHandler# // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter().[C] // ^^^^^^^^^^^ definition local 2 -// display_name constructor -// signature_documentation java Function constructor +// display_name contract +// signature_documentation java Class contract // enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). +// ^^^^^^^^^^^ definition local 4 +// display_name constructor +// signature_documentation java @SuppressWarnings("unchecked")\nConstructor constructor +// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). // kind Variable // ^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). // ^^^^^^^^ reference local 0 +// ^^^^^^^^ reference local 2 System.out.println(constructor); // ^^^^^^ reference semanticdb maven jdk 11 java/lang/System# // ^^^ reference semanticdb maven jdk 11 java/lang/System#out. // ^^^^^^^ reference semanticdb maven jdk 11 java/io/PrintStream#println(+9). // ^^^^^^^^^^^ reference local 2 +// ^^^^^^^^^^^ reference local 4 return null; } @@ -131,6 +180,10 @@ private Function getConstructor(Class contract) { // ^^^^^^^^ definition local 3 // display_name contract // signature_documentation java Class contract +// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). +// ^^^^^^^^ definition local 5 +// display_name contract +// signature_documentation java Class contract // enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). @SuppressWarnings("unchecked") // ^^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# @@ -142,12 +195,22 @@ private Function getConstructor(Class contract) { // signature_documentation java @SuppressWarnings("unchecked")\nConstructor constructor // enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). // kind Variable +// ^^^^^^^^^^^ definition local 6 +// display_name constructor +// signature_documentation java @SuppressWarnings\nConstructor constructor +// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). +// kind Variable // ^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/reflect/Constructor# // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor().[T] // ^^^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#proxyConstructors. // ^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/util/concurrent/ConcurrentMap#computeIfAbsent(). // ^^^^^^^^ reference local 3 +// ^^^^^^^^ reference local 5 // ^ definition local 5 +// display_name contract +// signature_documentation java Class contract +// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). +// ^ definition local 7 // display_name c // signature_documentation java Class c // enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). @@ -159,6 +222,7 @@ private Function getConstructor(Class contract) { // ^^^ reference semanticdb maven jdk 11 java/lang/System#out. // ^^^^^^^ reference semanticdb maven jdk 11 java/io/PrintStream#println(+9). // ^^^^^^^^^^^ reference local 4 +// ^^^^^^^^^^^ reference local 6 return null; } diff --git a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/Interfaces.java b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/Interfaces.java index eb57c174b..4ad676630 100644 --- a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/Interfaces.java +++ b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/Interfaces.java @@ -6,10 +6,25 @@ public interface Interfaces { // signature_documentation java public interface Interfaces // kind Interface static void staticInterfaceMethod() {} +// ^^^^^^^^^^^^^^^^^^^^^ definition semanticdb maven . . minimized/Interfaces#staticInterfaceMethod(). +// display_name staticInterfaceMethod +// signature_documentation java public static void staticInterfaceMethod() +// kind StaticMethod String abstractInterfaceMethod(); +//^^^^^^ reference semanticdb maven jdk 11 java/lang/String# +// ^^^^^^^^^^^^^^^^^^^^^^^ definition semanticdb maven . . minimized/Interfaces#abstractInterfaceMethod(). +// display_name abstractInterfaceMethod +// signature_documentation java public abstract String abstractInterfaceMethod() +// kind AbstractMethod +// relationship is_reference is_implementation semanticdb maven . . minimized/SubClasses#abstractInterfaceMethod(). default String defaultInterfaceMethod() { +// ^^^^^^ reference semanticdb maven jdk 11 java/lang/String# +// ^^^^^^^^^^^^^^^^^^^^^^ definition semanticdb maven . . minimized/Interfaces#defaultInterfaceMethod(). +// display_name defaultInterfaceMethod +// signature_documentation java public default String defaultInterfaceMethod() +// kind Method return "default"; } } @@ -20,6 +35,10 @@ interface BookService { // signature_documentation java interface BookService // kind Interface void checkPages(); +// ^^^^^^^^^^ definition semanticdb maven . . minimized/BookService#checkPages(). +// display_name checkPages +// signature_documentation java public abstract void checkPages() +// kind AbstractMethod } interface MyService { diff --git a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/LombokBuilder.java b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/LombokBuilder.java index 01161039a..98bf2e2ef 100644 --- a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/LombokBuilder.java +++ b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/LombokBuilder.java @@ -4,48 +4,14 @@ //^^^^^^^^^^^^^^^ reference local 0 //^^^^^^^^^^^^^^^ reference local 1 //^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ //^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ -//^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello# -//^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello# //^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello# //^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#HelloBuilder# -//^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#HelloBuilder# -//^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#HelloBuilder#message. -//^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#HelloBuilder#message. //^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#HelloBuilder#message. //^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#message. //^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/Override# //^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/String# -//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/String# -//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/String# -//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/String# //^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# -//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# -//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# -//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# -//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# -//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# -//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# -//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# -//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings#value(). //^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings#value(). //^^^^^ reference semanticdb maven . . lombok/ // ^^^^^^^ reference semanticdb maven . . lombok/Builder# diff --git a/tests/snapshots/src/main/scala/tests/MinimizedSnapshotScipGenerator.scala b/tests/snapshots/src/main/scala/tests/MinimizedSnapshotScipGenerator.scala index 9c8150b8b..64863e198 100644 --- a/tests/snapshots/src/main/scala/tests/MinimizedSnapshotScipGenerator.scala +++ b/tests/snapshots/src/main/scala/tests/MinimizedSnapshotScipGenerator.scala @@ -70,13 +70,16 @@ class MinimizedSnapshotScipGenerator extends SnapshotGenerator { context, expectOutput, () => { + println(document.getRelativePath) val uri = URI.create( List( index.getMetadata.getProjectRoot.stripSuffix("/"), document.getRelativePath ).mkString("/") ) + val absolutePath = AbsolutePath(Paths.get(uri)) + //println(absolutePath) val text = FileIO.slurp(absolutePath, StandardCharsets.UTF_8) ScipPrinters.printTextDocument(document, text) } From bf6995ed82479837d22343408262f65c88392d83 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 20 Aug 2024 09:37:53 +0100 Subject: [PATCH 3/9] Clean up semanticdb file upon entering a particular source file --- .../semanticdb_javac/SemanticdbTaskListener.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java index 8f3cec3c4..4c27843a5 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java @@ -45,7 +45,21 @@ public SemanticdbTaskListener( } @Override - public void started(TaskEvent e) {} + public void started(TaskEvent e) { + // Upon first encounter with a file (before any other tasks are run) + // we remove the semanticdb file for this source file to ensure + // stale data doesn't cause problems + if(e.getKind() == TaskEvent.Kind.ENTER) { + Result semanticdbPath = semanticdbOutputPath(options, e); + if(semanticdbPath.isOk()) { + try { + Files.deleteIfExists(semanticdbPath.getOrThrow()); + } catch (IOException ex) { + this.reportException(ex, e); + } + } + } + } @Override public void finished(TaskEvent e) { From 7a6b6557da68f78b36cc795d71b17947050ed89e Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 20 Aug 2024 09:38:21 +0100 Subject: [PATCH 4/9] Make document appender more robust --- .../SemanticdbTaskListener.java | 61 +++++++++++++------ .../MinimizedSnapshotScipGenerator.scala | 2 - 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java index 4c27843a5..c83eee37b 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java @@ -111,7 +111,6 @@ private void onFinishedAnalyze(TaskEvent e) { Result path = semanticdbOutputPath(options, e); if (path != null) { if (path.isOk()) { - // System.out.println("Triggering for " + path.getOrThrow() + e.toString()); Semanticdb.TextDocument textDocument = new SemanticdbVisitor(globals, e.getCompilationUnit(), options, types, trees, elements) .buildTextDocument(e.getCompilationUnit()); @@ -123,38 +122,60 @@ private void onFinishedAnalyze(TaskEvent e) { } private void writeSemanticdb(TaskEvent event, Path output, Semanticdb.TextDocument textDocument) { - if (output.toFile().exists()) { - // APPEND + if (Files.exists(output)) { + /* + * If there already is a semanticdb file at the given path, + * we do the following: + * - Read a documents collection + * - Try to find the document with the matching relative path (matching the incoming textDocument) + * - Then, depending on whether a matching document already exists in the collection: + * - if YES, mutate it in place to only add entries from the incoming document + * - if NO, simply add the incoming text document to the collection + * - Write the collection back to disk + * */ + Semanticdb.TextDocument document = null; + int documentIndex = -1; Semanticdb.TextDocuments documents = null; - try (InputStream is = Files.newInputStream(output.toFile().toPath())) { + try (InputStream is = Files.newInputStream(output.toFile().toPath())) { documents = Semanticdb.TextDocuments.parseFrom(is); + for (int i = 0; i < documents.getDocumentsCount(); i++) { + Semanticdb.TextDocument candidate = documents.getDocuments(i); + if (document == null && candidate.getUri().equals(textDocument.getUri())) { + document = candidate; + documentIndex = i; + } + } + } catch (IOException e) { this.reportException(e, event); - System.out.println(e); return; } - Semanticdb.TextDocument document = documents.getDocuments(0); + if (document != null) { + // If there is a previous semanticdb document at this path, we need + // to deduplicate symbols and occurrences and mutate the document in place + Set symbols = new HashSet<>(textDocument.getSymbolsList()); + Set occurrences = + new HashSet<>(textDocument.getOccurrencesList()); + Set synthetics = new HashSet<>(textDocument.getSyntheticsList()); - Set symbols = new HashSet<>(document.getSymbolsList()); - Set occurrences = new HashSet<>(document.getOccurrencesList()); + symbols.addAll(document.getSymbolsList()); + occurrences.addAll(document.getOccurrencesList()); + synthetics.addAll(document.getSyntheticsList()); - symbols.addAll(textDocument.getSymbolsList()); - occurrences.addAll(textDocument.getOccurrencesList()); + documents.toBuilder().addDocuments( + documentIndex, + document.toBuilder().clearOccurrences().addAllOccurrences(occurrences).clearSymbols().addAllSymbols(symbols).clearSynthetics().addAllSynthetics(synthetics) + ); - Semanticdb.TextDocument finalDocument = - document - .toBuilder() - .clearOccurrences() - .addAllOccurrences(occurrences) - .clearSymbols() - .addAllSymbols(symbols) - .build(); + } else { + // If no prior document was found, we can just add the incoming one to the collection + documents = documents.toBuilder().addDocuments(textDocument).build(); + } - byte[] bytes = - Semanticdb.TextDocuments.newBuilder().addDocuments(finalDocument).build().toByteArray(); + byte[] bytes = documents.toByteArray(); try { Files.createDirectories(output.getParent()); diff --git a/tests/snapshots/src/main/scala/tests/MinimizedSnapshotScipGenerator.scala b/tests/snapshots/src/main/scala/tests/MinimizedSnapshotScipGenerator.scala index 64863e198..b608943da 100644 --- a/tests/snapshots/src/main/scala/tests/MinimizedSnapshotScipGenerator.scala +++ b/tests/snapshots/src/main/scala/tests/MinimizedSnapshotScipGenerator.scala @@ -70,7 +70,6 @@ class MinimizedSnapshotScipGenerator extends SnapshotGenerator { context, expectOutput, () => { - println(document.getRelativePath) val uri = URI.create( List( index.getMetadata.getProjectRoot.stripSuffix("/"), @@ -79,7 +78,6 @@ class MinimizedSnapshotScipGenerator extends SnapshotGenerator { ) val absolutePath = AbsolutePath(Paths.get(uri)) - //println(absolutePath) val text = FileIO.slurp(absolutePath, StandardCharsets.UTF_8) ScipPrinters.printTextDocument(document, text) } From 82df232d477a1720bda8cabc257eb79f4600bdc4 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 20 Aug 2024 09:38:35 +0100 Subject: [PATCH 5/9] Rerun snapshots --- .../AnnotationsOnParameterizedTypes.java | 40 +++---------------- .../main/java/minimized/LombokBuilder.java | 34 ++++++++++++++++ 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationsOnParameterizedTypes.java b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationsOnParameterizedTypes.java index 3f00e3cd3..e50392d38 100644 --- a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationsOnParameterizedTypes.java +++ b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationsOnParameterizedTypes.java @@ -59,7 +59,6 @@ public static AnnotationsOnParameterizedTypes getInstance() { // signature_documentation java public abstract Function adapter(Class contract, Class wrappedClass) // kind AbstractMethod // relationship is_reference is_implementation semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). -// relationship is_reference is_implementation semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). // ^^^^^ reference semanticdb maven jdk 11 java/lang/Class# // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter().[C] // ^^^^^^^^ definition local 0 @@ -112,52 +111,38 @@ public Function adapter(Class contract, Class wrappedClass) { // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter().[C] // ^^^^^^^ definition semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). // display_name adapter -// signature_documentation java @Override\npublic Function adapter(Class contract, Class contract) +// signature_documentation java @Override\npublic Function adapter(Class contract, Class wrappedClass) // kind Method // relationship is_reference is_implementation semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter(). // ^^^^^ reference semanticdb maven jdk 11 java/lang/Class# // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter().[C] -// ^^^^^^^^ definition local 0 -// display_name contract -// signature_documentation java Class contract -// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter(). // ^^^^^^^^ definition local 2 // display_name contract // signature_documentation java Class contract // enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). // ^^^^^ reference semanticdb maven jdk 11 java/lang/Class# // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter().[W] -// ^^^^^^^^^^^^ definition local 1 +// ^^^^^^^^^^^^ definition local 3 // display_name wrappedClass // signature_documentation java Class wrappedClass -// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypes#adapter(). -// ^^^^^^^^^^^^ definition local 3 -// display_name contract -// signature_documentation java Class contract -// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). +// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). Function constructor = getConstructor(contract); // ^^^^^^^^ reference semanticdb maven jdk 11 java/util/function/Function# // ^^^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/reflect/InvocationHandler# // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter().[C] -// ^^^^^^^^^^^ definition local 2 -// display_name contract -// signature_documentation java Class contract -// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). // ^^^^^^^^^^^ definition local 4 // display_name constructor -// signature_documentation java @SuppressWarnings("unchecked")\nConstructor constructor -// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). +// signature_documentation java Function constructor +// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#adapter(). // kind Variable // ^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). -// ^^^^^^^^ reference local 0 // ^^^^^^^^ reference local 2 System.out.println(constructor); // ^^^^^^ reference semanticdb maven jdk 11 java/lang/System# // ^^^ reference semanticdb maven jdk 11 java/lang/System#out. // ^^^^^^^ reference semanticdb maven jdk 11 java/io/PrintStream#println(+9). -// ^^^^^^^^^^^ reference local 2 // ^^^^^^^^^^^ reference local 4 return null; @@ -177,10 +162,6 @@ private Function getConstructor(Class contract) { // kind Method // ^^^^^ reference semanticdb maven jdk 11 java/lang/Class# // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor().[T] -// ^^^^^^^^ definition local 3 -// display_name contract -// signature_documentation java Class contract -// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). // ^^^^^^^^ definition local 5 // display_name contract // signature_documentation java Class contract @@ -190,11 +171,6 @@ private Function getConstructor(Class contract) { Constructor constructor = (Constructor) proxyConstructors.computeIfAbsent(contract, c -> { // ^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/reflect/Constructor# // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor().[T] -// ^^^^^^^^^^^ definition local 4 -// display_name constructor -// signature_documentation java @SuppressWarnings("unchecked")\nConstructor constructor -// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). -// kind Variable // ^^^^^^^^^^^ definition local 6 // display_name constructor // signature_documentation java @SuppressWarnings\nConstructor constructor @@ -204,12 +180,7 @@ private Function getConstructor(Class contract) { // ^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor().[T] // ^^^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#proxyConstructors. // ^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/util/concurrent/ConcurrentMap#computeIfAbsent(). -// ^^^^^^^^ reference local 3 // ^^^^^^^^ reference local 5 -// ^ definition local 5 -// display_name contract -// signature_documentation java Class contract -// enclosing_symbol semanticdb maven . . minimized/AnnotationsOnParameterizedTypesImpl#getConstructor(). // ^ definition local 7 // display_name c // signature_documentation java Class c @@ -221,7 +192,6 @@ private Function getConstructor(Class contract) { // ^^^^^^ reference semanticdb maven jdk 11 java/lang/System# // ^^^ reference semanticdb maven jdk 11 java/lang/System#out. // ^^^^^^^ reference semanticdb maven jdk 11 java/io/PrintStream#println(+9). -// ^^^^^^^^^^^ reference local 4 // ^^^^^^^^^^^ reference local 6 return null; diff --git a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/LombokBuilder.java b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/LombokBuilder.java index 98bf2e2ef..01161039a 100644 --- a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/LombokBuilder.java +++ b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/LombokBuilder.java @@ -4,14 +4,48 @@ //^^^^^^^^^^^^^^^ reference local 0 //^^^^^^^^^^^^^^^ reference local 1 //^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ //^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . java/lang/ +//^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello# +//^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello# //^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello# //^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#HelloBuilder# +//^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#HelloBuilder# +//^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#HelloBuilder#message. +//^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#HelloBuilder#message. //^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#HelloBuilder#message. //^^^^^^^^^^^^^^^ reference semanticdb maven . . minimized/Hello#message. //^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/Override# //^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/String# +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/String# +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/String# +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/String# //^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings#value(). //^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings#value(). //^^^^^ reference semanticdb maven . . lombok/ // ^^^^^^^ reference semanticdb maven . . lombok/Builder# From 956383a05c26839f0d754ee28bb45c90d1a1091a Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 20 Aug 2024 09:43:45 +0100 Subject: [PATCH 6/9] cleanup --- .../SemanticdbTaskListener.java | 29 ++++++++++++------- .../semanticdb_javac/SemanticdbVisitor.java | 11 ------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java index c83eee37b..9a822a2b9 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java @@ -49,21 +49,20 @@ public void started(TaskEvent e) { // Upon first encounter with a file (before any other tasks are run) // we remove the semanticdb file for this source file to ensure // stale data doesn't cause problems - if(e.getKind() == TaskEvent.Kind.ENTER) { + if (e.getKind() == TaskEvent.Kind.ENTER) { Result semanticdbPath = semanticdbOutputPath(options, e); - if(semanticdbPath.isOk()) { - try { - Files.deleteIfExists(semanticdbPath.getOrThrow()); - } catch (IOException ex) { - this.reportException(ex, e); - } + if (semanticdbPath.isOk()) { + try { + Files.deleteIfExists(semanticdbPath.getOrThrow()); + } catch (IOException ex) { + this.reportException(ex, e); + } } } } @Override public void finished(TaskEvent e) { - // System.out.println("Finishing " + e.getKind() + " on " + e.getSourceFile()); if (e.getKind() != TaskEvent.Kind.ANALYZE) return; if (!options.errors.isEmpty()) { if (!options.alreadyReportedErrors) { @@ -165,10 +164,18 @@ private void writeSemanticdb(TaskEvent event, Path output, Semanticdb.TextDocume occurrences.addAll(document.getOccurrencesList()); synthetics.addAll(document.getSyntheticsList()); - documents.toBuilder().addDocuments( + documents + .toBuilder() + .addDocuments( documentIndex, - document.toBuilder().clearOccurrences().addAllOccurrences(occurrences).clearSymbols().addAllSymbols(symbols).clearSynthetics().addAllSynthetics(synthetics) - ); + document + .toBuilder() + .clearOccurrences() + .addAllOccurrences(occurrences) + .clearSymbols() + .addAllSymbols(symbols) + .clearSynthetics() + .addAllSynthetics(synthetics)); } else { // If no prior document was found, we can just add the incoming one to the collection diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java index 0657b0f6c..cebd32f95 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java @@ -93,11 +93,6 @@ public Semanticdb.TextDocument buildTextDocument(CompilationUnitTree tree) { resolveNodes(); - // System.out.println( - // "Finally: " - // + tree.getKind() - // + symbolInfos.stream().map(e -> e.getSymbol()).collect(Collectors.toList())); - return Semanticdb.TextDocument.newBuilder() .setSchema(Semanticdb.Schema.SEMANTICDB4) .setLanguage(Semanticdb.Language.JAVA) @@ -190,8 +185,6 @@ private void emitSymbolInformation(Element sym, Tree tree) { Semanticdb.SymbolInformation info = builder.build(); - // System.out.println(info.toString()); - symbolInfos.add(info); } @@ -230,10 +223,6 @@ void resolveNodes() { resolveMemberSelectTree((MemberSelectTree) node, entry.getValue()); } else if (node instanceof NewClassTree) { resolveNewClassTree((NewClassTree) node, entry.getValue()); - } else { - // System.out.println(node.getClass()); - - // System.out.println(entry.getValue().getLeaf()); } } } From 94e8cb8eca6c0308234729dd089c87e184551ad4 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 20 Aug 2024 10:07:20 +0100 Subject: [PATCH 7/9] Protect against NPE in path absolutisation --- .../sourcegraph/semanticdb_javac/SemanticdbTaskListener.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java index 9a822a2b9..46ad5eabf 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java @@ -283,6 +283,8 @@ private void inferBazelSourceroot(JavaFileObject file) { private Result semanticdbOutputPath(SemanticdbJavacOptions options, TaskEvent e) { Path absolutePath = absolutePathFromUri(options, e.getSourceFile()); + if (absolutePath == null) + return Result.error("Failed to produce absolute path for " + e.getSourceFile()); if (absolutePath.startsWith(options.sourceroot)) { Path relativePath = options.sourceroot.relativize(absolutePath); String filename = relativePath.getFileName().toString() + ".semanticdb"; From 69699b6ce4b61087c8401db1886cde2171bff4c3 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 20 Aug 2024 10:15:00 +0100 Subject: [PATCH 8/9] Call inferBazelSourceroot defensively --- .../sourcegraph/semanticdb_javac/SemanticdbTaskListener.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java index 46ad5eabf..02fe4164e 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java @@ -50,6 +50,7 @@ public void started(TaskEvent e) { // we remove the semanticdb file for this source file to ensure // stale data doesn't cause problems if (e.getKind() == TaskEvent.Kind.ENTER) { + inferBazelSourceroot(e.getSourceFile()); Result semanticdbPath = semanticdbOutputPath(options, e); if (semanticdbPath.isOk()) { try { @@ -283,8 +284,6 @@ private void inferBazelSourceroot(JavaFileObject file) { private Result semanticdbOutputPath(SemanticdbJavacOptions options, TaskEvent e) { Path absolutePath = absolutePathFromUri(options, e.getSourceFile()); - if (absolutePath == null) - return Result.error("Failed to produce absolute path for " + e.getSourceFile()); if (absolutePath.startsWith(options.sourceroot)) { Path relativePath = options.sourceroot.relativize(absolutePath); String filename = relativePath.getFileName().toString() + ".semanticdb"; From 80b5f0c431acc39ec2dda5f5759e5396cf156c96 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 20 Aug 2024 10:46:27 +0100 Subject: [PATCH 9/9] Separate writing and appending to Semanticdb --- .../SemanticdbTaskListener.java | 141 +++++++++--------- 1 file changed, 72 insertions(+), 69 deletions(-) diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java index 02fe4164e..7c5238f65 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java @@ -114,7 +114,9 @@ private void onFinishedAnalyze(TaskEvent e) { Semanticdb.TextDocument textDocument = new SemanticdbVisitor(globals, e.getCompilationUnit(), options, types, trees, elements) .buildTextDocument(e.getCompilationUnit()); - writeSemanticdb(e, path.getOrThrow(), textDocument); + Path output = path.getOrThrow(); + if (Files.exists(output)) appendSemanticdb(e, output, textDocument); + else writeSemanticdb(e, output, textDocument); } else { reporter.error(path.getErrorOrThrow(), e); } @@ -122,84 +124,85 @@ private void onFinishedAnalyze(TaskEvent e) { } private void writeSemanticdb(TaskEvent event, Path output, Semanticdb.TextDocument textDocument) { - if (Files.exists(output)) { - /* - * If there already is a semanticdb file at the given path, - * we do the following: - * - Read a documents collection - * - Try to find the document with the matching relative path (matching the incoming textDocument) - * - Then, depending on whether a matching document already exists in the collection: - * - if YES, mutate it in place to only add entries from the incoming document - * - if NO, simply add the incoming text document to the collection - * - Write the collection back to disk - * */ - Semanticdb.TextDocument document = null; - int documentIndex = -1; - Semanticdb.TextDocuments documents = null; + try { + byte[] bytes = + Semanticdb.TextDocuments.newBuilder().addDocuments(textDocument).build().toByteArray(); + Files.createDirectories(output.getParent()); + Files.write(output, bytes); + } catch (IOException e) { + this.reportException(e, event); + } + } - try (InputStream is = Files.newInputStream(output.toFile().toPath())) { - documents = Semanticdb.TextDocuments.parseFrom(is); + private void appendSemanticdb( + TaskEvent event, Path output, Semanticdb.TextDocument textDocument) { + /* + * If there already is a semanticdb file at the given path, + * we do the following: + * - Read a documents collection + * - Try to find the document with the matching relative path (matching the incoming textDocument) + * - Then, depending on whether a matching document already exists in the collection: + * - if YES, mutate it in place to only add entries from the incoming document + * - if NO, simply add the incoming text document to the collection + * - Write the collection back to disk + * */ + Semanticdb.TextDocument document = null; + int documentIndex = -1; + Semanticdb.TextDocuments documents = null; - for (int i = 0; i < documents.getDocumentsCount(); i++) { - Semanticdb.TextDocument candidate = documents.getDocuments(i); - if (document == null && candidate.getUri().equals(textDocument.getUri())) { - document = candidate; - documentIndex = i; - } - } + try (InputStream is = Files.newInputStream(output.toFile().toPath())) { + documents = Semanticdb.TextDocuments.parseFrom(is); - } catch (IOException e) { - this.reportException(e, event); - return; + for (int i = 0; i < documents.getDocumentsCount(); i++) { + Semanticdb.TextDocument candidate = documents.getDocuments(i); + if (document == null && candidate.getUri().equals(textDocument.getUri())) { + document = candidate; + documentIndex = i; + } } - if (document != null) { - // If there is a previous semanticdb document at this path, we need - // to deduplicate symbols and occurrences and mutate the document in place - Set symbols = new HashSet<>(textDocument.getSymbolsList()); - Set occurrences = - new HashSet<>(textDocument.getOccurrencesList()); - Set synthetics = new HashSet<>(textDocument.getSyntheticsList()); + } catch (IOException e) { + this.reportException(e, event); + return; + } - symbols.addAll(document.getSymbolsList()); - occurrences.addAll(document.getOccurrencesList()); - synthetics.addAll(document.getSyntheticsList()); + if (document != null) { + // If there is a previous semanticdb document at this path, we need + // to deduplicate symbols and occurrences and mutate the document in place + Set symbols = new HashSet<>(textDocument.getSymbolsList()); + Set occurrences = + new HashSet<>(textDocument.getOccurrencesList()); + Set synthetics = new HashSet<>(textDocument.getSyntheticsList()); - documents - .toBuilder() - .addDocuments( - documentIndex, - document - .toBuilder() - .clearOccurrences() - .addAllOccurrences(occurrences) - .clearSymbols() - .addAllSymbols(symbols) - .clearSynthetics() - .addAllSynthetics(synthetics)); + symbols.addAll(document.getSymbolsList()); + occurrences.addAll(document.getOccurrencesList()); + synthetics.addAll(document.getSyntheticsList()); - } else { - // If no prior document was found, we can just add the incoming one to the collection - documents = documents.toBuilder().addDocuments(textDocument).build(); - } + documents + .toBuilder() + .addDocuments( + documentIndex, + document + .toBuilder() + .clearOccurrences() + .addAllOccurrences(occurrences) + .clearSymbols() + .addAllSymbols(symbols) + .clearSynthetics() + .addAllSynthetics(synthetics)); - byte[] bytes = documents.toByteArray(); + } else { + // If no prior document was found, we can just add the incoming one to the collection + documents = documents.toBuilder().addDocuments(textDocument).build(); + } - try { - Files.createDirectories(output.getParent()); - Files.write(output, bytes); - } catch (IOException e) { - this.reportException(e, event); - } - } else { // OVERWRITE - try { - byte[] bytes = - Semanticdb.TextDocuments.newBuilder().addDocuments(textDocument).build().toByteArray(); - Files.createDirectories(output.getParent()); - Files.write(output, bytes); - } catch (IOException e) { - this.reportException(e, event); - } + byte[] bytes = documents.toByteArray(); + + try { + Files.createDirectories(output.getParent()); + Files.write(output, bytes); + } catch (IOException e) { + this.reportException(e, event); } }