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;
 }
 public void visitTryCatchFinally(TryCatchStatement statement) {
   statement.getTryStatement().visit(this);
   for (CatchStatement catchStatement : statement.getCatchStatements()) {
     catchStatement.visit(this);
   }
   Statement finallyStatement = statement.getFinallyStatement();
   if (finallyStatement instanceof EmptyStatement) {
     // dispatching to EmptyStatement will not call back visitor,
     // must call our visitEmptyStatement explicitly
     visitEmptyStatement((EmptyStatement) finallyStatement);
   } else {
     finallyStatement.visit(this);
   }
 }
  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);
  }
Пример #4
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));
 }
Пример #5
0
 @Override
 public void visitTryCatchFinally(final TryCatchStatement statement) {
   visitStatement(statement);
   Map<Variable, VariableState> beforeTryCatch = new HashMap<Variable, VariableState>(getState());
   statement.getTryStatement().visit(this);
   for (CatchStatement catchStatement : statement.getCatchStatements()) {
     catchStatement.visit(this);
   }
   Statement finallyStatement = statement.getFinallyStatement();
   // we need to recall which final variables are unassigned so cloning the current state
   Map<Variable, VariableState> afterTryCatchState =
       new HashMap<Variable, VariableState>(getState());
   if (finallyStatement instanceof EmptyStatement) {
     // dispatching to EmptyStatement will not call back visitor,
     // must call our visitEmptyStatement explicitly
     visitEmptyStatement((EmptyStatement) finallyStatement);
   } else {
     finallyStatement.visit(this);
   }
   // and now we must reset to uninitialized state variables which were only initialized during
   // try/catch
   Map<Variable, VariableState> afterFinally = new HashMap<Variable, VariableState>(getState());
   for (Map.Entry<Variable, VariableState> entry : afterFinally.entrySet()) {
     Variable var = entry.getKey();
     VariableState afterFinallyState = entry.getValue();
     VariableState beforeTryCatchState = beforeTryCatch.get(var);
     if (afterFinallyState == VariableState.is_final
         && beforeTryCatchState != VariableState.is_final
         && afterTryCatchState.get(var) != beforeTryCatchState) {
       getState()
           .put(
               var,
               beforeTryCatchState == null ? VariableState.is_uninitialized : beforeTryCatchState);
     }
   }
 }