private TryCatchStatement tryCatchAssertionFailedError(
     AnnotationNode annotationNode, MethodNode methodNode, ArrayList<Statement> statements) {
   TryCatchStatement tryCatchStatement =
       new TryCatchStatement(
           new BlockStatement(statements, methodNode.getVariableScope()), EmptyStatement.INSTANCE);
   tryCatchStatement.addCatch(
       new CatchStatement(
           new Parameter(CATCHED_THROWABLE_TYPE, "ex"), ReturnStatement.RETURN_NULL_OR_VOID));
   return tryCatchStatement;
 }
  private void createValidateMethod() {
    Validation.Mode mode =
        getEnumMemberValue(
            getAnnotation(annotatedClass, VALIDATION_ANNOTATION),
            "mode",
            Validation.Mode.class,
            Validation.Mode.AUTOMATIC);

    annotatedClass.addField(
        "$manualValidation",
        ACC_PRIVATE,
        ClassHelper.Boolean_TYPE,
        new ConstantExpression(mode == Validation.Mode.MANUAL));
    MethodBuilder.createPublicMethod("manualValidation")
        .param(Boolean_TYPE, "validation")
        .assignS(varX("$manualValidation"), varX("validation"))
        .addTo(annotatedClass);

    MethodBuilder methodBuilder = MethodBuilder.createPublicMethod(VALIDATE_METHOD);

    if (ASTHelper.isDSLObject(annotatedClass.getSuperClass())) {
      methodBuilder.statement(callSuperX(VALIDATE_METHOD));
    }

    BlockStatement block = new BlockStatement();
    validateFields(block);
    validateCustomMethods(block);

    TryCatchStatement tryCatchStatement = new TryCatchStatement(block, EmptyStatement.INSTANCE);
    tryCatchStatement.addCatch(
        new CatchStatement(
            param(ASSERTION_ERROR_TYPE, "e"),
            new ThrowStatement(
                ctorX(VALIDATION_EXCEPTION_TYPE, args(propX(varX("e"), "message"), varX("e"))))));
    tryCatchStatement.addCatch(
        new CatchStatement(
            param(EXCEPTION_TYPE, "e"),
            new ThrowStatement(
                ctorX(VALIDATION_EXCEPTION_TYPE, args(propX(varX("e"), "message"), varX("e"))))));

    methodBuilder.statement(tryCatchStatement).addTo(annotatedClass);
  }
Ejemplo n.º 3
0
 /**
  * Wraps a method body in try / catch logic that catches any errors and logs an error, but does
  * not rethrow!
  *
  * @param methodNode The method node
  */
 public static void wrapMethodBodyInTryCatchDebugStatements(MethodNode methodNode) {
   BlockStatement code = (BlockStatement) methodNode.getCode();
   BlockStatement newCode = new BlockStatement();
   TryCatchStatement tryCatchStatement = new TryCatchStatement(code, new BlockStatement());
   newCode.addStatement(tryCatchStatement);
   methodNode.setCode(newCode);
   BlockStatement catchBlock = new BlockStatement();
   ArgumentListExpression logArguments = new ArgumentListExpression();
   logArguments.addExpression(
       new BinaryExpression(
           new ConstantExpression("Error initializing class: "),
           Token.newSymbol(Types.PLUS, 0, 0),
           new VariableExpression("e")));
   logArguments.addExpression(new VariableExpression("e"));
   catchBlock.addStatement(
       new ExpressionStatement(
           new MethodCallExpression(new VariableExpression("log"), "error", logArguments)));
   tryCatchStatement.addCatch(
       new CatchStatement(new Parameter(new ClassNode(Throwable.class), "e"), catchBlock));
 }