content: /*
   * Copyright 2016 HuntBugs contributors
   * 
   * 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 one.util.huntbugs.detect;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  import com.strobel.assembler.metadata.MetadataHelper;
  import com.strobel.assembler.metadata.MethodReference;
  import com.strobel.assembler.metadata.TypeDefinition;
  import com.strobel.assembler.metadata.TypeReference;
  import com.strobel.decompiler.ast.AstCode;
  import com.strobel.decompiler.ast.Expression;
  import com.strobel.decompiler.ast.Variable;
  
  import one.util.huntbugs.flow.CFG.EdgeType;
  import one.util.huntbugs.flow.CodeBlock;
  import one.util.huntbugs.flow.Inf;
  import one.util.huntbugs.flow.ValuesFlow;
  import one.util.huntbugs.flow.etype.EType;
  import one.util.huntbugs.registry.MethodContext;
  import one.util.huntbugs.registry.anno.AstNodes;
  import one.util.huntbugs.registry.anno.AstVisitor;
  import one.util.huntbugs.registry.anno.WarningDefinition;
  import one.util.huntbugs.util.Methods;
  import one.util.huntbugs.util.Nodes;
  import one.util.huntbugs.util.YesNoMaybe;
  import one.util.huntbugs.warning.Role.StringRole;
  import one.util.huntbugs.warning.Roles;
  import one.util.huntbugs.warning.WarningAnnotation;
  
  /**
   * @author Tagir Valeev
   *
   */
  @WarningDefinition(category = "RedundantCode", name = "UnnecessaryInstanceOf", maxScore = 50)
  @WarningDefinition(category = "Correctness", name = "ImpossibleInstanceOf", maxScore = 70)
  @WarningDefinition(category = "Correctness", name = "ImpossibleCast", maxScore = 70)
  @WarningDefinition(category = "Correctness", name = "ClassComparisonFalse", maxScore = 70)
  public class UnnecessaryInstanceOf {
      private static final StringRole ETYPE = StringRole.forName("ETYPE");
  
      @AstVisitor(nodes = AstNodes.EXPRESSIONS)
      public void visit(Expression node, MethodContext mc, TypeDefinition td) {
          if (node.getCode() == AstCode.InstanceOf) {
              TypeReference typeRef = (TypeReference) node.getOperand();
              Expression expr = node.getArguments().get(0);
              EType eType = Inf.ETYPE.resolve(expr);
              YesNoMaybe ynm = eType.is(typeRef, false);
              if (ynm.yes()) {
                  report(mc, typeRef, expr, null, null, eType.shrinkConstraint(typeRef, false), "UnnecessaryInstanceOf");
              } else if (ynm.no()) {
                  // Autogenerated by javacc
                  if (expr.getOperand() instanceof Variable && ((Variable) expr.getOperand()).getName().startsWith("jjte")
                      && td.getName().endsWith("Parser"))
                      return;
                  report(mc, typeRef, expr, node, EdgeType.TRUE, eType.shrinkConstraint(typeRef, false),
                      "ImpossibleInstanceOf");
              }
          } else if (node.getCode() == AstCode.CheckCast) {
              TypeReference typeRef = MetadataHelper.erase((TypeReference) node.getOperand());
              Expression expr = node.getArguments().get(0);
              EType eType = Inf.ETYPE.resolve(expr);
              YesNoMaybe ynm = eType.is(typeRef, false);
              if (ynm.no()) {
                  report(mc, typeRef, expr, null, null, eType.shrinkConstraint(typeRef, false), "ImpossibleCast");
              }
          } else if (node.getCode() == AstCode.CmpEq || node.getCode() == AstCode.InvokeVirtual && Methods.isEqualsMethod(
              (MethodReference) node.getOperand())) {
              Nodes.ifBinaryWithConst(node, (arg, cst) -> {
                  if (cst instanceof TypeReference) {
                      arg = ValuesFlow.getSource(arg);
                      if (arg.getCode() == AstCode.InvokeVirtual && Methods.isGetClass((MethodReference) arg
                              .getOperand())) {
                          Expression obj = arg.getArguments().get(0);
                          EType eType = Inf.ETYPE.resolve(obj);
                          TypeReference typeRef = (TypeReference) cst;
                          YesNoMaybe ynm = eType.is(typeRef, true);
                          if (ynm.no()) {
                              report(mc, typeRef, obj, node, EdgeType.TRUE, eType.shrinkConstraint(typeRef, true),
                                  "ClassComparisonFalse");
                          }
                      }
                  }
              });
          }
      }
  
      private void report(MethodContext mc, TypeReference typeRef, Expression expr, Expression cond, EdgeType deadEdge,
              EType eType, String wType) {
          int priority = 0;
          List<WarningAnnotation<?>> anno = new ArrayList<>(Arrays.asList(Roles.TARGET_TYPE.create(typeRef), ETYPE.create(
              eType.toString()), Roles.EXPRESSION.create(expr)));
          if (deadEdge != null) {
              CodeBlock deadCode = mc.findDeadCode(cond, deadEdge);
              if (deadCode != null) {
                  priority = deadCode.isExceptional ? 40 : 0;
                  anno.add(Roles.DEAD_CODE_LOCATION.create(mc, deadCode.startExpr));
              } else {
                  priority = 20;
              }
          }
          mc.report(wType, priority, expr, anno);
      }
  }
  