Skip to content

Commit

Permalink
Increase the test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alban-auzeill committed Jul 16, 2024
1 parent 59793d4 commit a9c1a59
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
19 changes: 14 additions & 5 deletions java-frontend/src/main/java/org/sonar/java/model/JParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
import org.eclipse.jdt.internal.formatter.TokenManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.java.annotations.VisibleForTesting;
import org.sonar.java.ast.parser.ArgumentListTreeImpl;
import org.sonar.java.ast.parser.FormalParametersListTreeImpl;
import org.sonar.java.ast.parser.InitializerListTreeImpl;
Expand Down Expand Up @@ -302,7 +303,7 @@ static JavaTree.CompilationUnitTreeImpl convert(String version, String unitName,
converter.sema = new JSema(astNode.getAST());
converter.sema.undefinedTypes.addAll(undefinedTypes);
converter.compilationUnit = astNode;
converter.tokenManager = new TokenManager(lex(version, unitName, source.toCharArray()), source, new DefaultCodeFormatterOptions(new HashMap<>()));
converter.tokenManager = createTokenManager(version, unitName, source);

JavaTree.CompilationUnitTreeImpl tree = converter.convertCompilationUnit(astNode);
tree.sema = converter.sema;
Expand All @@ -314,6 +315,11 @@ static JavaTree.CompilationUnitTreeImpl convert(String version, String unitName,
return tree;
}

@VisibleForTesting
static TokenManager createTokenManager(String version, String unitName, String source) {
return new TokenManager(lex(version, unitName, source.toCharArray()), source, new DefaultCodeFormatterOptions(new HashMap<>()));
}

private static void setParents(Tree node) {
Iterator<Tree> childrenIterator = iteratorFor(node);
while (childrenIterator.hasNext()) {
Expand All @@ -331,7 +337,8 @@ private static Iterator<Tree> iteratorFor(Tree node) {
return ((JavaTree) node).getChildren().iterator();
}

private static List<Token> lex(String version, String unitName, char[] sourceChars) {
@VisibleForTesting
static List<Token> lex(String version, String unitName, char[] sourceChars) {
List<Token> tokens = new ArrayList<>();
Scanner scanner = new Scanner(
true,
Expand Down Expand Up @@ -440,7 +447,8 @@ private InternalSyntaxToken firstTokenIn(ASTNode e, int tokenType) {
* @param tokenTypeCandidateB {@link TerminalTokens}
* @return {@link TerminalTokens}
*/
private int firstIndexIn(ASTNode e, int tokenTypeCandidateA, int tokenTypeCandidateB) {
@VisibleForTesting
static int firstIndexIn(TokenManager tokenManager, ASTNode e, int tokenTypeCandidateA, int tokenTypeCandidateB) {
int first = tokenManager.firstIndexIn(e, ANY_TOKEN);
int last = tokenManager.lastIndexIn(e, ANY_TOKEN);
for (int tokenIndex = first; tokenIndex <= last; tokenIndex++) {
Expand All @@ -449,7 +457,8 @@ private int firstIndexIn(ASTNode e, int tokenTypeCandidateA, int tokenTypeCandid
return tokenIndex;
}
}
throw new IllegalStateException(ASTNode.nodeClassForType(e.getNodeType()).toString());
throw new IllegalStateException("Failed to find token " + tokenTypeCandidateA + " or " + tokenTypeCandidateB +
" in the tokens of a " + ASTNode.nodeClassForType(e.getNodeType()).getName());
}

/**
Expand Down Expand Up @@ -1287,7 +1296,7 @@ private VarTypeTreeImpl convertVarType(SimpleType simpleType) {
}

private IdentifierTreeImpl createSimpleName(SimpleName e) {
int tokenIndex = firstIndexIn(e, TerminalTokens.TokenNameIdentifier, TerminalTokens.TokenNameUNDERSCORE);
int tokenIndex = firstIndexIn(tokenManager, e, TerminalTokens.TokenNameIdentifier, TerminalTokens.TokenNameUNDERSCORE);
Token token = tokenManager.get(tokenIndex);
boolean isUnnamedVariable = token.tokenType == TerminalTokens.TokenNameUNDERSCORE;
IdentifierTreeImpl t = new IdentifierTreeImpl(createSyntaxToken(tokenIndex), isUnnamedVariable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.IPackageBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.sonarsource.analyzer.commons.collections.ListUtils;
import org.sonar.java.model.InternalSyntaxToken;
import org.sonar.java.model.JLabelSymbol;
import org.sonar.java.model.JavaTree;
Expand All @@ -39,6 +37,7 @@
import org.sonar.plugins.java.api.tree.SyntaxToken;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.TreeVisitor;
import org.sonarsource.analyzer.commons.collections.ListUtils;

public class IdentifierTreeImpl extends AssessableExpressionTree implements IdentifierTree, JavaTree.AnnotatedTypeTree {

Expand Down Expand Up @@ -86,7 +85,7 @@ public boolean isUnnamedVariable() {

@Override
public Symbol symbol() {
if (binding != null) {
if (binding != null && !isUnnamedVariable) {
switch (binding.getKind()) {
case IBinding.TYPE:
return root.sema.typeSymbol((ITypeBinding) binding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1648,10 +1648,9 @@ void should_skip_implicit_break_statement() {

private CompilationUnit createAST(String source) {
JavaVersion version = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION;
ASTParser astParser = ASTParser.newParser(AST.JLS14);
Map<String, String> options = new HashMap<>();
options.put(JavaCore.COMPILER_COMPLIANCE, version.toString());
options.put(JavaCore.COMPILER_SOURCE, version.toString());
ASTParser astParser = ASTParser.newParser(AST.getJLSLatest());
Map<String, String> options = new HashMap<>(JavaCore.getOptions());
JavaCore.setComplianceOptions(version.effectiveJavaVersionAsString(), options);
options.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, "enabled");
astParser.setCompilerOptions(options);
astParser.setEnvironment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@
import java.util.jar.Manifest;
import org.apache.commons.io.FileUtils;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
import org.eclipse.jdt.internal.formatter.TokenManager;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -410,7 +415,7 @@ void unnamed_pattern_variable() {
class C {
int m(Object o) {
return switch (o) {
case Box(var _) -> 1;
case Box(var /*comment*/_) -> 1;
default -> 0;
};
}
Expand All @@ -432,6 +437,7 @@ record Box(int value) {}
VariableTree variableTree = patternTree.patternVariable();
assertThat(variableTree.type().symbolType().fullyQualifiedName()).isEqualTo("int");
assertThat(variableTree.simpleName().name()).isEqualTo("_");
assertThat(variableTree.simpleName().symbol()).isEqualTo(Symbols.unknownSymbol);
assertThat(variableTree.simpleName().isUnnamedVariable()).isTrue();
}

Expand Down Expand Up @@ -494,6 +500,28 @@ record Box(int value) {}
assertThat(variableTree.simpleName().isUnnamedVariable()).isTrue();
}

@Test
void test_first_index_of_tokens_in_eclipse_ast() {
String version = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION.effectiveJavaVersionAsString();
String unitName = "C.java";
String source = "class C { }";

ASTParser astParser = ASTParser.newParser(AST.getJLSLatest());
JavaCore.setComplianceOptions(version, JavaCore.getOptions());
astParser.setResolveBindings(true);
astParser.setUnitName(unitName);
astParser.setSource(source.toCharArray());
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TokenManager tokenManager = JParser.createTokenManager(version, unitName, source);

assertThat(JParser.firstIndexIn(tokenManager, compilationUnit, TerminalTokens.TokenNameIdentifier, TerminalTokens.TokenNameLBRACE)).isEqualTo(1);
assertThat(JParser.firstIndexIn(tokenManager, compilationUnit, TerminalTokens.TokenNameLBRACE, TerminalTokens.TokenNameIdentifier)).isEqualTo(1);
assertThat(JParser.firstIndexIn(tokenManager, compilationUnit, TerminalTokens.TokenNameRBRACE, TerminalTokens.TokenNameLBRACE)).isEqualTo(2);
assertThatThrownBy(() -> JParser.firstIndexIn(tokenManager, compilationUnit, TerminalTokens.TokenNamebreak, TerminalTokens.TokenNameconst))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Failed to find token 83 or 138 in the tokens of a org.eclipse.jdt.core.dom.CompilationUnit");
}

@Test
void dont_include_running_VM_Bootclasspath_if_jvm_rt_jar_already_provided_in_classpath(@TempDir Path tempFolder) throws IOException {
VariableTree s1 = parseAndGetVariable("class C { void m() { String a; } }");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
*/
package org.sonar.java.model;

import java.util.Objects;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Objects;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

Expand Down Expand Up @@ -71,7 +71,9 @@ void resolvePackageAnnotations() {

@BeforeEach
void setup() {
ASTParser astParser = ASTParser.newParser(AST.JLS14);
ASTParser astParser = ASTParser.newParser(AST.getJLSLatest());
String version = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION.effectiveJavaVersionAsString();
JavaCore.setComplianceOptions(version, JavaCore.getOptions());
astParser.setEnvironment(
new String[]{"target/test-classes"},
new String[]{},
Expand Down

0 comments on commit a9c1a59

Please sign in to comment.