From f123b53d195e920e303a039380a24c431580cf68 Mon Sep 17 00:00:00 2001 From: rombirli Date: Fri, 20 Nov 2026 08:40:41 +0100 Subject: [PATCH 1/5] SONARJAVA-7339 Add the SemSitter front-end module with its protobuf data model --- java-frontend-semsitter/pom.xml | 56 ++++ .../src/main/proto/semsitter.proto | 313 ++++++++++++++++++ .../java/semsitter/protocol/ProtocolTest.java | 150 +++++++++ pom.xml | 7 + 4 files changed, 526 insertions(+) create mode 100644 java-frontend-semsitter/pom.xml create mode 100644 java-frontend-semsitter/src/main/proto/semsitter.proto create mode 100644 java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/protocol/ProtocolTest.java diff --git a/java-frontend-semsitter/pom.xml b/java-frontend-semsitter/pom.xml new file mode 100644 index 00000000000..b2ec687d67b --- /dev/null +++ b/java-frontend-semsitter/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + + + org.sonarsource.java + java + 9.0.0-SNAPSHOT + + + java-frontend-semsitter + + SonarQube Java :: SemSitter Front-end + Code Analyzer for Java :: Front-end based on SemSitter + + + target/generated-sources/** + + + + + com.google.protobuf + protobuf-java + + + org.junit.jupiter + junit-jupiter + test + + + org.assertj + assertj-core + test + + + + + + + io.github.ascopes + protobuf-maven-plugin + 5.1.12 + + ${protobuf.version} + + + + + generate + + + + + + + diff --git a/java-frontend-semsitter/src/main/proto/semsitter.proto b/java-frontend-semsitter/src/main/proto/semsitter.proto new file mode 100644 index 00000000000..869445a2fb6 --- /dev/null +++ b/java-frontend-semsitter/src/main/proto/semsitter.proto @@ -0,0 +1,313 @@ +// Protocol between SonarJava and the SemSitter Java analyzer server (udg-java-server). +// Each message is written length-delimited (varint size, then the message) on the standard +// streams of the server: requests on its input, responses on its output. +syntax = "proto3"; + +package sonar.semsitter; + +option java_multiple_files = true; +option java_package = "org.sonar.java.semsitter.protocol"; + +message Request { + // Echoed by the response. + uint32 id = 1; + oneof command { + HelloRequest hello = 2; + SetSettingsRequest set_settings = 3; + FullGenRequest full_gen = 4; + GetFileRequest get_file = 5; + GetTypesRequest get_types = 6; + HasPackageRequest has_package = 7; + QuitRequest quit = 8; + } +} + +message HelloRequest { +} + +message SetSettingsRequest { + // Replaces the settings of the session, such as jvm.libraries or jvm.java_version. + map settings = 1; +} + +message FullGenRequest { + // Scans exactly these files; none scans the source roots. + repeated string files = 1; +} + +message GetFileRequest { + string path = 1; + // Files after this one to prepare in the background. + uint32 ahead = 2; +} + +message GetTypesRequest { + // Erased binary names, nested types joined with '$'. + repeated string names = 1; +} + +message HasPackageRequest { + repeated string names = 1; +} + +message QuitRequest { +} + +message Response { + uint32 id = 1; + // Present when the scan has unresolved imports and no dependencies are configured. + repeated string warnings = 2; + // Unset for a quit request. + oneof result { + string error = 3; + HelloResponse hello = 4; + SetSettingsResponse set_settings = 5; + FullGenResponse full_gen = 6; + Document document = 7; + TypesResponse types = 8; + HasPackageResponse has_package = 9; + } +} + +message HelloResponse { + // Semantic version; a client rejects another major version. + string protocol = 1; +} + +message SetSettingsResponse { + bool rescanned = 1; + repeated string diagnostics = 2; +} + +message FullGenResponse { + uint32 files = 1; + uint32 failures = 2; + repeated string diagnostics = 3; +} + +message TypesResponse { + map declarations = 1; + // Requested names declared by the project. + repeated string project = 2; + // Requested names an earlier answer carried, not repeated. + repeated string already_sent = 3; + // Requested names no source, JDK or library declares. + repeated string missing = 4; +} + +message HasPackageResponse { + map packages = 1; +} + +message StringList { + repeated string values = 1; +} + +// The syntax tree of one file with its declarations, references, symbols and graph facts. +message Document { + string path = 1; + bool has_error = 2; + bool semantic = 3; + Node tree = 4; + repeated Symbol symbols = 5; + map facts = 6; +} + +// Lines are 1-based, columns are 0-based UTF-16 code units. +message Node { + string kind = 1; + bool named = 2; + string field = 3; + bool missing = 4; + uint32 start_line = 5; + uint32 start_column = 6; + uint32 end_line = 7; + uint32 end_column = 8; + uint32 start_byte = 9; + uint32 end_byte = 10; + string text = 11; + repeated Node children = 12; + // Ids of the symbols the node declares. + repeated string declarations = 13; + repeated Reference references = 14; +} + +message Reference { + string access = 1; + string outcome = 2; + string target = 3; + string reason = 4; + bool bounded = 5; + map facts = 6; +} + +message Symbol { + string id = 1; + string kind = 2; + string name = 3; + string owner = 4; + // Unset for a synthesized symbol. + Place place = 5; + map facts = 6; +} + +message Place { + uint32 start_byte = 1; + uint32 end_byte = 2; + uint32 line = 3; + uint32 column = 4; +} + +message FactValue { + oneof value { + bool flag = 1; + StringList ids = 2; + string text = 3; + TypeRef type = 4; + } +} + +message Fact { + string kind = 1; + string tier = 2; + string parent = 3; + map props = 4; + repeated string extends = 5; + repeated string implements = 6; + repeated string permits = 7; + repeated string returns = 8; + repeated string type = 9; + repeated string params = 10; + repeated string overrides = 11; + repeated string annotations = 12; +} + +message TypeRef { + enum Kind { + // A class, primitive or void type, by id. + NAMED = 0; + ARRAY = 1; + TYPE_VARIABLE = 2; + WILDCARD = 3; + UNKNOWN = 4; + } + Kind kind = 1; + string id = 2; + repeated TypeRef arguments = 3; + // The enclosing type of Outer.Inner. + TypeRef outer = 4; + TypeRef element = 5; + TypeRef extends_bound = 6; + TypeRef super_bound = 7; +} + +message TypeDeclaration { + // project, classpath, jdk or framework-db. + string origin = 1; + string id = 2; + string kind = 3; + optional string superclass = 4; + repeated string interfaces = 5; + repeated string modifiers = 6; + repeated Annotation annotations = 7; + uint32 type_parameter_count = 8; + repeated string type_variables = 9; + repeated TypeBounds type_variable_bounds = 10; + TypeRef superclass_type = 11; + repeated TypeRef interface_types = 12; + repeated TypeAnnotation type_annotations = 13; + repeated Member members = 14; +} + +message TypeBounds { + repeated TypeRef bounds = 1; +} + +// A library member is described by its fields, a project member by its id and facts. +message Member { + enum Kind { + KIND_UNSPECIFIED = 0; + METHOD = 1; + FIELD = 2; + } + Kind kind = 1; + string id = 2; + string name = 3; + string jvm_descriptor = 4; + string visibility = 5; + bool is_static = 6; + bool is_abstract = 7; + bool is_final = 8; + bool is_synchronized = 9; + bool is_varargs = 10; + bool is_enum_constant = 11; + optional string constant = 12; + repeated Annotation annotations = 13; + repeated TypeAnnotation type_annotations = 14; + repeated AnnotationList parameter_annotations = 15; + ElementValue default_value = 16; + repeated string throws = 17; + repeated TypeRef throws_types = 18; + repeated string use_ids = 19; + repeated string type_variables = 20; + repeated TypeBounds type_variable_bounds = 21; + TypeRef return_type = 22; + repeated TypeRef parameter_types = 23; + repeated string parameter_variables = 24; + // Index of the class type variable the method returns. + optional uint32 return_parameter = 25; + bool returns_self = 26; + TypeRef declared_type = 27; + Fact facts = 28; +} + +message Annotation { + string name = 1; + repeated ElementValue values = 2; +} + +message AnnotationList { + repeated Annotation annotations = 1; +} + +message TypeAnnotation { + enum Target { + TYPE = 0; + PARAMETER = 1; + SUPERCLASS = 2; + INTERFACE = 3; + } + Annotation annotation = 1; + Target target = 2; + uint32 index = 3; + // Steps into the annotated type: "[]", "?" or "". + repeated string path = 4; +} + +message ElementValue { + string name = 1; + oneof value { + bool boolean_value = 2; + sint32 byte_value = 3; + uint32 char_value = 4; + sint32 short_value = 5; + sint32 int_value = 6; + sint64 long_value = 7; + float float_value = 8; + double double_value = 9; + string string_value = 10; + string class_value = 11; + EnumConstant enum_value = 12; + Annotation annotation_value = 13; + ElementValueList array_value = 14; + } +} + +message EnumConstant { + string type = 1; + string constant = 2; +} + +message ElementValueList { + repeated ElementValue values = 1; +} diff --git a/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/protocol/ProtocolTest.java b/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/protocol/ProtocolTest.java new file mode 100644 index 00000000000..f58eb0f5cb6 --- /dev/null +++ b/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/protocol/ProtocolTest.java @@ -0,0 +1,150 @@ +/* + * SonarQube Java + * Copyright (C) SonarSource Sàrl + * mailto:info AT sonarsource DOT com + * + * You can redistribute and/or modify this program under the terms of + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the Sonar Source-Available License for more details. + * + * You should have received a copy of the Sonar Source-Available License + * along with this program; if not, see https://sonarsource.com/license/ssal/ + */ +package org.sonar.java.semsitter.protocol; + +import com.google.protobuf.MessageLite; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class ProtocolTest { + + @Test + void requests_round_trip_length_delimited() throws IOException { + Request settings = Request.newBuilder() + .setId(1) + .setSetSettings(SetSettingsRequest.newBuilder() + .putSettings("jvm.libraries", StringList.newBuilder().addValues("/libs/a.jar").addValues("/libs/b.jar").build())) + .build(); + Request getFile = Request.newBuilder() + .setId(2) + .setGetFile(GetFileRequest.newBuilder().setPath("p/A.java").setAhead(1)) + .build(); + + InputStream in = roundTrip(settings, getFile); + + assertThat(Request.parseDelimitedFrom(in)).isEqualTo(settings); + Request read = Request.parseDelimitedFrom(in); + assertThat(read.getCommandCase()).isEqualTo(Request.CommandCase.GET_FILE); + assertThat(read.getGetFile().getPath()).isEqualTo("p/A.java"); + assertThat(Request.parseDelimitedFrom(in)).isNull(); + } + + @Test + void document_round_trips_with_symbols_references_and_facts() throws IOException { + TypeRef listOfString = TypeRef.newBuilder() + .setId("java.util.List") + .addArguments(TypeRef.newBuilder().setId("java.lang.String")) + .build(); + Node identifier = Node.newBuilder() + .setKind("identifier") + .setNamed(true) + .setStartLine(2) + .setStartColumn(4) + .setText("xs") + .addDeclarations("p.A#xs") + .addReferences(Reference.newBuilder() + .setOutcome("resolved") + .setTarget("java.util.List") + .putFacts("type", FactValue.newBuilder().setType(listOfString).build())) + .build(); + Response response = Response.newBuilder() + .setId(3) + .setDocument(Document.newBuilder() + .setPath("p/A.java") + .setSemantic(true) + .setTree(Node.newBuilder().setKind("program").addChildren(identifier)) + .addSymbols(Symbol.newBuilder() + .setId("p.A#xs") + .setKind("field") + .setName("xs") + .setPlace(Place.newBuilder().setStartByte(30).setEndByte(32).setLine(2).setColumn(4)) + .putFacts("final", FactValue.newBuilder().setFlag(true).build())) + .putFacts("p.A", Fact.newBuilder() + .setKind("class") + .putProps("modifiers", StringList.newBuilder().addValues("public").build()) + .build())) + .build(); + + Response read = Response.parseDelimitedFrom(roundTrip(response)); + + assertThat(read).isEqualTo(response); + assertThat(read.getResultCase()).isEqualTo(Response.ResultCase.DOCUMENT); + Reference reference = read.getDocument().getTree().getChildren(0).getReferences(0); + assertThat(reference.getFactsOrThrow("type").getType().getArguments(0).getId()).isEqualTo("java.lang.String"); + } + + @Test + void type_declarations_round_trip() throws IOException { + Member get = Member.newBuilder() + .setKind(Member.Kind.METHOD) + .setName("get") + .setJvmDescriptor("(I)Ljava/lang/Object;") + .setIsAbstract(true) + .setReturnType(TypeRef.newBuilder().setKind(TypeRef.Kind.TYPE_VARIABLE).setId("E")) + .addParameterTypes(TypeRef.newBuilder().setId("int")) + .addAnnotations(Annotation.newBuilder() + .setName("java.lang.Deprecated") + .addValues(ElementValue.newBuilder().setName("forRemoval").setBooleanValue(true))) + .build(); + Response response = Response.newBuilder() + .setId(4) + .addWarnings("unresolved import x.Y") + .setTypes(TypesResponse.newBuilder() + .putDeclarations("java.util.List", TypeDeclaration.newBuilder() + .setOrigin("jdk") + .setKind("interface") + .addInterfaces("java.util.Collection") + .setTypeParameterCount(1) + .addTypeVariables("E") + .addMembers(get) + .build()) + .addMissing("x.Missing")) + .build(); + + Response read = Response.parseDelimitedFrom(roundTrip(response)); + + assertThat(read).isEqualTo(response); + TypeDeclaration list = read.getTypes().getDeclarationsOrThrow("java.util.List"); + assertThat(list.hasSuperclass()).isFalse(); + assertThat(list.getMembers(0).hasReturnParameter()).isFalse(); + assertThat(list.getMembers(0).getAnnotations(0).getValues(0).getValueCase()).isEqualTo(ElementValue.ValueCase.BOOLEAN_VALUE); + } + + @Test + void error_and_empty_results_are_told_apart() throws IOException { + Response error = Response.newBuilder().setId(5).setError("no such file p/Nope.java").build(); + Response quit = Response.newBuilder().setId(6).build(); + + InputStream in = roundTrip(error, quit); + + assertThat(Response.parseDelimitedFrom(in).getResultCase()).isEqualTo(Response.ResultCase.ERROR); + assertThat(Response.parseDelimitedFrom(in).getResultCase()).isEqualTo(Response.ResultCase.RESULT_NOT_SET); + } + + private static InputStream roundTrip(MessageLite... messages) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + for (MessageLite message : messages) { + message.writeDelimitedTo(out); + } + return new ByteArrayInputStream(out.toByteArray()); + } +} diff --git a/pom.xml b/pom.xml index a96781bc2df..0c39c07e922 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,7 @@ java-checks-test-sources java-frontend + java-frontend-semsitter java-checks-testkit java-checks java-checks-aws @@ -98,6 +99,7 @@ 6.4.4.4797 1.7.0.11463 1.26.0.4194 + 4.36.2 -Xmx512m sonar-java @@ -339,6 +341,11 @@ 2.15.0 compile + + com.google.protobuf + protobuf-java + ${protobuf.version} + com.fasterxml.staxmate From e65e6358016ed827fe6bad858862e2e00597c30c Mon Sep 17 00:00:00 2001 From: rombirli Date: Fri, 20 Nov 2026 09:27:45 +0100 Subject: [PATCH 2/5] SONARJAVA-7339 Use the SemSitter analyzer wire library for the data model --- java-frontend-semsitter/pom.xml | 28 +- .../src/main/proto/semsitter.proto | 313 ------------------ .../sonar/java/semsitter/ProtocolTest.java | 100 ++++++ .../java/semsitter/protocol/ProtocolTest.java | 150 --------- pom.xml | 7 +- 5 files changed, 105 insertions(+), 493 deletions(-) delete mode 100644 java-frontend-semsitter/src/main/proto/semsitter.proto create mode 100644 java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/ProtocolTest.java delete mode 100644 java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/protocol/ProtocolTest.java diff --git a/java-frontend-semsitter/pom.xml b/java-frontend-semsitter/pom.xml index b2ec687d67b..1aff2e46b6c 100644 --- a/java-frontend-semsitter/pom.xml +++ b/java-frontend-semsitter/pom.xml @@ -13,14 +13,10 @@ SonarQube Java :: SemSitter Front-end Code Analyzer for Java :: Front-end based on SemSitter - - target/generated-sources/** - - - com.google.protobuf - protobuf-java + org.sonarsource.semsitter + analyzer-wire org.junit.jupiter @@ -33,24 +29,4 @@ test - - - - - io.github.ascopes - protobuf-maven-plugin - 5.1.12 - - ${protobuf.version} - - - - - generate - - - - - - diff --git a/java-frontend-semsitter/src/main/proto/semsitter.proto b/java-frontend-semsitter/src/main/proto/semsitter.proto deleted file mode 100644 index 869445a2fb6..00000000000 --- a/java-frontend-semsitter/src/main/proto/semsitter.proto +++ /dev/null @@ -1,313 +0,0 @@ -// Protocol between SonarJava and the SemSitter Java analyzer server (udg-java-server). -// Each message is written length-delimited (varint size, then the message) on the standard -// streams of the server: requests on its input, responses on its output. -syntax = "proto3"; - -package sonar.semsitter; - -option java_multiple_files = true; -option java_package = "org.sonar.java.semsitter.protocol"; - -message Request { - // Echoed by the response. - uint32 id = 1; - oneof command { - HelloRequest hello = 2; - SetSettingsRequest set_settings = 3; - FullGenRequest full_gen = 4; - GetFileRequest get_file = 5; - GetTypesRequest get_types = 6; - HasPackageRequest has_package = 7; - QuitRequest quit = 8; - } -} - -message HelloRequest { -} - -message SetSettingsRequest { - // Replaces the settings of the session, such as jvm.libraries or jvm.java_version. - map settings = 1; -} - -message FullGenRequest { - // Scans exactly these files; none scans the source roots. - repeated string files = 1; -} - -message GetFileRequest { - string path = 1; - // Files after this one to prepare in the background. - uint32 ahead = 2; -} - -message GetTypesRequest { - // Erased binary names, nested types joined with '$'. - repeated string names = 1; -} - -message HasPackageRequest { - repeated string names = 1; -} - -message QuitRequest { -} - -message Response { - uint32 id = 1; - // Present when the scan has unresolved imports and no dependencies are configured. - repeated string warnings = 2; - // Unset for a quit request. - oneof result { - string error = 3; - HelloResponse hello = 4; - SetSettingsResponse set_settings = 5; - FullGenResponse full_gen = 6; - Document document = 7; - TypesResponse types = 8; - HasPackageResponse has_package = 9; - } -} - -message HelloResponse { - // Semantic version; a client rejects another major version. - string protocol = 1; -} - -message SetSettingsResponse { - bool rescanned = 1; - repeated string diagnostics = 2; -} - -message FullGenResponse { - uint32 files = 1; - uint32 failures = 2; - repeated string diagnostics = 3; -} - -message TypesResponse { - map declarations = 1; - // Requested names declared by the project. - repeated string project = 2; - // Requested names an earlier answer carried, not repeated. - repeated string already_sent = 3; - // Requested names no source, JDK or library declares. - repeated string missing = 4; -} - -message HasPackageResponse { - map packages = 1; -} - -message StringList { - repeated string values = 1; -} - -// The syntax tree of one file with its declarations, references, symbols and graph facts. -message Document { - string path = 1; - bool has_error = 2; - bool semantic = 3; - Node tree = 4; - repeated Symbol symbols = 5; - map facts = 6; -} - -// Lines are 1-based, columns are 0-based UTF-16 code units. -message Node { - string kind = 1; - bool named = 2; - string field = 3; - bool missing = 4; - uint32 start_line = 5; - uint32 start_column = 6; - uint32 end_line = 7; - uint32 end_column = 8; - uint32 start_byte = 9; - uint32 end_byte = 10; - string text = 11; - repeated Node children = 12; - // Ids of the symbols the node declares. - repeated string declarations = 13; - repeated Reference references = 14; -} - -message Reference { - string access = 1; - string outcome = 2; - string target = 3; - string reason = 4; - bool bounded = 5; - map facts = 6; -} - -message Symbol { - string id = 1; - string kind = 2; - string name = 3; - string owner = 4; - // Unset for a synthesized symbol. - Place place = 5; - map facts = 6; -} - -message Place { - uint32 start_byte = 1; - uint32 end_byte = 2; - uint32 line = 3; - uint32 column = 4; -} - -message FactValue { - oneof value { - bool flag = 1; - StringList ids = 2; - string text = 3; - TypeRef type = 4; - } -} - -message Fact { - string kind = 1; - string tier = 2; - string parent = 3; - map props = 4; - repeated string extends = 5; - repeated string implements = 6; - repeated string permits = 7; - repeated string returns = 8; - repeated string type = 9; - repeated string params = 10; - repeated string overrides = 11; - repeated string annotations = 12; -} - -message TypeRef { - enum Kind { - // A class, primitive or void type, by id. - NAMED = 0; - ARRAY = 1; - TYPE_VARIABLE = 2; - WILDCARD = 3; - UNKNOWN = 4; - } - Kind kind = 1; - string id = 2; - repeated TypeRef arguments = 3; - // The enclosing type of Outer.Inner. - TypeRef outer = 4; - TypeRef element = 5; - TypeRef extends_bound = 6; - TypeRef super_bound = 7; -} - -message TypeDeclaration { - // project, classpath, jdk or framework-db. - string origin = 1; - string id = 2; - string kind = 3; - optional string superclass = 4; - repeated string interfaces = 5; - repeated string modifiers = 6; - repeated Annotation annotations = 7; - uint32 type_parameter_count = 8; - repeated string type_variables = 9; - repeated TypeBounds type_variable_bounds = 10; - TypeRef superclass_type = 11; - repeated TypeRef interface_types = 12; - repeated TypeAnnotation type_annotations = 13; - repeated Member members = 14; -} - -message TypeBounds { - repeated TypeRef bounds = 1; -} - -// A library member is described by its fields, a project member by its id and facts. -message Member { - enum Kind { - KIND_UNSPECIFIED = 0; - METHOD = 1; - FIELD = 2; - } - Kind kind = 1; - string id = 2; - string name = 3; - string jvm_descriptor = 4; - string visibility = 5; - bool is_static = 6; - bool is_abstract = 7; - bool is_final = 8; - bool is_synchronized = 9; - bool is_varargs = 10; - bool is_enum_constant = 11; - optional string constant = 12; - repeated Annotation annotations = 13; - repeated TypeAnnotation type_annotations = 14; - repeated AnnotationList parameter_annotations = 15; - ElementValue default_value = 16; - repeated string throws = 17; - repeated TypeRef throws_types = 18; - repeated string use_ids = 19; - repeated string type_variables = 20; - repeated TypeBounds type_variable_bounds = 21; - TypeRef return_type = 22; - repeated TypeRef parameter_types = 23; - repeated string parameter_variables = 24; - // Index of the class type variable the method returns. - optional uint32 return_parameter = 25; - bool returns_self = 26; - TypeRef declared_type = 27; - Fact facts = 28; -} - -message Annotation { - string name = 1; - repeated ElementValue values = 2; -} - -message AnnotationList { - repeated Annotation annotations = 1; -} - -message TypeAnnotation { - enum Target { - TYPE = 0; - PARAMETER = 1; - SUPERCLASS = 2; - INTERFACE = 3; - } - Annotation annotation = 1; - Target target = 2; - uint32 index = 3; - // Steps into the annotated type: "[]", "?" or "". - repeated string path = 4; -} - -message ElementValue { - string name = 1; - oneof value { - bool boolean_value = 2; - sint32 byte_value = 3; - uint32 char_value = 4; - sint32 short_value = 5; - sint32 int_value = 6; - sint64 long_value = 7; - float float_value = 8; - double double_value = 9; - string string_value = 10; - string class_value = 11; - EnumConstant enum_value = 12; - Annotation annotation_value = 13; - ElementValueList array_value = 14; - } -} - -message EnumConstant { - string type = 1; - string constant = 2; -} - -message ElementValueList { - repeated ElementValue values = 1; -} diff --git a/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/ProtocolTest.java b/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/ProtocolTest.java new file mode 100644 index 00000000000..e3e56a42bd4 --- /dev/null +++ b/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/ProtocolTest.java @@ -0,0 +1,100 @@ +/* + * SonarQube Java + * Copyright (C) SonarSource Sàrl + * mailto:info AT sonarsource DOT com + * + * You can redistribute and/or modify this program under the terms of + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the Sonar Source-Available License for more details. + * + * You should have received a copy of the Sonar Source-Available License + * along with this program; if not, see https://sonarsource.com/license/ssal/ + */ +package org.sonar.java.semsitter; + +import com.google.protobuf.MessageLite; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import org.junit.jupiter.api.Test; +import org.sonarsource.semsitter.wire.Document; +import org.sonarsource.semsitter.wire.Fact; +import org.sonarsource.semsitter.wire.FactEntry; +import org.sonarsource.semsitter.wire.FactValue; +import org.sonarsource.semsitter.wire.Node; +import org.sonarsource.semsitter.wire.Place; +import org.sonarsource.semsitter.wire.Ref; +import org.sonarsource.semsitter.wire.StringList; +import org.sonarsource.semsitter.wire.Symbol; +import org.sonarsource.semsitter.wire.TypeRef; + +import static org.assertj.core.api.Assertions.assertThat; + +class ProtocolTest { + + @Test + void document_round_trips() throws IOException { + TypeRef listOfString = TypeRef.newBuilder() + .setId("java.util.List") + .addArguments(TypeRef.newBuilder().setId("java.lang.String")) + .build(); + Document document = Document.newBuilder() + .setPath("p/A.java") + .setSemantic(true) + .setTree(Node.newBuilder() + .setKind("program") + .addChildren(Node.newBuilder() + .setKind("identifier") + .setNamed(true) + .setStartLine(2) + .setText("xs") + .addDecls("p.A#xs") + .addRefs(Ref.newBuilder() + .setOutcome("resolved") + .setTarget("java.util.List") + .putFacts("type", FactValue.newBuilder().setType(listOfString).build())))) + .addSymbols(Symbol.newBuilder() + .setId("p.A#xs") + .setKind("field") + .setName("xs") + .setPlace(Place.newBuilder().setStartByte(30).setEndByte(32).setLine(2).setColumn(4)) + .putFacts("final", FactValue.newBuilder().setFlag(true).build())) + .addFacts(FactEntry.newBuilder() + .setKey("p.A") + .setValue(Fact.newBuilder() + .setKind("class") + .addProps(org.sonarsource.semsitter.wire.Prop.newBuilder() + .setName("modifiers") + .addValues("public") + .build()) + .build())) + .build(); + + Document read = Document.parseDelimitedFrom(roundTrip(document)); + + assertThat(read).isEqualTo(document); + assertThat(read.getTree().getChildren(0).getRefs(0).getFactsOrThrow("type").getType().getArguments(0).getId()) + .isEqualTo("java.lang.String"); + assertThat(read.getSymbols(0).getFactsOrThrow("final").getFlag()).isTrue(); + } + + @Test + void string_lists_round_trip() throws IOException { + StringList libraries = StringList.newBuilder().addValues("a.jar").addValues("b.jar").build(); + + StringList read = StringList.parseDelimitedFrom(roundTrip(libraries)); + + assertThat(read).isEqualTo(libraries); + } + + private static InputStream roundTrip(MessageLite message) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + message.writeDelimitedTo(out); + return new ByteArrayInputStream(out.toByteArray()); + } +} diff --git a/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/protocol/ProtocolTest.java b/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/protocol/ProtocolTest.java deleted file mode 100644 index f58eb0f5cb6..00000000000 --- a/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/protocol/ProtocolTest.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ -package org.sonar.java.semsitter.protocol; - -import com.google.protobuf.MessageLite; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -class ProtocolTest { - - @Test - void requests_round_trip_length_delimited() throws IOException { - Request settings = Request.newBuilder() - .setId(1) - .setSetSettings(SetSettingsRequest.newBuilder() - .putSettings("jvm.libraries", StringList.newBuilder().addValues("/libs/a.jar").addValues("/libs/b.jar").build())) - .build(); - Request getFile = Request.newBuilder() - .setId(2) - .setGetFile(GetFileRequest.newBuilder().setPath("p/A.java").setAhead(1)) - .build(); - - InputStream in = roundTrip(settings, getFile); - - assertThat(Request.parseDelimitedFrom(in)).isEqualTo(settings); - Request read = Request.parseDelimitedFrom(in); - assertThat(read.getCommandCase()).isEqualTo(Request.CommandCase.GET_FILE); - assertThat(read.getGetFile().getPath()).isEqualTo("p/A.java"); - assertThat(Request.parseDelimitedFrom(in)).isNull(); - } - - @Test - void document_round_trips_with_symbols_references_and_facts() throws IOException { - TypeRef listOfString = TypeRef.newBuilder() - .setId("java.util.List") - .addArguments(TypeRef.newBuilder().setId("java.lang.String")) - .build(); - Node identifier = Node.newBuilder() - .setKind("identifier") - .setNamed(true) - .setStartLine(2) - .setStartColumn(4) - .setText("xs") - .addDeclarations("p.A#xs") - .addReferences(Reference.newBuilder() - .setOutcome("resolved") - .setTarget("java.util.List") - .putFacts("type", FactValue.newBuilder().setType(listOfString).build())) - .build(); - Response response = Response.newBuilder() - .setId(3) - .setDocument(Document.newBuilder() - .setPath("p/A.java") - .setSemantic(true) - .setTree(Node.newBuilder().setKind("program").addChildren(identifier)) - .addSymbols(Symbol.newBuilder() - .setId("p.A#xs") - .setKind("field") - .setName("xs") - .setPlace(Place.newBuilder().setStartByte(30).setEndByte(32).setLine(2).setColumn(4)) - .putFacts("final", FactValue.newBuilder().setFlag(true).build())) - .putFacts("p.A", Fact.newBuilder() - .setKind("class") - .putProps("modifiers", StringList.newBuilder().addValues("public").build()) - .build())) - .build(); - - Response read = Response.parseDelimitedFrom(roundTrip(response)); - - assertThat(read).isEqualTo(response); - assertThat(read.getResultCase()).isEqualTo(Response.ResultCase.DOCUMENT); - Reference reference = read.getDocument().getTree().getChildren(0).getReferences(0); - assertThat(reference.getFactsOrThrow("type").getType().getArguments(0).getId()).isEqualTo("java.lang.String"); - } - - @Test - void type_declarations_round_trip() throws IOException { - Member get = Member.newBuilder() - .setKind(Member.Kind.METHOD) - .setName("get") - .setJvmDescriptor("(I)Ljava/lang/Object;") - .setIsAbstract(true) - .setReturnType(TypeRef.newBuilder().setKind(TypeRef.Kind.TYPE_VARIABLE).setId("E")) - .addParameterTypes(TypeRef.newBuilder().setId("int")) - .addAnnotations(Annotation.newBuilder() - .setName("java.lang.Deprecated") - .addValues(ElementValue.newBuilder().setName("forRemoval").setBooleanValue(true))) - .build(); - Response response = Response.newBuilder() - .setId(4) - .addWarnings("unresolved import x.Y") - .setTypes(TypesResponse.newBuilder() - .putDeclarations("java.util.List", TypeDeclaration.newBuilder() - .setOrigin("jdk") - .setKind("interface") - .addInterfaces("java.util.Collection") - .setTypeParameterCount(1) - .addTypeVariables("E") - .addMembers(get) - .build()) - .addMissing("x.Missing")) - .build(); - - Response read = Response.parseDelimitedFrom(roundTrip(response)); - - assertThat(read).isEqualTo(response); - TypeDeclaration list = read.getTypes().getDeclarationsOrThrow("java.util.List"); - assertThat(list.hasSuperclass()).isFalse(); - assertThat(list.getMembers(0).hasReturnParameter()).isFalse(); - assertThat(list.getMembers(0).getAnnotations(0).getValues(0).getValueCase()).isEqualTo(ElementValue.ValueCase.BOOLEAN_VALUE); - } - - @Test - void error_and_empty_results_are_told_apart() throws IOException { - Response error = Response.newBuilder().setId(5).setError("no such file p/Nope.java").build(); - Response quit = Response.newBuilder().setId(6).build(); - - InputStream in = roundTrip(error, quit); - - assertThat(Response.parseDelimitedFrom(in).getResultCase()).isEqualTo(Response.ResultCase.ERROR); - assertThat(Response.parseDelimitedFrom(in).getResultCase()).isEqualTo(Response.ResultCase.RESULT_NOT_SET); - } - - private static InputStream roundTrip(MessageLite... messages) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - for (MessageLite message : messages) { - message.writeDelimitedTo(out); - } - return new ByteArrayInputStream(out.toByteArray()); - } -} diff --git a/pom.xml b/pom.xml index 0c39c07e922..fba4acfc995 100644 --- a/pom.xml +++ b/pom.xml @@ -99,7 +99,6 @@ 6.4.4.4797 1.7.0.11463 1.26.0.4194 - 4.36.2 -Xmx512m sonar-java @@ -342,9 +341,9 @@ compile - com.google.protobuf - protobuf-java - ${protobuf.version} + org.sonarsource.semsitter + analyzer-wire + 2.0.0 From f9eee0f27fac5e75f7502c9d6b51fb1fe240676f Mon Sep 17 00:00:00 2001 From: rombirli Date: Fri, 20 Nov 2026 10:09:29 +0100 Subject: [PATCH 3/5] SONARJAVA-7339 Install analyzer-wire before the build resolves dependencies --- .github/scripts/install-analyzer-wire.sh | 13 +++++++++++++ .github/workflows/build.yml | 12 ++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 .github/scripts/install-analyzer-wire.sh diff --git a/.github/scripts/install-analyzer-wire.sh b/.github/scripts/install-analyzer-wire.sh new file mode 100644 index 00000000000..33d5dbfa70a --- /dev/null +++ b/.github/scripts/install-analyzer-wire.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -euo pipefail + +base="${ARTIFACTORY_URL}/sonarsource-public-qa/org/sonarsource/semsitter/analyzer-wire/2.0.0" +dest="${RUNNER_TEMP}/analyzer-wire" +mkdir -p "$dest" +curl -fsS -u "${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}" \ + -o "$dest/analyzer-wire-2.0.0.pom" "$base/analyzer-wire-2.0.0.pom" +curl -fsS -u "${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}" \ + -o "$dest/analyzer-wire-2.0.0.jar" "$base/analyzer-wire-2.0.0.jar" +mvn -B org.apache.maven.plugins:maven-install-plugin:3.1.2:install-file \ + -Dfile="$dest/analyzer-wire-2.0.0.jar" \ + -DpomFile="$dest/analyzer-wire-2.0.0.pom" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index dcd700ff400..c0d61a0810d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,6 +34,15 @@ jobs: - uses: jdx/mise-action@90f833d396d0cc3674b631d2d225e306476c303a # v5.2.10 with: version: 2026.10.20 + - name: Configure Maven + uses: SonarSource/ci-github-actions/config-maven@v2 + with: + repox-url: https://repox-internal.dev.sonar.build + artifactory-reader-role: private-reader + use-develocity: ${{ env.USE_DEVELOCITY }} + develocity-url: ${{ env.DEVELOCITY_URL }} + - name: Install analyzer-wire + run: bash ./.github/scripts/install-analyzer-wire.sh - uses: SonarSource/ci-github-actions/build-maven@v2 id: build-maven env: @@ -438,6 +447,9 @@ jobs: use-develocity: ${{ env.USE_DEVELOCITY }} develocity-url: ${{ env.DEVELOCITY_URL }} disable-caching: 'true' + - name: Install analyzer-wire + shell: bash + run: bash ./.github/scripts/install-analyzer-wire.sh - name: Run Maven env: JAVA_TOOL_OPTIONS: "" From 60b09e7db30137108e139ecc08ff488bdfacbbb1 Mon Sep 17 00:00:00 2001 From: rombirli Date: Fri, 20 Nov 2026 10:13:56 +0100 Subject: [PATCH 4/5] SONARJAVA-7339 Install analyzer-wire without running Maven setup twice --- .github/scripts/install-analyzer-wire.sh | 25 +++++++++++++++++++++++- .github/workflows/build.yml | 17 ++++++++++------ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/.github/scripts/install-analyzer-wire.sh b/.github/scripts/install-analyzer-wire.sh index 33d5dbfa70a..9bf394c447e 100644 --- a/.github/scripts/install-analyzer-wire.sh +++ b/.github/scripts/install-analyzer-wire.sh @@ -8,6 +8,29 @@ curl -fsS -u "${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}" \ -o "$dest/analyzer-wire-2.0.0.pom" "$base/analyzer-wire-2.0.0.pom" curl -fsS -u "${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}" \ -o "$dest/analyzer-wire-2.0.0.jar" "$base/analyzer-wire-2.0.0.jar" -mvn -B org.apache.maven.plugins:maven-install-plugin:3.1.2:install-file \ + +settings=() +if [[ ! -f "${MAVEN_CONFIG:-$HOME/.m2}/settings.xml" ]]; then + cat > "$dest/settings.xml" < + + + sonarsource + ${ARTIFACTORY_URL}/sonarsource-qa + * + + + + + sonarsource + ${ARTIFACTORY_USERNAME} + ${ARTIFACTORY_ACCESS_TOKEN} + + + +EOF + settings=(-s "$dest/settings.xml") +fi +mvn -B "${settings[@]}" org.apache.maven.plugins:maven-install-plugin:3.1.2:install-file \ -Dfile="$dest/analyzer-wire-2.0.0.jar" \ -DpomFile="$dest/analyzer-wire-2.0.0.pom" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c0d61a0810d..5c72dc325d4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,14 +34,19 @@ jobs: - uses: jdx/mise-action@90f833d396d0cc3674b631d2d225e306476c303a # v5.2.10 with: version: 2026.10.20 - - name: Configure Maven - uses: SonarSource/ci-github-actions/config-maven@v2 + - name: Read Repox credentials + id: repox + uses: SonarSource/vault-action-wrapper@v3 with: - repox-url: https://repox-internal.dev.sonar.build - artifactory-reader-role: private-reader - use-develocity: ${{ env.USE_DEVELOCITY }} - develocity-url: ${{ env.DEVELOCITY_URL }} + url: https://vault.dev.sonar.build + secrets: | + development/artifactory-edge-dev/token/{REPO_OWNER_NAME_DASH}-private-reader username | ARTIFACTORY_USERNAME; + development/artifactory-edge-dev/token/{REPO_OWNER_NAME_DASH}-private-reader access_token | ARTIFACTORY_ACCESS_TOKEN; - name: Install analyzer-wire + env: + ARTIFACTORY_URL: https://repox-internal.dev.sonar.build/artifactory + ARTIFACTORY_USERNAME: ${{ fromJSON(steps.repox.outputs.vault).ARTIFACTORY_USERNAME }} + ARTIFACTORY_ACCESS_TOKEN: ${{ fromJSON(steps.repox.outputs.vault).ARTIFACTORY_ACCESS_TOKEN }} run: bash ./.github/scripts/install-analyzer-wire.sh - uses: SonarSource/ci-github-actions/build-maven@v2 id: build-maven From 01252029521b6886799db6bcb69aa077972a91b7 Mon Sep 17 00:00:00 2001 From: rombirli Date: Fri, 20 Nov 2026 11:26:38 +0100 Subject: [PATCH 5/5] SONARJAVA-7339 Install analyzer-wire before the analysis job resolves dependencies Test Analyze builds with the Maven setup that only sees the sonarsource repository, where analyzer-wire 2.0.0 is not published. --- .github/workflows/build.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5c72dc325d4..fa153eea35e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -361,6 +361,20 @@ jobs: secrets: | development/kv/data/next url | SONAR_HOST_URL; development/kv/data/next token | SONAR_TOKEN; + - name: Read Repox credentials + id: repox + uses: SonarSource/vault-action-wrapper@v3 + with: + url: https://vault.dev.sonar.build + secrets: | + development/artifactory-edge-dev/token/{REPO_OWNER_NAME_DASH}-private-reader username | ARTIFACTORY_USERNAME; + development/artifactory-edge-dev/token/{REPO_OWNER_NAME_DASH}-private-reader access_token | ARTIFACTORY_ACCESS_TOKEN; + - name: Install analyzer-wire + env: + ARTIFACTORY_URL: https://repox-internal.dev.sonar.build/artifactory + ARTIFACTORY_USERNAME: ${{ fromJSON(steps.repox.outputs.vault).ARTIFACTORY_USERNAME }} + ARTIFACTORY_ACCESS_TOKEN: ${{ fromJSON(steps.repox.outputs.vault).ARTIFACTORY_ACCESS_TOKEN }} + run: bash ./.github/scripts/install-analyzer-wire.sh - uses: SonarSource/ci-github-actions/build-maven@v2 with: repox-url: https://repox-internal.dev.sonar.build