コード例 #1
0
 /*
  * (non-Javadoc)
  *
  * @see
  * org.walkmod.javalang.visitors.VoidVisitorAdapter#visit(org.walkmod.javalang.ast.expr.VariableDeclarationExpr,
  * java.lang.Object)
  */
 @SuppressWarnings("unchecked")
 @Override
 public void visit(VariableDeclarationExpr n, VisitorContext arg) {
   Map<String, Object> m = variablesMap.get(classStack.peek());
   Object[] strings = methodStack.toArray();
   for (int i = 0; i < strings.length; i++) {
     m = (Map<String, Object>) m.get(strings[i]);
   }
   String type = getActualType(n.getType());
   List<VariableDeclarator> lDeclarators = n.getVars();
   for (VariableDeclarator declarator : lDeclarators) {
     m.put(declarator.getId().getName(), type);
   }
   super.visit(n, arg);
 }
コード例 #2
0
 /*
  * (non-Javadoc)
  *
  * @see org.walkmod.javalang.visitors.VoidVisitorAdapter#visit(org.walkmod.javalang.ast.body.FieldDeclaration,
  * java.lang.Object)
  */
 @Override
 public void visit(FieldDeclaration n, VisitorContext arg) {
   Map<String, Object> m = variablesMap.get(classStack.peek());
   String type = getActualType(n.getType());
   List<VariableDeclarator> lDeclarators;
   // if(n instanceof VariableDeclarationExpr){
   // lDeclarators = ((VariableDeclarationExpr) n).getVars();
   // }
   // else{
   // }
   lDeclarators = n.getVariables();
   for (VariableDeclarator declarator : lDeclarators) {
     m.put(declarator.getId().getName(), type);
   }
   super.visit(n, arg);
 }
コード例 #3
0
 /*
  * (non-Javadoc)
  *
  * @see org.walkmod.javalang.visitors.VoidVisitorAdapter#visit(org.walkmod.javalang .ast.stmt.BlockStmt,
  * java.lang.Object)
  */
 @Override
 public void visit(BlockStmt n, VisitorContext arg) {
   List<Statement> stmts = n.getStmts();
   if (stmts == null) {
     super.visit(n, arg);
     return;
   }
   int returnStmtIndex = -1;
   String variableName = null;
   for (int i = 0; i < stmts.size(); i++) {
     Statement statement = stmts.get(i);
     if (statement instanceof ReturnStmt) {
       Expression expr = ((ReturnStmt) statement).getExpr();
       if (expr instanceof NameExpr) {
         returnStmtIndex = i;
         variableName = ((NameExpr) expr).getName();
         break;
       }
     }
   }
   if (returnStmtIndex > 0) {
     Statement statement = stmts.get(returnStmtIndex - 1);
     boolean isEmpty = false;
     boolean isRemove = false;
     Expression init = null;
     if (statement instanceof ExpressionStmt) {
       Expression expression = ((ExpressionStmt) statement).getExpression();
       if (expression instanceof VariableDeclarationExpr) {
         List<VariableDeclarator> vars = ((VariableDeclarationExpr) expression).getVars();
         for (VariableDeclarator variableDeclarator : vars) {
           if (variableDeclarator.getId().getName().equals(variableName)) {
             init = variableDeclarator.getInit();
             vars.remove(variableDeclarator);
             isRemove = true;
             break;
           }
         }
         isEmpty = vars.isEmpty();
       } else if (expression instanceof AssignExpr) {
         AssignExpr assignExpr = (AssignExpr) expression;
         if (assignExpr.getOperator() == ASSIGN_OPERATOR) {
           Expression target = assignExpr.getTarget();
           if (target instanceof NameExpr) {
             NameExpr expr = (NameExpr) target;
             if (expr.getName().equals(variableName)) {
               init = assignExpr.getValue();
               isRemove = true;
               isEmpty = true;
             }
           }
         }
       }
       if (isRemove) {
         ReturnStmt returnStmt = (ReturnStmt) stmts.get(returnStmtIndex);
         returnStmt.setExpr(init);
         if (isEmpty) {
           stmts.remove(returnStmtIndex - 1);
         }
       }
     }
   }
   super.visit(n, arg);
 }