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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

BinaryNames in short

java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/BinaryNames.java maps SemSitter type ids to the binary names Java gives local classes, and back.

flowchart LR
  a["p.C#f()$Local"] --> A["p.C$1Local"]
  b["p.C#g()$Local"] --> B["p.C$2Local"]
  c["p.C#f()$A#g()$B"] --> C["p.C$1A$1B"]
  d["p.C#f()$lambda$0()$Local"] -->|lambda segment stripped| A
Loading
  • binaryName(id) numbers local classes: the first free $k in the enclosing class wins.
  • binaryOf(name) returns the binary name already given, or the name itself.
  • sourceName(fqn) goes back to the id. JSema and MemberLookup use it to read typeInfos.

Possible issue: idByBinary keeps only the last id registered for a binary name. After the lambda id above is registered, sourceName("p.C$1Local") returns p.C#f()$lambda$0()$Local, not p.C#f()$Local. Can SemSitter emit lambda-qualified ids for local classes? If typeInfos is keyed by the plain id, that lookup misses. Worth a test.

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);
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

DeclaredAnnotation in short

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)

SemaAnnotations builds it from JSON annotation lists, parameter annotations and graph facts.

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) {
}
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<>();
}
Loading