Repository navigation
JAVA-7395 Run a SemSitter parsing session #6569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rombirli
wants to merge
2
commits into
rombirli/semsitter-protocol-client
from
rombirli/semsitter-parsing-session
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/DocumentTranslator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
| /** 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); | ||
| } | ||
| } | ||
105 changes: 105 additions & 0 deletions
105
java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/ParseProgress.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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."); | ||
| } | ||
| } | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/ProjectRoot.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
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('\\', '/'); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.