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

org.sonar.flex.checks.DuplicateSwitchCaseImplementationCheck Maven / Gradle / Ivy

/*
 * SonarQube Flex Plugin
 * Copyright (C) 2010 SonarSource
 * [email protected]
 *
 * 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  02
 */
package org.sonar.flex.checks;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Token;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.flex.FlexGrammar;
import org.sonar.squidbridge.checks.SquidCheck;
import org.sonar.sslr.parser.LexerlessGrammar;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;

@Rule(
  key = "S1871",
  priority = Priority.MAJOR)
public class DuplicateSwitchCaseImplementationCheck extends SquidCheck {

  @Override
  public void init() {
    subscribeTo(FlexGrammar.SWITCH_STATEMENT);
  }

  @Override
  public void visitNode(AstNode astNode) {
    Map> cases = Maps.newLinkedHashMap();

    for (AstNode caseElement : astNode.getChildren(FlexGrammar.CASE_ELEMENT)) {
      AstNode caseLabel = caseElement.getLastChild(FlexGrammar.CASE_LABEL);
      List tokens = getCaseImplementationTokens(caseElement);

      if (!tokens.isEmpty()) {
        AstNode duplicatedCase = getDuplicatedCase(cases, tokens);

        if (duplicatedCase != null) {
          getContext().createLineViolation(this, "Either merge this case with the identical one on line \"{0}\" or change one of the implementations.",
            caseElement, duplicatedCase.getTokenLine());
        } else {
          cases.put(caseLabel, tokens);
        }
      }
    }
  }

  private List getCaseImplementationTokens(AstNode caseElement) {
    List tokens = Lists.newArrayList();

    for (AstNode directive : caseElement.getChildren(FlexGrammar.DIRECTIVE)) {
      tokens.addAll(directive.getTokens());
    }
    return tokens;
  }

  /**
   * Compares case implementation to previous ones.
   *
   * @return duplicated case condition if there is, null otherwise.
   */
  @Nullable
  private AstNode getDuplicatedCase(Map> cases, List tokens) {
    for (Map.Entry> entry : cases.entrySet()) {

      if (areTokenValuesIdentical(entry.getValue(), tokens)) {
        return entry.getKey();
      }
    }
    return null;
  }

  private boolean areTokenValuesIdentical(List implementation1, List implementation2) {
    int nbToken = implementation1.size();

    if (implementation2.size() != nbToken) {
      return false;
    }

    for (int i = 0; i < nbToken; i++) {
      if (!implementation1.get(i).getValue().equals(implementation2.get(i).getValue())) {
        return false;
      }
    }

    return true;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy