Skip to content

Commit 3924b47

Browse files
committed
SONARJAVA-7236 Keep symbols on the AST after it stopped carrying Eclipse bindings
1 parent 68f77c7 commit 3924b47

22 files changed

Lines changed: 393 additions & 361 deletions

‎java-frontend/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
</properties>
1919

2020
<dependencies>
21+
<dependency>
22+
<groupId>${project.groupId}</groupId>
23+
<artifactId>jdt-package</artifactId>
24+
<version>1.9.0.1840</version>
25+
<scope>test</scope>
26+
</dependency>
2127
<dependency>
2228
<groupId>org.sonarsource.api.plugin</groupId>
2329
<artifactId>sonar-plugin-api</artifactId>

‎java-frontend/src/main/java/org/sonar/java/model/AbstractTypedTree.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.sonar.java.model;
1818

19+
import java.util.Objects;
1920
import javax.annotation.Nullable;
2021
import org.sonar.plugins.java.api.semantic.Type;
2122

@@ -27,6 +28,10 @@ public abstract class AbstractTypedTree extends JavaTree {
2728
@Nullable
2829
public Type type;
2930

31+
public void setSymbolType(Type symbolType) {
32+
this.type = Objects.requireNonNull(symbolType);
33+
}
34+
3035
public Type symbolType() {
3136
return type != null ? type : Type.UNKNOWN;
3237
}

‎java-frontend/src/main/java/org/sonar/java/model/CompilerWarnings.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private CompilerWarnings(JSema sema) {
4949
}
5050

5151
static void addTo(JavaTree.CompilationUnitTreeImpl unit) {
52-
CompilerWarnings visitor = new CompilerWarnings(unit.sema);
52+
CompilerWarnings visitor = new CompilerWarnings((JSema) unit.sema);
5353
visitor.scan(unit.packageDeclaration());
5454
visitor.scan(unit.types());
5555
unit.imports().forEach(visitor::scan);

‎java-frontend/src/main/java/org/sonar/java/model/JavaTree.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public static class ImportTreeImpl extends JavaTree implements ImportTree {
340340
private final SyntaxToken moduleToken;
341341

342342
@Nullable
343-
public Symbol symbol;
343+
private Symbol symbol;
344344

345345
public ImportTreeImpl(InternalSyntaxToken importToken, @Nullable InternalSyntaxToken staticToken,
346346
@Nullable InternalSyntaxToken moduleToken,
@@ -359,6 +359,10 @@ public Symbol symbol() {
359359
return symbol;
360360
}
361361

362+
public void setSymbol(@Nullable Symbol symbol) {
363+
this.symbol = symbol;
364+
}
365+
362366
@Override
363367
public Kind kind() {
364368
return Kind.IMPORT;

‎java-frontend/src/main/java/org/sonar/java/model/KeywordSuper.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ final class KeywordSuper extends IdentifierTreeImpl {
2626
super(token);
2727
}
2828

29+
KeywordSuper(InternalSyntaxToken token, Symbol symbol) {
30+
this(token);
31+
setSymbol(symbol);
32+
}
33+
2934
@Override
3035
public Type symbolType() {
3136
return symbol().type();

‎java-frontend/src/main/java/org/sonar/java/model/KeywordThis.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ final class KeywordThis extends IdentifierTreeImpl {
2525
super(token);
2626
}
2727

28+
KeywordThis(InternalSyntaxToken token, Symbol symbol) {
29+
this(token);
30+
setSymbol(symbol);
31+
}
32+
2833
@Override
2934
public Symbol symbol() {
3035
return symbol != null ? symbol : Symbol.UNKNOWN_SYMBOL;

‎java-frontend/src/main/java/org/sonar/java/model/SemsitterTranslator.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static JavaTree.CompilationUnitTreeImpl translate(JSema sema, JsonObject documen
174174
translator.indexTokens(root);
175175
translator.declareGenericReturns(root);
176176
JavaTree.CompilationUnitTreeImpl unit = translator.compilationUnit(root);
177-
unit.sema = sema;
177+
unit.setSemanticModel(sema, sema.undefinedTypes(), sema.getEnvironmentCleaner());
178178
setParents(unit);
179179
CompilerWarnings.addTo(unit);
180180
EffectivelyFinalVariables.mark(unit);
@@ -411,7 +411,7 @@ private JavaTree.ImportTreeImpl importDeclaration(Node node) {
411411
if (staticToken == null && name instanceof MemberSelectExpressionTree select && !"*".equals(select.identifier().name())) {
412412
String fqn = dottedName(name);
413413
if (sema.declaresType(fqn) && sema.resolve(fqn) instanceof Symbol.TypeSymbol type) {
414-
imported.symbol = type;
414+
imported.setSymbol(type);
415415
}
416416
}
417417
return imported;

‎java-frontend/src/main/java/org/sonar/java/model/TypeParameterTreeImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public List<Tree> children() {
9090
}
9191

9292
public void setSymbol(Symbol symbol) {
93-
this.symbol = Objects.requireNonNull(symbol);
93+
this.typeSymbol = (Symbol.TypeSymbol) Objects.requireNonNull(symbol);
9494
}
9595

9696
public Symbol symbol() {

‎java-frontend/src/main/java/org/sonar/java/model/declaration/ClassTreeImpl.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.sonar.java.ast.parser.QualifiedIdentifierListTreeImpl;
2020
import org.sonar.java.ast.parser.TypeParameterListTreeImpl;
21+
import org.sonarsource.analyzer.commons.collections.ListUtils;
2122
import org.sonar.java.model.InternalSyntaxToken;
2223
import org.sonar.java.model.JavaTree;
2324
import org.sonar.java.model.expression.IdentifierTreeImpl;
@@ -33,7 +34,11 @@
3334
import org.sonar.plugins.java.api.tree.TypeParameters;
3435
import org.sonar.plugins.java.api.tree.TypeTree;
3536
import org.sonar.plugins.java.api.tree.VariableTree;
36-
import org.sonarsource.analyzer.commons.collections.ListUtils;
37+
38+
import javax.annotation.Nullable;
39+
import java.util.Collections;
40+
import java.util.List;
41+
import java.util.Objects;
3742

3843
public class ClassTreeImpl extends JavaTree implements ClassTree {
3944

@@ -212,7 +217,7 @@ public SyntaxToken closeBraceToken() {
212217
}
213218

214219
public void setSymbol(Symbol.TypeSymbol symbol) {
215-
this.symbol = Objects.requireNonNull(symbol);
220+
this.typeSymbol = Objects.requireNonNull(symbol);
216221
}
217222

218223
@Override

‎java-frontend/src/main/java/org/sonar/java/model/declaration/MethodTreeImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public ExpressionTree defaultValue() {
216216
}
217217

218218
public void setSymbol(Symbol.MethodSymbol symbol) {
219-
this.symbol = Objects.requireNonNull(symbol);
219+
this.methodSymbol = Objects.requireNonNull(symbol);
220220
}
221221

222222
@Override

0 commit comments

Comments
 (0)