Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo-pilastri-sonarsource committed May 30, 2024
1 parent 63486fe commit 9fcaeb4
Show file tree
Hide file tree
Showing 21 changed files with 1,250 additions and 60 deletions.
5 changes: 0 additions & 5 deletions java-frontend/src/main/java/org/sonar/java/model/JUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Set;
import javax.annotation.Nullable;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Modifier;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.SymbolMetadata;
import org.sonar.plugins.java.api.semantic.Type;
Expand Down Expand Up @@ -89,10 +88,6 @@ public static Symbol getPackage(Symbol symbol) {
return symbol;
}

public static boolean isNativeMethod(Symbol.MethodSymbol method) {
return !method.isUnknown() && Modifier.isNative(((JMethodSymbol) method).binding.getModifiers());
}

@Nullable
public static Object defaultValue(Symbol.MethodSymbol method) {
if (method.isUnknown()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ default void customRulesDefinition(RulesDefinition.Context context, RulesDefinit

}

default void register(RegistrarContext registrarContext, CheckFactory checkFActory) {
default void register(RegistrarContext registrarContext, CheckFactory checkFactory) {
register(registrarContext);
}

Expand Down Expand Up @@ -133,11 +133,17 @@ public void registerMainChecks(String repositoryKey, Collection<?> javaCheckClas
// to be overridden
}

/**
* Register main code java checks which have already been initialized by a CheckFactory.
*/
@Beta
public void registerMainChecks(Checks<JavaCheck> checks, Collection<?> javaCheckClassesAndInstances){
// to be overridden
}

/**
* Register test code java checks which have already been initialized by a CheckFactory.
*/
@Beta
public void registerTestChecks(Checks<JavaCheck> checks, Collection<?> javaCheckClassesAndInstances){
// to be overridden
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* SonarQube Java
* Copyright (C) 2012-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* 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 GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.java.collections;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

final class SECollectionUtilsTest {

@Test
void test_get_first_String() {
List<String> list = Arrays.asList("A", "B", "Z");
assertThat(CollectionUtils.getFirst(list, null)).isEqualTo("A");
}

@Test
void test_get_first_default_value() {
assertThat(CollectionUtils.getFirst(Collections.emptySet(), "ABC")).isEqualTo("ABC");
}

@Test
void test_get_collection_size() {
assertThat(CollectionUtils.size(Arrays.asList("a", "b", "c"))).isEqualTo(3);
}

@Test
void test_get_iterable_size() {
assertThat(CollectionUtils.size(new SomeIterable<String>())).isEqualTo(3);
}

private static class SomeIterable<T> implements Iterable<T> {

@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int count = 3;
@Override
public boolean hasNext() {
return count > 0;
}

@Override
@Nullable
public T next() {
count--;
return null;
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Objects;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.java.AnalysisProgress;
Expand All @@ -50,6 +51,9 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.sonar.java.model.JUtilsTest.firstClass;
import static org.sonar.java.model.JUtilsTest.firstMethod;
import static org.sonar.java.model.JUtilsTest.nthMethod;

class JMethodSymbolTest {

Expand Down Expand Up @@ -464,4 +468,32 @@ private static CompilationUnitTreeImpl test(String source) {
return (CompilationUnitTreeImpl) JParserTestUtils.parse(source);
}

@Nested
class IsNativeMethod {
private final JavaTree.CompilationUnitTreeImpl cu = test("""
class A {
native void foo();
void bar() { }
}
""");
private final ClassTreeImpl a = firstClass(cu);

@Test
void isNativeMethod() {
MethodTreeImpl nativeMethod = firstMethod(a);
assertThat(nativeMethod.symbol().isNativeMethod()).isTrue();
}

@Test
void not_native() {
MethodTreeImpl nonNativeMethod = nthMethod(a, 1);
assertThat(nonNativeMethod.symbol().isNativeMethod()).isFalse();
}

@Test
void unknown_method_is_not_native() {
assertThat(Symbols.unknownMethodSymbol.isNativeMethod()).isFalse();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ private void variable_in_class_initializer(boolean isStatic) {
assertThat(initializerBlock.isDefaultMethod()).isFalse();
assertThat(initializerBlock.isSynchronizedMethod()).isFalse();
assertThat(initializerBlock.isVarArgsMethod()).isFalse();
assertThat(initializerBlock.isNativeMethod()).isFalse();
}

@Test
Expand Down
32 changes: 3 additions & 29 deletions java-frontend/src/test/java/org/sonar/java/model/JUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -570,32 +570,6 @@ void unknown_method_is_not_synchronized() {
}
}

@Nested
class IsNativeMethod {
private final JavaTree.CompilationUnitTreeImpl cu = test("class A {\n"
+ " native void foo();\n"
+ " void bar() { }\n"
+ "}");
private final ClassTreeImpl a = firstClass(cu);

@Test
void isNativeMethod() {
MethodTreeImpl nativeMethod = firstMethod(a);
assertThat(JUtils.isNativeMethod(nativeMethod.symbol())).isTrue();
}

@Test
void not_native() {
MethodTreeImpl nonNativeMethod = nthMethod(a, 1);
assertThat(JUtils.isNativeMethod(nonNativeMethod.symbol())).isFalse();
}

@Test
void unknown_method_is_not_native() {
assertThat(JUtils.isNativeMethod(Symbols.unknownMethodSymbol)).isFalse();
}
}

@Nested
class IsDefaultMethod {
private final JavaTree.CompilationUnitTreeImpl cu = test("interface A {\n"
Expand Down Expand Up @@ -913,7 +887,7 @@ private static JavaTree.CompilationUnitTreeImpl test(String source) {
return (JavaTree.CompilationUnitTreeImpl) JParserTestUtils.parse(source);
}

private static ClassTreeImpl firstClass(JavaTree.CompilationUnitTreeImpl cu) {
static ClassTreeImpl firstClass(JavaTree.CompilationUnitTreeImpl cu) {
return nthClass(cu, 0);
}

Expand All @@ -929,11 +903,11 @@ private static ClassTreeImpl nthClass(ClassTreeImpl classTree, int n) {
return (ClassTreeImpl) classTree.members().get(n);
}

private static MethodTreeImpl firstMethod(ClassTreeImpl classTree) {
static MethodTreeImpl firstMethod(ClassTreeImpl classTree) {
return nthMethod(classTree, 0);
}

private static MethodTreeImpl nthMethod(ClassTreeImpl classTree, int n) {
static MethodTreeImpl nthMethod(ClassTreeImpl classTree, int n) {
return (MethodTreeImpl) classTree.members().get(n);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
import org.sonarsource.analyzer.commons.collections.ListUtils;
import org.sonarsource.analyzer.commons.collections.SetUtils;

public class LiveVariables {
public class SELiveVariables {

private final ControlFlowGraph cfg;
private final Map<ControlFlowGraph.Block, Set<Symbol>> out = new HashMap<>();
private final Map<ControlFlowGraph.Block, Set<Symbol>> in = new HashMap<>();
private final boolean includeFields;

private LiveVariables(ControlFlowGraph cfg, boolean includeFields) {
private SELiveVariables(ControlFlowGraph cfg, boolean includeFields) {
this.cfg = cfg;
this.includeFields = includeFields;
}
Expand All @@ -61,14 +61,14 @@ public Set<Symbol> getOut(ControlFlowGraph.Block block) {
}

/**
* Returns LiveVariables object with information concerning local variables and parameters
* Returns SELiveVariables object with information concerning local variables and parameters
*/
public static LiveVariables analyze(ControlFlowGraph cfg) {
public static SELiveVariables analyze(ControlFlowGraph cfg) {
return analyze(cfg, false);
}

private static LiveVariables analyze(ControlFlowGraph cfg, boolean includeFields) {
LiveVariables liveVariables = new LiveVariables(cfg, includeFields);
private static SELiveVariables analyze(ControlFlowGraph cfg, boolean includeFields) {
SELiveVariables liveVariables = new SELiveVariables(cfg, includeFields);
// Generate kill/gen for each block in isolation
Map<ControlFlowGraph.Block, Set<Symbol>> kill = new HashMap<>();
Map<ControlFlowGraph.Block, Set<Symbol>> gen = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SonarQube Java
* Copyright (C) 2012-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* 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 GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
@javax.annotation.ParametersAreNonnullByDefault
@MethodsAreNonnullByDefault
package org.sonar.java.model;

import org.sonar.plugins.java.api.tree.MethodsAreNonnullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import javax.annotation.Nullable;
import org.sonar.java.Preconditions;
import org.sonar.java.annotations.VisibleForTesting;
import org.sonar.java.cfg.LiveVariables;
import org.sonar.java.cfg.SELiveVariables;
import org.sonar.java.model.CFGUtils;
import org.sonar.java.model.SEExpressionUtils;
import org.sonar.java.model.SELineUtils;
Expand Down Expand Up @@ -141,7 +141,7 @@ public class ExplodedGraphWalker {
ExplodedGraph.Node node;
ProgramPoint programPosition;
ProgramState programState;
private LiveVariables liveVariables;
private SELiveVariables liveVariables;
@VisibleForTesting
CheckerDispatcher checkerDispatcher;
private Block exitBlock;
Expand Down Expand Up @@ -233,8 +233,8 @@ private void execute(MethodTree tree) {

checkerDispatcher.init(tree, cfg);

PerformanceMeasure.Duration liveVariablesDuration = PerformanceMeasure.start("LiveVariables.analyze");
liveVariables = LiveVariables.analyze(cfg);
PerformanceMeasure.Duration liveVariablesDuration = PerformanceMeasure.start("SELiveVariables.analyze");
liveVariables = SELiveVariables.analyze(cfg);
liveVariablesDuration.stop();

explodedGraph = new ExplodedGraph();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
package org.sonar.java.se.constraint;

import java.util.Objects;
import org.sonar.java.model.SESymbols;
import org.sonar.plugins.java.api.semantic.Sema;
import org.sonar.plugins.java.api.semantic.Type;

public class TypedConstraint implements Constraint {

Expand Down Expand Up @@ -52,10 +49,4 @@ public int hashCode() {
return Objects.hash(type);
}

public Type getType(Sema semanticModel) {
if (type.charAt(0) == '!') {
return SESymbols.unknownType;
}
return semanticModel.getClassType(type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class SimpleAssignments {

Integer myField;

public void mixedReference() {
myField = getSomething(); // Simple
this.myField = getSomething(); // Simple
this.getOtherInstance().myField = getSomething(); // Not simple
this.myField /= getSomething(); // Not simple

SimpleAssignments simpleAssignments1 = new SimpleAssignments();
simpleAssignments.myField = getSomething();

SimpleAssignments simpleAssignments2 = getOtherInstance();
}

public SimpleAssignments getOtherInstance() {
return new SimpleAssignments();
}

private int getSomething() {
return 1;
}
}
Loading

0 comments on commit 9fcaeb4

Please sign in to comment.