Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.sonar.java.checks.verifier.CheckVerifier;
import org.sonar.java.checks.verifier.TestUtils;
import org.sonar.java.model.declaration.VariableTreeImpl;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.VariableTree;

Expand Down Expand Up @@ -64,7 +65,7 @@ private static class EraseSymbols extends BaseTreeVisitor {
@Override
public void visitVariable(VariableTree tree) {
if (tree instanceof VariableTreeImpl varImpl) {
varImpl.variableBinding = null;
varImpl.setSymbol(Symbol.UNKNOWN_SYMBOL);
}
super.visitVariable(tree);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private static void scanAsBatchCallback(InputFile inputFile, JParserConfig.Resul
Duration duration = PerformanceMeasure.start(context.descriptor(inputFile));
scanner.simpleScan(inputFile, result, ast ->
// In batch mode, we delay the cleaning of the environment as it will be used in later processing.
environmentsCleaners.add(ast.sema.getEnvironmentCleaner())
environmentsCleaners.add(ast.getEnvironmentCleaner())
);
duration.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public void scanForTesting(Iterable<? extends InputFile> inputFiles, Consumer<Co
scan(inputFiles, modifyCompilationUnit);
}


private void scan(Iterable<? extends InputFile> inputFiles, Consumer<CompilationUnitTree> modifyCompilationUnit) {
List<? extends InputFile> filesNames = filterModuleInfo(inputFiles).toList();
AnalysisProgress analysisProgress = new AnalysisProgress(filesNames.size());
Expand Down Expand Up @@ -166,7 +165,7 @@ public void simpleScan(InputFile inputFile, JParserConfig.Result result, Consume
modifyCompilationUnit.accept(ast);
visitor.visitFile(ast, sonarComponents != null && sonarComponents.fileCanBeSkipped(inputFile));
String path = inputFile.toString();
Set<JProblem> undefinedTypes = ast.sema.undefinedTypes();
Set<JProblem> undefinedTypes = ast.undefinedTypes();
collectUndefinedTypes(path, undefinedTypes);
cleanUp.accept(ast);
telemetryAnalysisKeys = javaAnalysisKeys.success();
Expand Down Expand Up @@ -196,7 +195,7 @@ public void simpleScan(InputFile inputFile, JParserConfig.Result result, Consume

private static void cleanUpAst(JavaTree.CompilationUnitTreeImpl ast) {
// release environment used for semantic resolution
ast.sema.getEnvironmentCleaner().run();
ast.getEnvironmentCleaner().run();
}

private void collectUndefinedTypes(String path, Set<JProblem> undefinedTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,22 @@
*/
package org.sonar.java.model;

import org.eclipse.jdt.core.dom.ITypeBinding;
import java.util.Objects;
import org.sonar.plugins.java.api.semantic.Type;

import javax.annotation.Nullable;

/**
* This class is intended for internal use during semantic analysis and should not be used in checks.
*/
public abstract class AbstractTypedTree extends JavaTree {

@Nullable
public ITypeBinding typeBinding;
private Type symbolType = Type.UNKNOWN;

public void setSymbolType(Type symbolType) {
this.symbolType = Objects.requireNonNull(symbolType);
}

public Type symbolType() {
return typeBinding != null
? root.sema.type(typeBinding)
: Type.UNKNOWN;
return symbolType;
}

}
235 changes: 129 additions & 106 deletions java-frontend/src/main/java/org/sonar/java/model/JParser.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This file is quite big and it's migration is not complete in this PR (usages of ITypeBinding, IMethodBinding...), it might be easier to migrate this file in it's own PR. It could be good as well to split this 3k LoC file to a "parser" module containing small java files (<300LoC) to make it more maintainable.

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.

This file corresponds to the implementation of parsing with ECJ, so it is expected that it contains references to IBinding and other ECJ specific types.
Yes, the goal in this PR is only to remove ECJ types from AST nodes in the Sonar Java API. Another PR is planned to move JParser and other ECJ specific J* classes behind implementation agnostic interfaces and into a dedicated package.

Large diffs are not rendered by default.

52 changes: 45 additions & 7 deletions java-frontend/src/main/java/org/sonar/java/model/JSema.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTUtils;
Expand All @@ -41,7 +39,6 @@
public final class JSema implements Sema {

private final AST ast;
final Set<JProblem> undefinedTypes = new HashSet<>();
final Map<IBinding, Tree> declarations = new HashMap<>();
final Map<IBinding, List<IdentifierTree>> usages = new HashMap<>();
private final Map<ITypeBinding, JType> types = new HashMap<>();
Expand All @@ -55,6 +52,51 @@ public final class JSema implements Sema {
this.ast = ast;
}

Type typeOrUnknown(@Nullable ITypeBinding binding) {
return binding == null ? Type.UNKNOWN : type(binding);
}

Symbol.TypeSymbol typeSymbolOrUnknown(@Nullable ITypeBinding binding) {
return binding == null ? Symbol.TypeSymbol.UNKNOWN_TYPE : typeSymbol(binding);
}

Symbol.MethodSymbol methodSymbolOrUnknown(@Nullable IMethodBinding binding) {
return binding == null ? Symbol.MethodSymbol.UNKNOWN_METHOD : methodSymbol(binding);
}

Symbol variableSymbolOrUnknown(@Nullable IVariableBinding binding) {
return binding == null ? Symbol.UNKNOWN_SYMBOL : variableSymbol(binding);
}

Symbol symbolOrUnknown(@Nullable IBinding binding) {
if (binding == null) {
return Symbol.UNKNOWN_SYMBOL;
}
return switch (binding.getKind()) {
case IBinding.TYPE -> typeSymbol((ITypeBinding) binding);
case IBinding.METHOD -> methodSymbol((IMethodBinding) binding);
case IBinding.VARIABLE -> variableSymbol((IVariableBinding) binding);
case IBinding.PACKAGE -> packageSymbol((IPackageBinding) binding);
default -> Symbol.UNKNOWN_SYMBOL;
};
}

@Nullable
Symbol importSymbolOrUnknown(@Nullable IBinding binding) {
if (binding == null || (binding.getKind() != IBinding.TYPE && binding.getKind() != IBinding.METHOD && binding.getKind() != IBinding.VARIABLE)) {
return null;
}
return symbolOrUnknown(binding);
}

Symbol thisSymbolOrUnknown(@Nullable ITypeBinding binding) {
return binding == null ? Symbol.UNKNOWN_SYMBOL : typeSymbol(binding).thisSymbol;
}

Symbol superSymbolOrUnknown(@Nullable ITypeBinding binding) {
return binding == null ? Symbol.UNKNOWN_SYMBOL : typeSymbol(binding).superSymbol;
}

public JType type(ITypeBinding typeBinding) {
JType cached = types.get(typeBinding);
if (cached != null) {
Expand Down Expand Up @@ -117,7 +159,6 @@ public JInitializerBlockSymbol staticInitializerBlockSymbol(JTypeSymbol owner) {
return staticInitializerBlockSymbols.computeIfAbsent(owner, k -> new JInitializerBlockSymbol(k, true));
}


public JVariableSymbol variableSymbol(IVariableBinding variableBinding) {
JVariableSymbol cached = (JVariableSymbol) symbols.get(variableBinding);
if (cached != null) {
Expand Down Expand Up @@ -187,7 +228,4 @@ public Runnable getEnvironmentCleaner() {
return ASTUtils.getEnvironmentCleaner(ast);
}

public Set<JProblem> undefinedTypes() {
return Collections.unmodifiableSet(undefinedTypes);
}
}
4 changes: 2 additions & 2 deletions java-frontend/src/main/java/org/sonar/java/model/JSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private Symbol ownerOfRecordComponentConstant(IVariableBinding variableBinding)
case CLASS,
RECORD,
ENUM:
JTypeSymbol typeSymbol = sema.typeSymbol(((ClassTreeImpl) node).typeBinding);
JTypeSymbol typeSymbol = (JTypeSymbol) ((ClassTreeImpl) node).symbol();
if (initializerBlock) {
return sema.initializerBlockSymbol(typeSymbol);
}
Expand Down Expand Up @@ -460,7 +460,7 @@ private TypeSymbol variableEnclosingClass(IVariableBinding variableBinding) {
ENUM:
// variable declaration in a static or instance initializer
// or local variable declaration in recovered method
return sema.typeSymbol(((ClassTreeImpl) node).typeBinding);
return ((ClassTreeImpl) node).symbol();
default:
// continue exploring parent
break;
Expand Down
42 changes: 25 additions & 17 deletions java-frontend/src/main/java/org/sonar/java/model/JavaTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@
import java.util.Objects;
import java.util.Set;
import javax.annotation.Nullable;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.sonar.java.Preconditions;
import org.sonar.java.annotations.Beta;
import org.sonar.java.ast.parser.QualifiedIdentifierListTreeImpl;
import org.sonar.java.model.declaration.AnnotationTreeImpl;
import org.sonar.java.model.expression.AssessableExpressionTree;
import org.sonar.java.model.expression.TypeArgumentListTreeImpl;
import org.sonar.plugins.java.api.semantic.Sema;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.AnnotationTree;
import org.sonar.plugins.java.api.tree.ArrayTypeTree;
Expand Down Expand Up @@ -164,7 +161,9 @@ public static class CompilationUnitTreeImpl extends JavaTree implements Compilat
@Nullable
private final ModuleDeclarationTree moduleDeclaration;
private final SyntaxToken eofToken;
public JSema sema;
public Sema sema;
private Set<JProblem> undefinedTypes = Set.of();
private Runnable environmentCleaner = () -> {};

private final Map<JProblem.Type, Set<JWarning>> warnings = new EnumMap<>(JProblem.Type.class);

Expand All @@ -178,6 +177,20 @@ public CompilationUnitTreeImpl(@Nullable PackageDeclarationTree packageDeclarati
this.eofToken = eofToken;
}

public void setSemanticModel(Sema sema, Set<JProblem> undefinedTypes, Runnable environmentCleaner) {
this.sema = Objects.requireNonNull(sema);
this.undefinedTypes = Set.copyOf(undefinedTypes);
this.environmentCleaner = Objects.requireNonNull(environmentCleaner);
}

public Set<JProblem> undefinedTypes() {
return undefinedTypes;
}

public Runnable getEnvironmentCleaner() {
return environmentCleaner;
}

@Override
public Kind kind() {
return Kind.COMPILATION_UNIT;
Expand Down Expand Up @@ -326,7 +339,8 @@ public static class ImportTreeImpl extends JavaTree implements ImportTree {
@Nullable
private final SyntaxToken moduleToken;

public IBinding binding;
@Nullable
private Symbol symbol;

public ImportTreeImpl(InternalSyntaxToken importToken, @Nullable InternalSyntaxToken staticToken,
@Nullable InternalSyntaxToken moduleToken,
Expand All @@ -342,17 +356,11 @@ public ImportTreeImpl(InternalSyntaxToken importToken, @Nullable InternalSyntaxT

@Nullable
public Symbol symbol() {
if (binding != null) {
switch (binding.getKind()) {
case IBinding.TYPE:
return root.sema.typeSymbol((ITypeBinding) binding);
case IBinding.METHOD:
return root.sema.methodSymbol((IMethodBinding) binding);
case IBinding.VARIABLE:
return root.sema.variableSymbol((IVariableBinding) binding);
}
}
return null;
return symbol;
}

public void setSymbol(@Nullable Symbol symbol) {
this.symbol = symbol;
}

@Override
Expand Down
15 changes: 2 additions & 13 deletions java-frontend/src/main/java/org/sonar/java/model/KeywordSuper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,20 @@
*/
package org.sonar.java.model;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.sonar.java.model.expression.IdentifierTreeImpl;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.Type;

import javax.annotation.Nullable;

final class KeywordSuper extends IdentifierTreeImpl {

KeywordSuper(InternalSyntaxToken token, @Nullable ITypeBinding typeBinding) {
KeywordSuper(InternalSyntaxToken token, Symbol symbol) {
super(token);
this.typeBinding = typeBinding;
setSymbol(symbol);
}

@Override
public Type symbolType() {
return symbol().type();
}

@Override
public Symbol symbol() {
if (typeBinding == null) {
return Symbol.UNKNOWN_SYMBOL;
}
return root.sema.typeSymbol(typeBinding).superSymbol;
}

}
15 changes: 2 additions & 13 deletions java-frontend/src/main/java/org/sonar/java/model/KeywordThis.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,14 @@
*/
package org.sonar.java.model;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.sonar.java.model.expression.IdentifierTreeImpl;
import org.sonar.plugins.java.api.semantic.Symbol;

import javax.annotation.Nullable;

final class KeywordThis extends IdentifierTreeImpl {

KeywordThis(InternalSyntaxToken token, @Nullable ITypeBinding typeBinding) {
KeywordThis(InternalSyntaxToken token, Symbol symbol) {
super(token);
this.typeBinding = typeBinding;
}

@Override
public Symbol symbol() {
if (typeBinding == null) {
return Symbol.UNKNOWN_SYMBOL;
}
return root.sema.typeSymbol(typeBinding).thisSymbol;
setSymbol(symbol);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.sonar.java.ast.parser.QualifiedIdentifierListTreeImpl;
import org.sonar.java.model.expression.IdentifierTreeImpl;
import org.sonar.plugins.java.api.semantic.Symbol;
Expand All @@ -39,8 +39,7 @@ public class TypeParameterTreeImpl extends JavaTree implements TypeParameterTree
private final SyntaxToken extendsToken;
private final QualifiedIdentifierListTreeImpl bounds;

@Nullable
ITypeBinding typeBinding;
private Symbol symbol = Symbol.UNKNOWN_SYMBOL;

public TypeParameterTreeImpl(IdentifierTreeImpl identifier) {
this(identifier, null, QualifiedIdentifierListTreeImpl.emptyList());
Expand Down Expand Up @@ -89,9 +88,11 @@ public List<Tree> children() {
return Collections.unmodifiableList(list);
}

public void setSymbol(Symbol symbol) {
this.symbol = Objects.requireNonNull(symbol);
}

public Symbol symbol() {
return typeBinding != null
? root.sema.typeSymbol(typeBinding)
: Symbol.UNKNOWN_SYMBOL;
return symbol;
}
}
Loading
Loading