Skip to content
Draft
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 @@ -48,7 +48,7 @@

import static org.sonar.java.checks.helpers.ReassignmentFinder.getInitializerOrExpression;
import static org.sonar.java.checks.helpers.ReassignmentFinder.getReassignments;
import static org.sonar.java.model.JUtils.hasUnknownTypeInHierarchy;
import static org.sonar.java.model.SemanticUtils.hasUnknownTypeInHierarchy;

public class ExpressionsHelper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@
package org.sonar.java.checks.helpers;

import com.google.common.io.Files;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.sonar.java.model.JParser;
import org.sonar.java.model.JParserConfig;
import org.sonar.java.JavaParsers;
import org.sonar.plugins.java.api.JavaVersion;
import org.sonar.plugins.java.api.tree.*;

Expand Down Expand Up @@ -76,8 +73,8 @@ public static CompilationUnitTree parse(File file) {

public static CompilationUnitTree parse(String source) {
List<File> classpath = Arrays.asList(new File("target/test-classes"), new File("target/classes"));
JavaVersion version = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION;
return JParser.parse(JParserConfig.Mode.FILE_BY_FILE.create(version, classpath).astParser(), version.toString(), "test", source);
JavaVersion version = JavaParsers.MAXIMUM_SUPPORTED_JAVA_VERSION;
return JavaParsers.Mode.FILE_BY_FILE.create(version, classpath).parse("test", source);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.sonar.check.Rule;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.SemanticUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol;
Expand Down Expand Up @@ -51,8 +51,8 @@ public void visitNode(Tree tree) {
Map<String, Object> defaultValueByName = annotationSymbol.memberSymbols().stream()
.filter(Symbol::isMethodSymbol)
.map(Symbol.MethodSymbol.class::cast)
.filter(s -> JUtils.defaultValue(s) != null)
.collect(Collectors.toMap(Symbol::name, JUtils::defaultValue));
.filter(s -> SemanticUtils.annotationElementDefaultValue(s) != null)
.collect(Collectors.toMap(Symbol::name, SemanticUtils::annotationElementDefaultValue));

for (ExpressionTree argument : annotationTree.arguments()) {
ExpressionTree valueSet = argument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.Optional;
import org.sonar.check.Rule;
import org.sonar.java.checks.helpers.MethodTreeUtils;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.SemanticUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Symbol;
Expand Down Expand Up @@ -67,7 +67,7 @@ private void checkContractChange(MethodTree methodTree, Symbol.MethodSymbol over

for (int i = 0; i < methodTree.parameters().size(); i++) {
VariableTree parameter = methodTree.parameters().get(i);
checkParameter(parameter, JUtils.parameterAnnotations(overridee, i));
checkParameter(parameter, SemanticUtils.parameterAnnotations(overridee, i));
}

// If the method from the parent claims to never return null, the method from the child
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
*/
package org.sonar.java.checks;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import org.sonar.check.Rule;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.SemanticUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.Type;
Expand All @@ -30,13 +35,6 @@
import org.sonar.plugins.java.api.tree.Tree.Kind;
import org.sonar.plugins.java.api.tree.TypeTree;

import javax.annotation.Nullable;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Rule(key = "S2440")
public class ClassWithOnlyStaticMethodsInstantiationCheck extends IssuableSubscriptionVisitor {

Expand All @@ -60,7 +58,7 @@ public void visitNode(Tree tree) {
}

private static boolean instantiateOwnClass(Tree identifier, Symbol.TypeSymbol newClassTypeSymbol) {
Symbol enclosingClass = JUtils.enclosingClass(identifier);
Symbol enclosingClass = SemanticUtils.enclosingClass(identifier);
return enclosingClass != null && enclosingClass.type().equals(newClassTypeSymbol.type());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.List;
import java.util.Optional;
import org.sonar.check.Rule;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.SemanticUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.ParameterizedTypeTree;
Expand All @@ -45,7 +45,7 @@ public void visitNode(Tree tree) {
getMapKeyTree(typeTree).ifPresent(mapKeyTree -> {
if (!isGenericOrWildCard(mapKeyTree) &&
!implementsComparable(mapKeyTree.symbolType()) &&
!JUtils.hasUnknownTypeInHierarchy(mapKeyTree.symbolType().symbol())) {
!SemanticUtils.hasUnknownTypeInHierarchy(mapKeyTree.symbolType().symbol())) {
reportIssue(mapKeyTree, "The key type should implement Comparable.");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.HashSet;
import java.util.List;
import org.sonar.check.Rule;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.SemanticUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.SymbolMetadata;
Expand Down Expand Up @@ -110,8 +110,8 @@ private static boolean useRawTypeOfParametrizedType(Type methodParam, Type overr
private static boolean differentAnnotationsOnParameters(Symbol.MethodSymbol method, Symbol.MethodSymbol overridee) {
for (int i = 0; i < method.parameterTypes().size(); i++) {
if (differentAnnotations(
JUtils.parameterAnnotations(method, i),
JUtils.parameterAnnotations(overridee, i))) {
SemanticUtils.parameterAnnotations(method, i),
SemanticUtils.parameterAnnotations(overridee, i))) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.sonar.java.checks.helpers.ExpressionsHelper;
import org.sonar.java.checks.helpers.QuickFixHelper;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.LineUtils;
import org.sonar.java.model.SemanticUtils;
import org.sonar.java.reporting.JavaQuickFix;
import org.sonar.java.reporting.JavaTextEdit;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
Expand Down Expand Up @@ -395,6 +395,6 @@ public static Optional<ExpressionTree> expressionWithoutParentheses(@Nullable Tr

private static boolean isArgumentCompatible(Type argumentType, Type parameterType) {
return argumentType.isSubtypeOf(parameterType)
|| JUtils.wrapTypeIfPrimitive(argumentType).equals(JUtils.wrapTypeIfPrimitive(parameterType));
|| SemanticUtils.wrapTypeIfPrimitive(argumentType).equals(SemanticUtils.wrapTypeIfPrimitive(parameterType));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Optional;
import java.util.stream.Stream;
import org.sonar.check.Rule;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.SemanticUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.CatchTree;
Expand Down Expand Up @@ -103,7 +103,7 @@ private static boolean isConcreteNonComparable(Type type) {
!type.symbol().isInterface() &&
!type.symbol().isAbstract() &&
!hasCompatibleNaturalOrdering(type) &&
!JUtils.hasUnknownTypeInHierarchy(type.symbol());
!SemanticUtils.hasUnknownTypeInHierarchy(type.symbol());
}

private static boolean hasCompatibleNaturalOrdering(Type type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.sonar.java.model.JParser;
import org.sonar.java.model.JParserConfig;
import org.sonar.java.JavaParsers;
import org.sonar.plugins.java.api.JavaVersion;
import org.sonar.plugins.java.api.tree.AssignmentExpressionTree;
import org.sonar.plugins.java.api.tree.ClassTree;
Expand Down Expand Up @@ -84,8 +83,8 @@ public static CompilationUnitTree parse(File file) {

public static CompilationUnitTree parse(String source) {
List<File> classpath = Arrays.asList(new File("target/test-classes"), new File("target/classes"));
JavaVersion version = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION;
return JParser.parse(JParserConfig.Mode.FILE_BY_FILE.create(version, classpath).astParser(), version.toString(), "test", source);
JavaVersion version = JavaParsers.MAXIMUM_SUPPORTED_JAVA_VERSION;
return JavaParsers.Mode.FILE_BY_FILE.create(version, classpath).parse("test", source);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
import java.util.List;
import java.util.function.UnaryOperator;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.java.JavaParsers;
import org.sonar.java.SonarComponents;
import org.sonar.java.checks.verifier.TestUtils;
import org.sonar.java.model.JParser;
import org.sonar.java.model.JParserConfig;
import org.sonar.java.model.VisitorsBridge;
import org.sonar.java.model.springcontext.BeanDefinitionGatherer;
import org.sonar.java.model.springcontext.BeanDefinitionHolder;
Expand Down Expand Up @@ -173,7 +172,7 @@ private static CompilationUnitTree parse(File file, List<File> classpath) {
} catch (Exception e) {
throw new IllegalStateException("Unable to read file '" + file.getAbsolutePath() + "'", e);
}
JavaVersion version = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION;
return JParser.parse(JParserConfig.Mode.FILE_BY_FILE.create(version, classpath).astParser(), version.toString(), file.getName(), source);
JavaVersion version = JavaParsers.MAXIMUM_SUPPORTED_JAVA_VERSION;
return JavaParsers.Mode.FILE_BY_FILE.create(version, classpath).parse(file.getName(), source);
}
}
5 changes: 2 additions & 3 deletions java-frontend/src/main/java/org/sonar/java/JavaFrontend.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.sonar.java.collections.CollectionUtils;
import org.sonar.java.exceptions.ApiMismatchException;
import org.sonar.java.filters.SonarJavaIssueFilter;
import org.sonar.java.model.JParserConfig;
import org.sonar.java.model.VisitorsBridge;
import org.sonar.java.telemetry.Telemetry;
import org.sonar.java.telemetry.TelemetryKey;
Expand Down Expand Up @@ -237,15 +236,15 @@ private void scanInBatches(BatchModeContext context, List<InputFile> allInputFil
private <T extends InputFile> void scanBatch(BatchModeContext context, List<T> batchFiles, AnalysisProgress analysisProgress) {
analysisProgress.startBatch(batchFiles.size());
Set<Runnable> environmentsCleaners = new HashSet<>();
JParserConfig.Mode.BATCH
JavaParsers.Mode.BATCH
.create(javaVersion, context.getClasspath(), sonarComponents.shouldIgnoreUnnamedModuleForSplitPackage())
.parse(batchFiles, sonarComponents::analysisCancelled, analysisProgress, (input, result) -> scanAsBatchCallback(input, result, context, environmentsCleaners));
// Due to a bug in ECJ, JAR files remain locked after the analysis on Windows, we unlock them manually, at the end of each batches. See SONARJAVA-3609.
environmentsCleaners.forEach(Runnable::run);
analysisProgress.endBatch();
}

private static void scanAsBatchCallback(InputFile inputFile, JParserConfig.Result result, BatchModeContext context, Set<Runnable> environmentsCleaners) {
private static void scanAsBatchCallback(InputFile inputFile, ParseResult result, BatchModeContext context, Set<Runnable> environmentsCleaners) {
JavaAstScanner scanner = context.selectScanner(inputFile);
Duration duration = PerformanceMeasure.start(context.descriptor(inputFile));
scanner.simpleScan(inputFile, result, ast ->
Expand Down
30 changes: 30 additions & 0 deletions java-frontend/src/main/java/org/sonar/java/JavaParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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;

import java.util.function.BiConsumer;
import java.util.function.BooleanSupplier;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.java.model.JavaTree;

public interface JavaParser {

JavaTree.CompilationUnitTreeImpl parse(String unitName, String source);

void parse(Iterable<? extends InputFile> inputFiles, BooleanSupplier isCanceled, AnalysisProgress analysisProgress, BiConsumer<InputFile, ParseResult> action);

}
45 changes: 45 additions & 0 deletions java-frontend/src/main/java/org/sonar/java/JavaParsers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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;

import java.io.File;
import java.util.List;
import org.sonar.java.jdt.JParserConfig;
import org.sonar.java.model.JavaVersionImpl;
import org.sonar.plugins.java.api.JavaVersion;

public final class JavaParsers {

public static final JavaVersion MAXIMUM_SUPPORTED_JAVA_VERSION = new JavaVersionImpl(JavaVersionImpl.MAX_SUPPORTED, true);

private JavaParsers() {
}

public enum Mode {
BATCH,
FILE_BY_FILE;

public JavaParser create(JavaVersion javaVersion, List<File> classpath) {
return create(javaVersion, classpath, false);
}

public JavaParser create(JavaVersion javaVersion, List<File> classpath, boolean shouldIgnoreUnnamedModuleForSplitPackage) {
return JParserConfig.create(this, javaVersion, classpath, shouldIgnoreUnnamedModuleForSplitPackage);
}
}

}
41 changes: 41 additions & 0 deletions java-frontend/src/main/java/org/sonar/java/ParseResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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;

import org.sonar.java.model.JavaTree;

public final class ParseResult {
private final Exception exception;
private final JavaTree.CompilationUnitTreeImpl tree;

public ParseResult(Exception exception) {
this.exception = exception;
this.tree = null;
}

public ParseResult(JavaTree.CompilationUnitTreeImpl tree) {
this.exception = null;
this.tree = tree;
}

public JavaTree.CompilationUnitTreeImpl get() throws Exception {

Check warning on line 35 in java-frontend/src/main/java/org/sonar/java/ParseResult.java

View check run for this annotation

SonarQube-Next / SonarQube Code Analysis

Replace generic exceptions with specific library exceptions or a custom exception.

[S112] Generic exceptions should never be thrown See more on https://next.sonarqube.com/sonarqube/project/issues?id=org.sonarsource.java%3Ajava&pullRequest=6485&issues=05f1016e-c85c-4878-b88e-085681a71ab1&open=05f1016e-c85c-4878-b88e-085681a71ab1
if (exception != null) {
throw exception;
}
return tree;
}
}
Loading
Loading