Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions java-frontend-semsitter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.sonarsource.java</groupId>
<artifactId>java</artifactId>
<version>9.1.0-SNAPSHOT</version>
</parent>

<artifactId>java-frontend-semsitter</artifactId>

<name>SonarQube Java :: SemSitter Front-end</name>
<description>Code Analyzer for Java :: Front-end based on SemSitter</description>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>java-frontend</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.sonarsource.api.plugin</groupId>
<artifactId>sonar-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.semsitter</groupId>
<artifactId>analyzer-wire</artifactId>
<version>[2,3-alpha)</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.sonarsource.scanner.engine</groupId>
<artifactId>plugin-api-scanner-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.scanner.engine</groupId>
<artifactId>sensor-test-fixtures</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.sonarsource.semsitter.wire.Document;

/**
* What the SemSitter frontend asks of a running SemSitter server. {@link SemSitterClient} answers it over a process.
*/
public interface SemSitterApi extends AutoCloseable {

void setSettings(Map<String, List<String>> settings) throws IOException;

int fullGen(Collection<String> files) throws IOException;

Document getFile(String path, int ahead) throws IOException;

/**
* @return the JSON object of declarations, as {@code types.schema.json} describes it
*/
byte[] getTypes(Collection<String> names) throws IOException;

Map<String, Boolean> hasPackage(Collection<String> names) throws IOException;

@Override
void close();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
* 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.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.protobuf.CodedInputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.sonarsource.semsitter.wire.Document;

/**
* Client of one SemSitter Java analyzer server process. Control messages are JSON lines. A {@code get-file} answer
* carries a {@link Document} and a {@code get-types} answer carries a JSON object, each prefixed by the byte count
* the header names. The process starts with the client and ends with {@link #close()}.
*/
public final class SemSitterClient implements SemSitterApi {

/** Deeper documents are rejected with an {@link IOException} before parsing them could overflow the stack. */
static final int MAX_TREE_DEPTH = 1_000;

private static final List<String> SERVER_FLAGS = List.of("--server", "--lang", "java", "--modifiers", "--structural",
"--decl-location", "--field-items", "--field-reference-edges", "--override-edges");

private final Process process;
private final BufferedWriter toServer;
private final InputStream fromServer;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fromServer InputStream is never closed. Could we explicitly close it in the close() method maybe?

private int nextId = 1;

private SemSitterClient(Process process) {
this.process = process;
this.toServer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8));
this.fromServer = process.getInputStream();
}

/**
* Starts the server on the sources of {@code projectRoot} and waits for it to answer {@code hello}.
*/
public static SemSitterClient start(Path executable, Path projectRoot) throws IOException {
return start(command(List.of(executable.toAbsolutePath().toString()), projectRoot));
}

static SemSitterClient start(List<String> command) throws IOException {
Process process = new ProcessBuilder(command).redirectError(ProcessBuilder.Redirect.INHERIT).start();
SemSitterClient client = new SemSitterClient(process);
try {
client.send(request("hello"));
} catch (IOException | RuntimeException e) {
client.close();
throw e;
}
return client;
}

static List<String> command(List<String> launcher, Path projectRoot) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could we rename this method to buildServerCommand? That would make it clearer what the returned arguments are for.

String root = projectRoot.toAbsolutePath().toString();
List<String> command = new ArrayList<>(launcher);
command.addAll(SERVER_FLAGS);
command.addAll(List.of("--src", root, "--project-root", root));
return command;
}

@Override
public void setSettings(Map<String, List<String>> settings) throws IOException {
JsonObject request = request("set-settings");
JsonObject values = new JsonObject();
settings.forEach((key, list) -> {
JsonArray array = new JsonArray();
list.forEach(array::add);
values.add(key, array);
});
Comment on lines +93 to +98

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
JsonObject values = new JsonObject();
settings.forEach((key, list) -> {
JsonArray array = new JsonArray();
list.forEach(array::add);
values.add(key, array);
});
var gson = new Gson();
gson.toJsonTree(settings);

Here could we use the suggested Gson library to transform the map into a JsonElement? I think that would avoid boilerplate code and make it easier to understand the implementation.

The same logic could be applied to the other methods in the class that creates JsonElement.

request.add("settings", values);
send(request);
}

/**
* Scans {@code files}, or the source roots when empty.
* @return how many files the server scanned
*/
@Override
public int fullGen(Collection<String> files) throws IOException {
JsonObject request = request("full-gen");
if (!files.isEmpty()) {
JsonArray array = new JsonArray();
files.forEach(array::add);
request.add("files", array);
}
return send(request).get("files").getAsInt();
}

