All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.java.checks.StaticMembersAccessCheck Maven / Gradle / Ivy

There is a newer version: 8.10.0.38194
Show newest version
/*
 * 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 Sonar Source-Available License Version 1, as published by SonarSource SA.
 *
 * 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.checks;

import java.util.Collections;
import java.util.List;
import org.sonar.check.Rule;
import org.sonar.java.checks.helpers.QuickFixHelper;
import org.sonar.java.reporting.JavaQuickFix;
import org.sonar.java.reporting.JavaTextEdit;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
import org.sonar.plugins.java.api.tree.Tree;

@Rule(key = "S2209")
public class StaticMembersAccessCheck extends IssuableSubscriptionVisitor {

  private QuickFixHelper.ImportSupplier importSupplier;

  @Override
  public List nodesToVisit() {
    return Collections.singletonList(Tree.Kind.MEMBER_SELECT);
  }

  @Override
  public void setContext(JavaFileScannerContext context) {
    super.setContext(context);
    importSupplier = null;
  }

  @Override
  public void leaveFile(JavaFileScannerContext context) {
    importSupplier = null;
  }

  @Override
  public void visitNode(Tree tree) {
    MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) tree;
    IdentifierTree memberSelectIdentifier = memberSelect.identifier();
    Symbol memberSelectSymbol = memberSelectIdentifier.symbol();
    if (memberSelectSymbol.isStatic()) {
      ExpressionTree leftOperand = memberSelect.expression();
      ExpressionTree selectExpression = leftOperand.is(Tree.Kind.MEMBER_SELECT)
        ? ((MemberSelectExpressionTree) leftOperand).identifier()
        : leftOperand;
      if (!selectExpression.is(Tree.Kind.IDENTIFIER) || ((IdentifierTree) selectExpression).symbol().isVariableSymbol()) {
        QuickFixHelper.newIssue(context)
          .forRule(this)
          .onTree(leftOperand)
          .withMessage("Change this instance-reference to a static reference.")
          .withQuickFix(() -> createQuickFixes(leftOperand, memberSelectSymbol.owner().type()))
          .report();
      }
    }
  }

  private JavaQuickFix createQuickFixes(ExpressionTree leftOperand, Type type) {
    String leftOperandAsText = leftOperand.is(Tree.Kind.IDENTIFIER)
      ? ("\"" + ((IdentifierTree) leftOperand).name() + "\"")
      : "the expression";
    JavaQuickFix.Builder builder = JavaQuickFix.newQuickFix(String.format("Replace %s by \"%s\"", leftOperandAsText, type.name()))
      .addTextEdit(JavaTextEdit.replaceTree(leftOperand, type.name()));

    if (importSupplier == null) {
      importSupplier = QuickFixHelper.newImportSupplier(context);
    }
    importSupplier.newImportEdit(type.fullyQualifiedName())
      .ifPresent(builder::addTextEdit);

    return builder.build();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy