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
5 changes: 5 additions & 0 deletions java-frontend-semsitter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<artifactId>sensor-test-fixtures</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.api.plugin</groupId>
<artifactId>sonar-plugin-api-test-fixtures</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.JsonObject;
import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.java.model.JParseResult;
import org.sonarsource.semsitter.wire.Document;

/**
* Turns the document a SemSitter server answered for one file into the tree the checks run on.
*/
@FunctionalInterface
public interface DocumentTranslator {

JParseResult translate(Document document, ServerLoaders loaders, boolean noLibraries, int javaVersion);
Comment thread
rombirli marked this conversation as resolved.
Resolved

/** What the server answers while the session is open. Both answers are null once it is closed. */
interface ServerLoaders {

/** The declarations of the named types. */
@Nullable
JsonObject types(Set<String> names);

/** Whether the package {@code name} exists. */
@Nullable
Boolean hasPackage(String name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.util.List;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.java.AnalysisProgress;
import org.sonar.java.model.JParser;
import org.sonarsource.analyzer.commons.ProgressReport;

/**
* Progress of the files one call of {@link SemSitterParser#parse} goes through. File by file it is a
* {@link ProgressReport} over these files. In batch mode the calls are the batches of one analysis, so, like the
* default frontend, it logs the start on the first batch, the end on the last one, and in between the share of the
* whole analysis that is done.
*/
abstract class ParseProgress {

static final long PERIOD_MILLIS = TimeUnit.SECONDS.toMillis(10);

abstract void nextFile();

abstract void end(boolean completed);

static ParseProgress start(JParser.Mode mode, List<InputFile> files, AnalysisProgress analysisProgress) {
return mode == JParser.Mode.BATCH ? new Batch(analysisProgress, files.size(), PERIOD_MILLIS) : new FileByFile(files);
}

private static final class FileByFile extends ParseProgress {
private final ProgressReport report = new ProgressReport("Report about progress of Java AST analyzer", PERIOD_MILLIS);

private FileByFile(List<InputFile> files) {
report.start(files.stream().map(InputFile::toString).toList());
}

@Override
void nextFile() {
report.nextFile();
}

@Override
void end(boolean completed) {
if (completed) {
report.stop();
} else {
report.cancel();
}
}
}

static final class Batch extends ParseProgress {
private static final Logger LOG = LoggerFactory.getLogger(Batch.class);

private final AnalysisProgress analysisProgress;
private final int files;
private final long periodNanos;
private int done;
private long lastLog;

Batch(AnalysisProgress analysisProgress, int files, long periodMillis) {
this.analysisProgress = analysisProgress;
this.files = files;
this.periodNanos = TimeUnit.MILLISECONDS.toNanos(periodMillis);
this.lastLog = System.nanoTime();
if (analysisProgress.isFirstBatch()) {
LOG.info("Starting batch processing.");
}
}

@Override
void nextFile() {
done++;
long now = System.nanoTime();
if (now - lastLog >= periodNanos) {
lastLog = now;
LOG.info("{}% analyzed", (int) (analysisProgress.toGlobalPercentage(done / (double) files) * 100));
}
}

@Override
void end(boolean completed) {
if (completed && analysisProgress.isLastBatch()) {
LOG.info("100% analyzed");
LOG.info("Batch processing: Done.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.nio.file.Path;
import java.util.Collection;
import java.util.Iterator;
import java.util.Optional;
import javax.annotation.Nullable;

/**
* The directory a SemSitter server is started on.
*/
final class ProjectRoot {

private ProjectRoot() {
}

/**
* @return the deepest directory that contains every file, none when there is no file or the files share no directory,
* like files on two Windows drives
*/
static Optional<Path> commonAncestor(Collection<Path> files) {
Iterator<Path> paths = files.iterator();
if (!paths.hasNext()) {
return Optional.empty();
}
Path common = directory(paths.next());
while (common != null && paths.hasNext()) {
common = shared(common, directory(paths.next()));
}
return Optional.ofNullable(common);
}

private static Path directory(Path file) {
return file.toAbsolutePath().normalize().getParent();
}

@Nullable
private static Path shared(Path first, Path second) {
Path candidate = first;
while (candidate != null && !second.startsWith(candidate)) {
candidate = candidate.getParent();
}
return candidate;
}
Comment thread
gitar-bot[bot] marked this conversation as resolved.
Resolved

/**
* @return the path of {@code file} below {@code root}, written with {@code /} on every platform, as the server names files
*/
static String relative(Path root, Path file) {
return root.relativize(file.toAbsolutePath().normalize()).toString().replace('\\', '/');
}
}
Loading