/**
  * Check expression statement for test assertions.
  *
  * @param estmt Expression statements.
  */
 private void checkAssertions(ExpressionStatement estmt) {
   if (estmt.getExpression() instanceof MethodInvocation) {
     MethodInvocation mi = (MethodInvocation) estmt.getExpression();
     // Increment number of test assertions.
     if (mi.getName() != null && mi.getName().getIdentifier().startsWith("assert")) {
       this.numOfTestAssertions++;
     }
   }
 }
 private boolean isAssigned(Statement statement, SimpleName name) {
   if (statement instanceof ExpressionStatement) {
     ExpressionStatement exstat = (ExpressionStatement) statement;
     if (exstat.getExpression() instanceof Assignment) {
       Assignment assignment = (Assignment) exstat.getExpression();
       return assignment.getLeftHandSide() == name;
     }
   }
   return false;
 }
Ejemplo n.º 3
0
  /**
   * Regression test: verify that a class passed in the constructor of an anonymous class is
   * converted.
   */
  public void testAnonymousClassWithTypeArgParameter() {
    List<TypeDeclaration> types =
        translateClassBody(
            "public Test(Class c) {} static Test t = "
                + "new Test(Test.class) { @Override public int hashCode() { return 1; } };");
    assertEquals(2, types.size());

    TypeDeclaration type = types.get(0);
    List<BodyDeclaration> members = type.bodyDeclarations();
    for (BodyDeclaration member : members) {
      if (member instanceof MethodDeclaration) {
        MethodDeclaration m = (MethodDeclaration) member;
        if (m.getName().getIdentifier().equals("initialize")) {
          Block b = (Block) m.getBody().statements().get(0);
          ExpressionStatement expStmt = (ExpressionStatement) b.statements().get(0);
          Assignment assign = (Assignment) expStmt.getExpression();
          ClassInstanceCreation create = (ClassInstanceCreation) assign.getRightHandSide();
          assertTrue(create.arguments().get(0) instanceof TypeLiteral);
        }
      }
    }
  }
 /*
  * @see ASTVisitor#visit(ExpressionStatement)
  */
 public boolean visit(ExpressionStatement node) {
   node.getExpression().accept(this);
   this.fBuffer.append(";"); // $NON-NLS-1$
   return false;
 }
Ejemplo n.º 5
0
  // generate a variable declaration with an initializer specified by the given expression
  // e.g. given x.f(a,b) ==> int y = x.f(a,b);
  public static List<Statement> genVarDeclStatement(Expression exp) {

    List<Statement> result = new ArrayList<Statement>();

    VariableDeclarationFragment fragment = AST.newAST(AST.JLS8).newVariableDeclarationFragment();

    ExpressionStatement assignmentStmt = genAssignmentStatement(exp);

    // The type of the generated variable
    Type varType = AST.newAST(AST.JLS8).newWildcardType();
    if (exp.resolveTypeBinding() != null) {
      if (exp.resolveTypeBinding().isPrimitive()) {
        switch (exp.resolveTypeBinding().getName()) {
          case "void":
            varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.VOID);
            break;
          case "int":
            varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.INT);
            break;
          case "char":
            varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.CHAR);
            break;
          case "long":
            varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.LONG);
            break;
          case "boolean":
            varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.BOOLEAN);
            break;
          case "float":
            varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.FLOAT);
            break;
          case "short":
            varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.SHORT);
            break;
          case "byte":
            varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.BYTE);
            break;
          case "double":
            varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.DOUBLE);
            break;
        }
      } else {

        // Option 1: only use the simplename
        /*
        SimpleName typeName = AST.newAST(AST.JLS8).newSimpleName(exp.resolveTypeBinding().getName());
        AST tempAST = AST.newAST(AST.JLS8);
        varType = tempAST.newSimpleType((Name) ASTNode.copySubtree(tempAST, typeName));
        */

        varType = resolveQualifiedType(exp.resolveTypeBinding().getQualifiedName());
      }
    }
    // Declaration Fragment
    fragment.setName(
        (SimpleName)
            ASTNode.copySubtree(
                fragment.getAST(),
                ((SimpleName) ((Assignment) assignmentStmt.getExpression()).getLeftHandSide())));
    // fragment.setInitializer((Expression) ASTNode.copySubtree(fragment.getAST(), exp));

    AST varDeclFragAST = AST.newAST(AST.JLS8);
    VariableDeclarationStatement decl =
        varDeclFragAST.newVariableDeclarationStatement(
            (VariableDeclarationFragment) ASTNode.copySubtree(varDeclFragAST, fragment));

    decl.setType((Type) ASTNode.copySubtree(decl.getAST(), varType));

    result.add(decl);

    // initializer is defined here as a separate statement
    Assignment assign = varDeclFragAST.newAssignment();
    assign.setLeftHandSide((Expression) ASTNode.copySubtree(varDeclFragAST, fragment.getName()));
    assign.setRightHandSide((Expression) ASTNode.copySubtree(varDeclFragAST, exp));
    ExpressionStatement assignStmt = varDeclFragAST.newExpressionStatement(assign);

    result.add(assignStmt);

    return result;
  }