@Override
public Document getFile(String path, int ahead) throws IOException {
JsonObject request = request("get-file");
request.addProperty("path", path);
request.addProperty("ahead", ahead);
CodedInputStream input = CodedInputStream.newInstance(exchange(request).payload);
Comment thread
gitar-bot[bot] marked this conversation as resolved.
Resolved
input.setRecursionLimit(MAX_TREE_DEPTH);
return Document.parseFrom(input);
Comment thread
gitar-bot[bot] marked this conversation as resolved.
Resolved
}

/**
* @return the JSON object of declarations, as {@code types.schema.json} describes it
*/
@Override
public byte[] getTypes(Collection<String> names) throws IOException {
JsonObject request = request("get-types");
JsonArray array = new JsonArray();
names.forEach(array::add);
request.add("names", array);
return exchange(request).payload;
}

@Override
public Map<String, Boolean> hasPackage(Collection<String> names) throws IOException {
JsonObject request = request("has-package");
JsonArray array = new JsonArray();
names.forEach(array::add);
request.add("names", array);
Map<String, Boolean> found = new LinkedHashMap<>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Is preserving the server’s response order intentional here? If callers aren’t meant to rely on it, could we use a simple HashMap?

for (Map.Entry<String, JsonElement> entry : send(request).getAsJsonObject("packages").entrySet()) {
found.put(entry.getKey(), entry.getValue().getAsBoolean());
}
return found;
}

private static JsonObject request(String name) {
JsonObject request = new JsonObject();
request.addProperty("cmd", name);
return request;
}

private JsonObject send(JsonObject request) throws IOException {
return exchange(request).header;
}

private int write(JsonObject request) throws IOException {
int id = nextId;
nextId++;
request.addProperty("id", id);
toServer.write(request.toString());
toServer.write('\n');
toServer.flush();
return id;
}

private synchronized Answer exchange(JsonObject request) throws IOException {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can exchange and quit be called from different threads? It looks like the usage is sequential so I'm wondering if there is a concurrent case where the synchronized is required or if it could be dropped?

try {
int id = write(request);
String text = readLine(fromServer);
JsonObject header = JsonParser.parseString(text).getAsJsonObject();
byte[] payload = new byte[0];
if (header.has("payload_bytes")) {
int expected = header.get("payload_bytes").getAsInt();
payload = fromServer.readNBytes(expected);
if (payload.length != expected) {
throw new IOException("SemSitter server sent " + payload.length + " of " + expected + " payload bytes");
}
}
Comment thread
gitar-bot[bot] marked this conversation as resolved.
Resolved
if (header.get("id").getAsInt() != id) {
throw new IOException("SemSitter server answered request " + header.get("id") + " instead of " + id);
}
if (!header.get("ok").getAsBoolean()) {
throw new IOException("SemSitter server: " + header.get("error").getAsString());
Comment thread
rombirli marked this conversation as resolved.
Resolved
}
return new Answer(header, payload);
} catch (IOException e) {
String message = e.getMessage().startsWith("SemSitter") ? e.getMessage() : ("SemSitter server: " + e.getMessage());
throw new IOException(message, e);
} catch (RuntimeException e) {
throw new IOException("SemSitter server sent a malformed answer: " + e.getMessage(), e);
}
}

private synchronized void quit() throws IOException {
write(request("quit"));
toServer.close();
}

/**
* Asks the server to quit without waiting for its answer, then waits for it to exit and forces it to stop when it
* does not.
*/
@Override
public void close() {
try {
if (process.isAlive()) {
quit();
if (!process.waitFor(10, TimeUnit.SECONDS)) {
process.destroyForcibly();
}
}
} catch (IOException e) {
process.destroyForcibly();
Comment thread
rombirli marked this conversation as resolved.
Resolved
} catch (InterruptedException e) {
process.destroyForcibly();
Thread.currentThread().interrupt();
}
}

private static String readLine(InputStream in) throws IOException {
ByteArrayOutputStream line = new ByteArrayOutputStream();
for (int b = in.read(); b != '\n'; b = in.read()) {
if (b < 0) {
throw new IOException("SemSitter server closed its output");
}
line.write(b);
}
return line.toString(StandardCharsets.UTF_8);
}

private static final class Answer {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could Answer be transformed into a record? It just carries header and payload.

private final JsonObject header;
private final byte[] payload;

private Answer(JsonObject header, byte[] payload) {
this.header = header;
this.payload = payload;
}
}
}
Loading