예제 #1
0
 public void visit(BlockStmt n, Object arg) {
   if (n.getStmts() != null) {
     for (Statement s : n.getStmts()) {
       s.accept(this, arg);
     }
   }
 }
 /**
  * Looking for dependencies in method's body
  *
  * @param allClasses
  */
 public void findDependBody(ArrayList<RealClass> allClasses) {
   // Every possible class has to be considered
   // Therefore all classes passed in
   HashSet<String> allClassName = new HashSet<String>();
   for (RealClass realClass : allClasses) {
     allClassName.add(realClass.getClassName());
   }
   for (TypeDeclaration typeDeclaration : cUnit.getTypes()) {
     List<BodyDeclaration> bodyDeclarations = typeDeclaration.getMembers();
     try {
       MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclarations.get(0);
       if (methodDeclaration.getName().contentEquals("main")) {
         // Block here means all lines inside {}
         BlockStmt blockStmt = methodDeclaration.getBody();
         List<Statement> statements = blockStmt.getStmts();
         for (Statement statement : statements) {
           String stmtLine = statement.toString();
           // Only care about dependencies, therefore no need for scanning whole line
           // Just looking at strings before "="
           if (stmtLine.contains("=")) {
             stmtLine = stmtLine.substring(0, stmtLine.indexOf('='));
             String variable = stmtLine.substring(0, stmtLine.indexOf(' '));
             if (allClassName.contains(variable)) {
               ClassRelation relation =
                   new ClassRelation(this.getClassName(), variable, RelatinType.DEPENDENCIES);
               classRelations.add(relation);
             }
           }
         }
       }
     } catch (Exception e) {
       // TODO: handle exception
     }
   }
 }
 /**
  * Adds the given statement to the specified block. The list of statements will be initialized if
  * it is <code>null</code>.
  *
  * @param block
  * @param stmt
  */
 public static void addStmt(BlockStmt block, Statement stmt) {
   List<Statement> stmts = block.getStmts();
   if (stmts == null) {
     stmts = new ArrayList<Statement>();
     block.setStmts(stmts);
   }
   stmts.add(stmt);
 }
예제 #4
0
 public MethodBuilder statement(Statement astStmt) {
   body.getStmts().add(astStmt);
   return this;
 }
예제 #5
0
 /**
  * Highly recursive method that will parse a file into its semantical statements. Currently
  * identifies all often used Statements (if, for, while, try, etc.). Also every other non-flow
  * dependant statement like variable assignments, method calls, etc are considered Expression
  * Statements. As they are "primitive" in terms that cannot contain inner expressions, we just use
  * the default name of the class for them as the Token Type.
  *
  * <p>For more information and examples on how this parser actually works, please refer to the
  * documentation.
  *
  * @param stmt
  */
 private void parseStatement(Statement stmt) {
   // full list of all statements:
   // https://java.net/projects/jpjavaparser/sources/source-code-repository/show/src/japa/parser/ast/stmt
   if (stmt == null) {
     return;
   }
   // Block Statement is a List of statements
   if (stmt instanceof BlockStmt) {
     BlockStmt blockStmt = (BlockStmt) stmt;
     for (Statement st : blockStmt.getStmts()) {
       parseStatement(st);
     }
     // If Statement
   } else if (stmt instanceof IfStmt) {
     IfStmt ifStmt = (IfStmt) stmt;
     tokens.add(new Token(ifStmt.getCondition().toString(), getObjectType(ifStmt)));
     parseStatement(ifStmt.getThenStmt());
     parseStatement(ifStmt.getElseStmt());
     // For statement
   } else if (stmt instanceof ForStmt) {
     ForStmt forStmt = (ForStmt) stmt;
     if (forStmt.getInit() != null
         && forStmt.getCompare() != null
         && forStmt.getUpdate() != null) {
       tokens.add(
           new Token(
               forStmt.getInit().toString()
                   + ";"
                   + forStmt.getCompare().toString()
                   + ";"
                   + forStmt.getUpdate().toString(),
               getObjectType(forStmt)));
     } else {
       tokens.add(new Token("For", getObjectType(forStmt)));
     }
     parseStatement(forStmt.getBody());
     // Foreach Statement
   } else if (stmt instanceof ForeachStmt) {
     ForeachStmt foreachStmt = (ForeachStmt) stmt;
     tokens.add(
         new Token(
             foreachStmt.getVariable().toString() + ":" + foreachStmt.getIterable().toString(),
             getObjectType(foreachStmt)));
     parseStatement(foreachStmt.getBody());
     // While Statement
   } else if (stmt instanceof WhileStmt) {
     WhileStmt whileStmt = (WhileStmt) stmt;
     tokens.add(new Token(whileStmt.getCondition().toString(), getObjectType(whileStmt)));
     parseStatement(whileStmt.getBody());
     // Do Statement
   } else if (stmt instanceof DoStmt) {
     DoStmt doStmt = (DoStmt) stmt;
     tokens.add(new Token(doStmt.getCondition().toString(), getObjectType(doStmt)));
     parseStatement(doStmt.getBody());
     // Try Statement
   } else if (stmt instanceof TryStmt) {
     TryStmt tryStmt = (TryStmt) stmt;
     tokens.add(new Token("Try", getObjectType(tryStmt)));
     parseStatement(tryStmt.getTryBlock());
     // Return Statement
   } else if (stmt instanceof ReturnStmt) {
     ReturnStmt returnStmt = (ReturnStmt) stmt;
     if (returnStmt.getExpr() != null) {
       tokens.add(new Token(returnStmt.getExpr().toString(), getObjectType(returnStmt)));
     } else {
       tokens.add(new Token("Void", getObjectType(returnStmt)));
     }
     // Empty Statement
   } else if (stmt instanceof EmptyStmt) {
     tokens.add(new Token("", getObjectType(stmt)));
     // Continue Statement
   } else if (stmt instanceof ContinueStmt) {
     tokens.add(new Token("Continue", getObjectType(stmt)));
     // Break Statement
   } else if (stmt instanceof BreakStmt) {
     tokens.add(new Token("Break", getObjectType(stmt)));
     // Explicit Constructor Invocation Statement
   } else if (stmt instanceof ExplicitConstructorInvocationStmt) {
     ExplicitConstructorInvocationStmt eciStmt = (ExplicitConstructorInvocationStmt) stmt;
     tokens.add(new Token(eciStmt.getExpr().toString(), getObjectType(eciStmt)));
     // Expression Statement
   } else if (stmt instanceof ExpressionStmt) {
     ExpressionStmt exprStmt = (ExpressionStmt) stmt;
     tokens.add(new Token(exprStmt.toString(), getObjectType(exprStmt.getExpression())));
   } else {
     // this should never be called
     tokens.add(new Token(stmt.toString(), "OTHER"));
   }
 }