示例#1
0
  private boolean checkStmt(ASTNode onlyNode, ASTIfElseNode node) {
    if (!(onlyNode instanceof ASTStatementSequenceNode)) {
      // only interested in StmtSeq nodes
      return false;
    }

    ASTStatementSequenceNode stmtNode = (ASTStatementSequenceNode) onlyNode;
    List<Object> statements = stmtNode.getStatements();
    if (statements.size() != 1) {
      // need one stmt only
      return false;
    }

    AugmentedStmt as = (AugmentedStmt) statements.get(0);
    Stmt stmt = as.get_Stmt();

    if (!(stmt instanceof DAbruptStmt)) {
      // interested in abrupt stmts only
      return false;
    }
    DAbruptStmt abStmt = (DAbruptStmt) stmt;
    if (!(abStmt.is_Break() || abStmt.is_Continue())) {
      // interested in breaks and continues only
      return false;
    }

    // make sure that the break is not that of the if
    // unliekly but good to check
    SETNodeLabel ifLabel = ((ASTLabeledNode) node).get_Label();

    if (ifLabel != null) {
      if (ifLabel.toString() != null) {
        if (abStmt.is_Break()) {
          String breakLabel = abStmt.getLabel().toString();
          if (breakLabel != null) {
            if (breakLabel.compareTo(ifLabel.toString()) == 0) {
              // is a break of this label
              return false;
            }
          }
        }
      }
    }
    return true;
  }
  public ASTMethodNode inline(SootMethod maybeInline) {
    // check if this method should be inlined

    if (sootClass != null) {
      // 1, method should belong to the same class as the clinit method
      if (sootClass.declaresMethod(maybeInline.getSubSignature())) {
        // System.out.println("The method invoked is from the same class");
        // 2, method should be static

        if (Modifier.isStatic(maybeInline.getModifiers())) {
          // decided to inline
          // send the ASTMethod node of the TO BE INLINED METHOD

          // retireve the active body
          if (!maybeInline.hasActiveBody())
            throw new RuntimeException("method " + maybeInline.getName() + " has no active body!");

          Body bod = maybeInline.getActiveBody();

          Chain units = ((DavaBody) bod).getUnits();

          if (units.size() != 1) {
            throw new RuntimeException("DavaBody AST doesn't have single root.");
          }

          ASTNode ASTtemp = (ASTNode) units.getFirst();
          if (!(ASTtemp instanceof ASTMethodNode))
            throw new RuntimeException("Starting node of DavaBody AST is not an ASTMethodNode");

          // restricting to methods which do not have any variables declared
          ASTMethodNode toReturn = (ASTMethodNode) ASTtemp;

          ASTStatementSequenceNode declarations = toReturn.getDeclarations();
          if (declarations.getStatements().size() == 0) {
            // inline only if there are no declarations in the method inlined
            System.out.println("No declarations in the method. we can inline this method");
            return toReturn;
          }
        }
      }
    }
    return null; // meaning dont inline
  }