Ejemplo n.º 1
0
 protected void addMockCollaborator(
     String mockType, ClassExpression targetClass, BlockStatement methodBody) {
   ArgumentListExpression args = new ArgumentListExpression();
   args.addExpression(targetClass);
   methodBody
       .getStatements()
       .add(
           0,
           new ExpressionStatement(
               new MethodCallExpression(THIS_EXPRESSION, "mock" + mockType, args)));
 }
Ejemplo n.º 2
0
  private IfStatement getAutowiringIfStatement(
      ClassExpression targetClass,
      VariableExpression fieldExpression,
      BinaryExpression testTargetAssignment) {
    VariableExpression appCtxVar = new VariableExpression("applicationContext");

    BooleanExpression applicationContextCheck =
        new BooleanExpression(
            new BinaryExpression(
                new BinaryExpression(
                    fieldExpression,
                    GrailsASTUtils.EQUALS_OPERATOR,
                    GrailsASTUtils.NULL_EXPRESSION),
                Token.newSymbol("&&", 0, 0),
                new BinaryExpression(
                    appCtxVar,
                    GrailsASTUtils.NOT_EQUALS_OPERATOR,
                    GrailsASTUtils.NULL_EXPRESSION)));
    BlockStatement performAutowireBlock = new BlockStatement();
    ArgumentListExpression arguments = new ArgumentListExpression();
    arguments.addExpression(fieldExpression);
    arguments.addExpression(new ConstantExpression(1));
    arguments.addExpression(new ConstantExpression(false));
    BlockStatement assignFromApplicationContext = new BlockStatement();
    ArgumentListExpression argWithClassName = new ArgumentListExpression();
    MethodCallExpression getClassNameMethodCall =
        new MethodCallExpression(targetClass, "getName", new ArgumentListExpression());
    argWithClassName.addExpression(getClassNameMethodCall);

    assignFromApplicationContext.addStatement(
        new ExpressionStatement(
            new BinaryExpression(
                fieldExpression,
                ASSIGN,
                new MethodCallExpression(appCtxVar, "getBean", argWithClassName))));
    BlockStatement elseBlock = new BlockStatement();
    elseBlock.addStatement(new ExpressionStatement(testTargetAssignment));
    performAutowireBlock.addStatement(
        new IfStatement(
            new BooleanExpression(
                new MethodCallExpression(appCtxVar, "containsBean", argWithClassName)),
            assignFromApplicationContext,
            elseBlock));
    performAutowireBlock.addStatement(
        new ExpressionStatement(
            new MethodCallExpression(
                new PropertyExpression(appCtxVar, "autowireCapableBeanFactory"),
                "autowireBeanProperties",
                arguments)));
    return new IfStatement(applicationContextCheck, performAutowireBlock, new BlockStatement());
  }