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

com.google.errorprone.bugpatterns.MissingCasesInEnumSwitch Maven / Gradle / Ivy

There is a newer version: 2.27.1
Show newest version
/*
 * Copyright 2014 The Error Prone Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.errorprone.bugpatterns;

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.RuntimeVersion;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.SwitchTree;
import com.sun.tools.javac.code.Type;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.lang.model.element.ElementKind;

/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
@BugPattern(
    summary = "Switches on enum types should either handle all values, or have a default case.",
    severity = WARNING)
public class MissingCasesInEnumSwitch extends BugChecker implements SwitchTreeMatcher {

  public static final int MAX_CASES_TO_PRINT = 5;

  @Override
  public Description matchSwitch(SwitchTree tree, VisitorState state) {
    Type switchType = ASTHelpers.getType(tree.getExpression());
    if (switchType.asElement().getKind() != ElementKind.ENUM) {
      return Description.NO_MATCH;
    }
    // default case is present
    if (tree.getCases().stream().anyMatch(c -> c.getExpression() == null)) {
      return Description.NO_MATCH;
    }
    ImmutableSet handled =
        tree.getCases().stream()
            .flatMap(MissingCasesInEnumSwitch::getExpressions)
            .filter(IdentifierTree.class::isInstance)
            .map(e -> ((IdentifierTree) e).getName().toString())
            .collect(toImmutableSet());
    Set unhandled = Sets.difference(ASTHelpers.enumValues(switchType.asElement()), handled);
    if (unhandled.isEmpty()) {
      return Description.NO_MATCH;
    }
    return buildDescription(tree).setMessage(buildMessage(unhandled)).build();
  }

  /**
   * Build the diagnostic message.
   *
   * 

Examples: * *

    *
  • Non-exhaustive switch, expected cases for: FOO *
  • Non-exhaustive switch, expected cases for: FOO, BAR, BAZ, and 42 others. *
*/ private static String buildMessage(Set unhandled) { StringBuilder message = new StringBuilder( "Non-exhaustive switch; either add a default or handle the remaining cases: "); int numberToShow = unhandled.size() > MAX_CASES_TO_PRINT ? 3 // if there are too many to print, only show three examples. : unhandled.size(); message.append(unhandled.stream().limit(numberToShow).collect(Collectors.joining(", "))); if (numberToShow < unhandled.size()) { message.append(String.format(", and %d others", unhandled.size() - numberToShow)); } return message.toString(); } @SuppressWarnings("unchecked") private static Stream getExpressions(CaseTree caseTree) { try { if (RuntimeVersion.isAtLeast12()) { return ((List) CaseTree.class.getMethod("getExpressions").invoke(caseTree)) .stream(); } else { return Stream.of(caseTree.getExpression()); } } catch (ReflectiveOperationException e) { throw new LinkageError(e.getMessage(), e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy