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 @@ -16,7 +16,6 @@
*/
package org.sonar.java.checks;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
Expand All @@ -29,6 +28,7 @@
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.VariableTree;
import org.sonarsource.analyzer.commons.collections.ListUtils;

public abstract class AbstractCallToDeprecatedCodeChecker extends IssuableSubscriptionVisitor {

Expand All @@ -41,7 +41,7 @@ public final void leaveFile(JavaFileScannerContext context) {

@Override
public final List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.IDENTIFIER, Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE, Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR);
return ListUtils.concat(Tree.CLASS_KINDS, List.of(Tree.Kind.IDENTIFIER, Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR));
}

@Override
Expand Down Expand Up @@ -120,7 +120,7 @@ private static boolean isDeprecatedMethod(Tree tree) {
}

private static boolean isDeprecatedClassTree(Tree tree) {
return tree.is(Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE) && ((ClassTree) tree).symbol().isDeprecated();
return Tree.CLASS_KINDS.contains(tree.kind()) && ((ClassTree) tree).symbol().isDeprecated();
}

boolean isFlaggedForRemoval(Symbol deprecatedSymbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.sonar.java.checks;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -44,7 +43,7 @@ public class CallOuterPrivateMethodCheck extends IssuableSubscriptionVisitor {

@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.CLASS, Tree.Kind.INTERFACE);
return Tree.CLASS_KINDS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.sonar.java.checks;

import java.util.Arrays;
import java.util.List;
import org.sonar.check.Rule;
import org.sonar.java.model.ExpressionUtils;
Expand All @@ -34,7 +33,7 @@ public class CallSuperMethodFromInnerClassCheck extends IssuableSubscriptionVisi

@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.CLASS, Tree.Kind.INTERFACE);
return Tree.CLASS_KINDS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.sonar.java.checks;

import java.util.List;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
Expand All @@ -24,9 +25,6 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.VariableTree;

import java.util.Arrays;
import java.util.List;

import static org.sonar.java.checks.helpers.ExpressionsHelper.reportOnClassTree;

@Rule(key = "S1820")
Expand All @@ -43,7 +41,7 @@ public class ClassFieldCountCheck extends IssuableSubscriptionVisitor {

@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.CLASS, Tree.Kind.INTERFACE, Tree.Kind.ENUM);
return Tree.CLASS_KINDS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@ private enum EmptyComparisonType {
Tree.Kind.GREATER_THAN,
Tree.Kind.GREATER_THAN_OR_EQUAL_TO
};
private static final Tree.Kind[] CLASS_TREES = {
Tree.Kind.CLASS,
Tree.Kind.ENUM,
Tree.Kind.INTERFACE,
Tree.Kind.RECORD,
Tree.Kind.ANNOTATION_TYPE
};
private static final Tree.Kind[] CLASS_TREES = Tree.CLASS_KINDS.toArray(new Tree.Kind[0]);
private static final Deque<Boolean> IS_COLLECTION_ENCLOSING_TYPES_STACK = new LinkedList<>();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package org.sonar.java.checks;

import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.java.checks.helpers.MethodTreeUtils;
Expand All @@ -24,12 +28,7 @@
import org.sonar.plugins.java.api.tree.LambdaExpressionTree;
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.Tree;

import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import org.sonarsource.analyzer.commons.collections.ListUtils;

@Rule(key = "S1067")
public class ExpressionComplexityCheck extends IssuableSubscriptionVisitor {
Expand All @@ -56,9 +55,7 @@ public void setContext(JavaFileScannerContext context) {

@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(
Tree.Kind.CLASS,
Tree.Kind.RECORD,
return ListUtils.concat(Tree.CLASS_KINDS, List.of(
Tree.Kind.POSTFIX_INCREMENT,
Tree.Kind.POSTFIX_DECREMENT,
Tree.Kind.PREFIX_INCREMENT,
Expand Down Expand Up @@ -118,12 +115,12 @@ public List<Tree.Kind> nodesToVisit() {
Tree.Kind.IDENTIFIER,
Tree.Kind.ARRAY_TYPE,
Tree.Kind.LAMBDA_EXPRESSION,
Tree.Kind.PRIMITIVE_TYPE);
Tree.Kind.PRIMITIVE_TYPE));
}

@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.CLASS, Tree.Kind.RECORD, Tree.Kind.NEW_ARRAY) || isLambdaWithBlock(tree)) {
if (Tree.CLASS_KINDS.contains(tree.kind()) || tree.is(Tree.Kind.NEW_ARRAY) || isLambdaWithBlock(tree)) {
count.push(0);
level.push(0);
} else {
Expand All @@ -136,7 +133,7 @@ public void visitNode(Tree tree) {

@Override
public void leaveNode(Tree tree) {
if (tree.is(Tree.Kind.CLASS, Tree.Kind.RECORD, Tree.Kind.NEW_ARRAY) || isLambdaWithBlock(tree)) {
if (Tree.CLASS_KINDS.contains(tree.kind()) || tree.is(Tree.Kind.NEW_ARRAY) || isLambdaWithBlock(tree)) {
count.pop();
level.pop();
} else {
Expand All @@ -155,7 +152,7 @@ public void leaveNode(Tree tree) {

private static boolean isInsideEquals(Tree tree) {
Tree parent = tree.parent();
while (parent != null && !parent.is(Tree.Kind.CLASS, Tree.Kind.RECORD)) {
while (parent != null && !Tree.CLASS_KINDS.contains(parent.kind())) {
if (parent.is(Tree.Kind.METHOD) && MethodTreeUtils.isEqualsMethod((MethodTree) parent)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.sonar.java.checks;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
Expand All @@ -27,8 +26,6 @@
import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.check.Rule;
import org.sonarsource.analyzer.commons.collections.MapBuilder;
import org.sonarsource.analyzer.commons.collections.SetUtils;
import org.sonar.java.model.JavaTree;
import org.sonar.java.model.LineUtils;
import org.sonar.java.model.ModifiersUtils;
Expand All @@ -42,6 +39,8 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.VariableTree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
import org.sonarsource.analyzer.commons.collections.ListUtils;
import org.sonarsource.analyzer.commons.collections.MapBuilder;

@DeprecatedRuleKey(ruleKey = "HiddenFieldCheck", repositoryKey = "squid")
@Rule(key = "S1117")
Expand All @@ -53,18 +52,12 @@

@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(
Tree.Kind.CLASS,
Tree.Kind.ENUM,
Tree.Kind.INTERFACE,
Tree.Kind.ANNOTATION_TYPE,
Tree.Kind.RECORD,
Tree.Kind.IMPLICIT_CLASS,
Tree.Kind.VARIABLE,
Tree.Kind.METHOD,
Tree.Kind.CONSTRUCTOR,
Tree.Kind.STATIC_INITIALIZER
);
return ListUtils.concat(Tree.CLASS_KINDS, List.of(
Tree.Kind.VARIABLE,
Tree.Kind.METHOD,
Tree.Kind.CONSTRUCTOR,
Tree.Kind.STATIC_INITIALIZER
));
}

@Override
Expand Down Expand Up @@ -131,14 +124,7 @@
}

private static boolean isClassTree(Tree tree) {
return tree.is(
Tree.Kind.CLASS,
Tree.Kind.ENUM,
Tree.Kind.INTERFACE,
Tree.Kind.ANNOTATION_TYPE,
Tree.Kind.RECORD,
Tree.Kind.IMPLICIT_CLASS
);
return Tree.CLASS_KINDS.contains(tree.kind());
}

@Override
Expand Down Expand Up @@ -176,7 +162,10 @@
}

public Set<Tree.Kind> excludedNodes() {
return SetUtils.immutableSetOf(Tree.Kind.METHOD, Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.INTERFACE, Tree.Kind.NEW_CLASS);
Set<Tree.Kind> excluded = new HashSet<>(Tree.CLASS_KINDS);

Check warning on line 165 in java-checks/src/main/java/org/sonar/java/checks/HiddenFieldCheck.java

View check run for this annotation

SonarQube-Next / SonarQube Code Analysis

Convert this Set to an EnumSet.

[S1641] Sets with elements that are enum values should be replaced with EnumSet See more on https://next.sonarqube.com/sonarqube/project/issues?id=org.sonarsource.java%3Ajava&pullRequest=5869&issues=5dabf70c-85c3-4ac2-8f87-fbb4d0545ada&open=5dabf70c-85c3-4ac2-8f87-fbb4d0545ada
excluded.add(Tree.Kind.METHOD);
excluded.add(Tree.Kind.NEW_CLASS);
return excluded;
}
Comment on lines 164 to 169

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Quality: HiddenFieldCheck excludedNodes() now also excludes RECORD/ANN/IMPLICIT

VariableList.excludedNodes() previously stopped recursion at {METHOD, CLASS, ENUM, INTERFACE, NEW_CLASS}; it now uses Tree.CLASS_KINDS plus METHOD/NEW_CLASS, additionally excluding RECORD, ANNOTATION_TYPE and IMPLICIT_CLASS. This changes which variables inside static blocks/method bodies are collected as excluded when a local record (or annotation type) is present, altering hidden-field detection in that edge case. Verify this matches the intended behavior or that it is exercised by tests.

Was this helpful? React with 👍 / 👎


private void visit(Tree tree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.sonar.java.checks;

import java.util.List;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.java.checks.helpers.ExpressionsHelper;
Expand All @@ -27,9 +28,6 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.Tree.Kind;

import java.util.Arrays;
import java.util.List;

@Rule(key = "S2972")
public class InnerClassTooManyLinesCheck extends IssuableSubscriptionVisitor {

Expand All @@ -42,7 +40,7 @@ public class InnerClassTooManyLinesCheck extends IssuableSubscriptionVisitor {

@Override
public List<Kind> nodesToVisit() {
return Arrays.asList(Kind.CLASS, Kind.ENUM, Kind.INTERFACE, Kind.ANNOTATION_TYPE);
return Tree.CLASS_KINDS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@
*/
package org.sonar.java.checks;

import java.util.List;
import java.util.Locale;
import javax.annotation.Nullable;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.ClassTree;
import org.sonar.plugins.java.api.tree.Tree;
import javax.annotation.Nullable;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

@Rule(key = "S2176")
public class InterfaceOrSuperclassShadowingCheck extends IssuableSubscriptionVisitor {

@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.CLASS, Tree.Kind.INTERFACE, Tree.Kind.RECORD);
return Tree.CLASS_KINDS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.sonar.java.checks;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -47,7 +46,7 @@ public class MembersDifferOnlyByCapitalizationCheck extends IssuableSubscription

@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.CLASS, Tree.Kind.INTERFACE, Tree.Kind.ENUM, Tree.Kind.RECORD);
return Tree.CLASS_KINDS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class MultipleMainInstancesCheck extends IssuableSubscriptionVisitor implements JavaVersionAwareVisitor {
@Override
public List<Tree.Kind> nodesToVisit() {
return List.of(Tree.Kind.CLASS, Tree.Kind.INTERFACE, Tree.Kind.ENUM, Tree.Kind.RECORD, Tree.Kind.IMPLICIT_CLASS);
return Tree.CLASS_KINDS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.sonar.java.checks;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -36,6 +35,7 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.Tree.Kind;
import org.sonar.plugins.java.api.tree.VariableTree;
import org.sonarsource.analyzer.commons.collections.ListUtils;

import static org.sonar.java.reporting.AnalyzerMessage.textSpanBetween;

Expand All @@ -46,12 +46,12 @@ public class OneDeclarationPerLineCheck extends IssuableSubscriptionVisitor {

@Override
public List<Kind> nodesToVisit() {
return Arrays.asList(Kind.INTERFACE, Kind.CLASS, Kind.ENUM, Kind.ANNOTATION_TYPE, Kind.BLOCK, Kind.STATIC_INITIALIZER, Kind.CASE_GROUP);
return ListUtils.concat(Tree.CLASS_KINDS, List.of(Kind.BLOCK, Kind.STATIC_INITIALIZER, Kind.CASE_GROUP));
}

@Override
public void visitNode(Tree tree) {
if (tree.is(Kind.INTERFACE, Kind.CLASS, Kind.ENUM, Kind.ANNOTATION_TYPE)) {
if (Tree.CLASS_KINDS.contains(tree.kind())) {
// Field class declaration
checkVariables(((ClassTree) tree).members());
} else if (tree.is(Kind.BLOCK, Kind.STATIC_INITIALIZER)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.sonar.plugins.java.api.tree.NewArrayTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.VariableTree;
import org.sonarsource.analyzer.commons.collections.ListUtils;

@Rule(key = "S2386")
public class PublicStaticMutableMembersCheck extends IssuableSubscriptionVisitor {
Expand Down Expand Up @@ -115,7 +116,7 @@ public class PublicStaticMutableMembersCheck extends IssuableSubscriptionVisitor

@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.INTERFACE, Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.ASSIGNMENT);
return ListUtils.concat(Tree.CLASS_KINDS, List.of(Tree.Kind.ASSIGNMENT));
}

@Override
Expand Down Expand Up @@ -170,8 +171,8 @@ private void checkAssignment(AssignmentExpressionTree node) {

@Override
public void leaveNode(Tree tree) {
// cleanup
if (tree.is(Tree.Kind.CLASS, Tree.Kind.ENUM)) {
// cleanup for every class-like declaration visited (i.e. all subscribed nodes except ASSIGNMENT)
if (!tree.is(Tree.Kind.ASSIGNMENT)) {
IMMUTABLE_CANDIDATES.removeAll(CLASS_IMMUTABLE_CANDIDATES.getOrDefault(tree, Collections.emptyList()));
}
}
Expand Down
Loading
Loading