示例#1
0
  private static int calculateValue(UastAndroidContext context, UExpression expression) {
    // This function assumes that the only inputs to the expression are static integer
    // flags that combined via bitwise operands.
    if (UastLiteralUtils.isIntegralLiteral(expression)) {
      return (int) UastLiteralUtils.getLongValue((ULiteralExpression) expression);
    }

    if (expression instanceof UResolvable) {
      UDeclaration resolvedNode = ((UResolvable) expression).resolve(context);
      if (resolvedNode instanceof UVariable) {
        UExpression initializer = ((UVariable) resolvedNode).getInitializer();
        if (initializer != null) {
          Object value = initializer.evaluate();
          if (value instanceof Integer) {
            return (Integer) value;
          }
        }
      }
    }

    if (expression instanceof UBinaryExpression) {
      UBinaryExpression binaryExpression = (UBinaryExpression) expression;
      UastBinaryOperator operator = binaryExpression.getOperator();
      int leftValue = calculateValue(context, binaryExpression.getLeftOperand());
      int rightValue = calculateValue(context, binaryExpression.getRightOperand());

      if (operator == UastBinaryOperator.BITWISE_OR) {
        return leftValue | rightValue;
      }
      if (operator == UastBinaryOperator.BITWISE_AND) {
        return leftValue & rightValue;
      }
      if (operator == UastBinaryOperator.BITWISE_XOR) {
        return leftValue ^ rightValue;
      }
    }

    return 0;
  }
示例#2
0
  @Nullable
  private static String getLhs(@NonNull UCallExpression call) {
    UElement parent = call.getParent();
    if (UastBinaryExpressionWithTypeUtils.isTypeCast(parent)) {
      assert parent != null;
      parent = parent.getParent();
    }

    if (parent instanceof UVariable) {
      UVariable vde = (UVariable) parent;
      return vde.getName();
    } else if (parent instanceof UBinaryExpression) {
      UBinaryExpression be = (UBinaryExpression) parent;
      UExpression left = be.getLeftOperand();
      if (left instanceof USimpleReferenceExpression || left instanceof UQualifiedExpression) {
        return be.getLeftOperand().toString();
      } else if (left instanceof UArrayAccessExpression) {
        UArrayAccessExpression aa = (UArrayAccessExpression) left;
        return aa.getReceiver().toString();
      }
    }

    return null;
  }