Repository navigation
JAVA-7396 Add the SemSitter semantic model of types, symbols and members #6573
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
11
commits into
rombirli/semsitter-parsing-session
from
rombirli/semsitter-semantic-model
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b66e015
JAVA-7396 Add the semantic model of types, symbols and members
rombirli a2b3a87
JAVA-7396 Test the semantic model against recorded JDK types
rombirli e1222c0
JAVA-7396 Read Spring bean and qualifier names only from String annot…
rombirli 44ed098
JAVA-7396 Inline SemaParts into JSema and remove the no-op Environmen…
rombirli 27c3cbd
JAVA-7396 Remove unused stubs, methods and fields from the semantic m…
rombirli 64eb9d3
JAVA-7396 Read project parameter annotations directly from parameterA…
rombirli b500113
JAVA-7396 Narrow visibility of JSema and MemberFactory internals
rombirli 6c29fa9
JAVA-7396 Fix BinaryNames lambda collisions, Javadoc annotation order…
rombirli 54a4a3f
JAVA-7396 Move MemberId parse cache from SemaFacts static state onto …
rombirli 6e0148f
JAVA-7396 Cache type hierarchies, subtype checks and index member loo…
rombirli 39a2cdd
JAVA-7396 Place private methods after public and package-private methods
rombirli 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
79 changes: 79 additions & 0 deletions
79
java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/BinaryNames.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,79 @@ | ||
| /* | ||
| * 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.HashMap; | ||
| import java.util.Map; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** The binary names Java gives the types SemSitter names, and back, for the types of one file. */ | ||
| final class BinaryNames { | ||
|
|
||
| private static final Pattern LAMBDA_SEGMENTS = Pattern.compile("^(?:\\$lambda\\$[^$()]*+\\(\\))++"); | ||
|
|
||
| private final Map<String, String> binaryById = new HashMap<>(); | ||
| private final Map<String, String> idByBinary = new HashMap<>(); | ||
|
|
||
| /** | ||
| * The binary name Java gives the type SemSitter names {@code id}: a local class {@code p.C#f()$Local} becomes | ||
| * {@code p.C$1Local}, numbered by the first index not yet taken in its enclosing class, in declaration order. | ||
| */ | ||
| String binaryName(String id) { | ||
| String known = binaryById.get(id); | ||
| if (known != null) { | ||
| return known; | ||
| } | ||
| int hash = id.lastIndexOf('#'); | ||
| int open = id.indexOf('(', hash); | ||
| int close = open < 0 ? -1 : id.indexOf(')', open); | ||
| String afterMethod = close < 0 ? "" : id.substring(close + 1); | ||
| var lambdaPrefix = LAMBDA_SEGMENTS.matcher(afterMethod); | ||
| int lambdaEnd = lambdaPrefix.find() ? lambdaPrefix.end() : 0; | ||
| String rest = afterMethod.substring(lambdaEnd); | ||
| if (hash < 0 || !rest.startsWith("$") || rest.length() == 1) { | ||
| return id; | ||
| } | ||
| int nested = rest.indexOf('$', 1); | ||
| String name = nested < 0 ? rest.substring(1) : rest.substring(1, nested); | ||
| String localId = id.substring(0, close + 1 + lambdaEnd) + "$" + name; | ||
| String local = binaryById.get(localId); | ||
| if (local == null) { | ||
| String enclosing = binaryName(id.substring(0, hash)); | ||
| int index = 1; | ||
| while (idByBinary.containsKey(enclosing + "$" + index + name)) { | ||
| index++; | ||
| } | ||
| local = enclosing + "$" + index + name; | ||
| binaryById.put(localId, local); | ||
| idByBinary.put(local, localId); | ||
| } | ||
| String binary = nested < 0 ? local : (local + rest.substring(nested)); | ||
| binaryById.put(id, binary); | ||
| idByBinary.putIfAbsent(binary, id); | ||
| return binary; | ||
| } | ||
|
|
||
| /** The binary name already given to {@code name}, or {@code name} itself. */ | ||
| String binaryOf(String name) { | ||
| return binaryById.getOrDefault(name, name); | ||
| } | ||
|
|
||
| /** The SemSitter name of the type Java names {@code fqn}, or {@code fqn} itself. */ | ||
| String sourceName(String fqn) { | ||
| return idByBinary.getOrDefault(fqn, fqn); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/DeclaredAnnotation.java
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
One annotation read from SemSitter: its erased type name, its element values, and whether those values were read. readAnnotationList(["lib.Marker"])
// DeclaredAnnotation("lib.Marker", [], true)
readAnnotationList([{"name": "lib.Timeout", "values": [{"name": "seconds", "kind": "int", "value": 2}]}])
// DeclaredAnnotation("lib.Timeout", [seconds=2], true)
declaredAnnotations("p.A") // graph fact "lib.Marker`1"
// DeclaredAnnotation("lib.Marker", [], false)
|
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,23 @@ | ||
| /* | ||
| * 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 org.sonar.plugins.java.api.semantic.SymbolMetadata; | ||
|
|
||
| record DeclaredAnnotation(String name, List<SymbolMetadata.AnnotationValue> values) { | ||
| } |
34 changes: 34 additions & 0 deletions
34
java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/DeclaredMembers.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,34 @@ | ||
| /* | ||
| * 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.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.sonarsource.semsitter.wire.Fact; | ||
|
|
||
| /** What the {@code types} answers declare about members and packages, by symbol id, for one {@link JSema}. */ | ||
| final class DeclaredMembers { | ||
| final Map<String, List<DeclaredAnnotation>> libraryAnnotations = new HashMap<>(); | ||
| final Map<String, List<String>> projectThrows = new HashMap<>(); | ||
| final Map<String, Fact> projectMemberFacts = new HashMap<>(); | ||
| final Map<String, Boolean> projectVarArgs = new HashMap<>(); | ||
| final Map<String, JsonObject> projectFieldTypes = new HashMap<>(); | ||
| final Map<String, String> projectConstants = new HashMap<>(); | ||
| final Map<String, Object> annotationDefaults = new HashMap<>(); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BinaryNamesin shortjava-frontend-semsitter/src/main/java/org/sonar/java/semsitter/BinaryNames.javamaps SemSitter type ids to the binary names Java gives local classes, and back.binaryName(id)numbers local classes: the first free$kin the enclosing class wins.binaryOf(name)returns the binary name already given, or the name itself.sourceName(fqn)goes back to the id.JSemaandMemberLookupuse it to readtypeInfos.Possible issue:
idByBinarykeeps only the last id registered for a binary name. After the lambda id above is registered,sourceName("p.C$1Local")returnsp.C#f()$lambda$0()$Local, notp.C#f()$Local. Can SemSitter emit lambda-qualified ids for local classes? IftypeInfosis keyed by the plain id, that lookup misses. Worth a test.