/** * @return Returns the interruption check statement. */ protected Statement createInterruptStatement() { return ifS(createCondition(), throwS( ctorX(thrownExceptionType, args(constX(getErrorMessage()))) ) ); }
/** * Takes a statement and wraps it into a block statement which first element is the interruption check statement. * * @param statement the statement to be wrapped * @return a {@link BlockStatement block statement} which first element is for checking interruption, and the * second one the statement to be wrapped. */ protected final Statement wrapBlock(Statement statement) { BlockStatement stmt = new BlockStatement(); stmt.addStatement(createInterruptStatement()); stmt.addStatement(statement); return stmt; }
protected void setupTransform(AnnotationNode node) { checkOnMethodStart = getBooleanAnnotationParameter(node, CHECK_METHOD_START_MEMBER, true); applyToAllMembers = getBooleanAnnotationParameter(node, APPLY_TO_ALL_MEMBERS, true); applyToAllClasses = applyToAllMembers && getBooleanAnnotationParameter(node, APPLY_TO_ALL_CLASSES, true); thrownExceptionType = getClassAnnotationParameter(node, THROWN_EXCEPTION_TYPE, ClassHelper.make(InterruptedException.class)); }
public void visit(ASTNode[] nodes, SourceUnit source) { if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) { internalError("Expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes)); if (!type().equals(node.getClassNode())) { internalError("Transformation called from wrong annotation: " + node.getClassNode().getName()); setupTransform(node); final List<ClassNode> classes = tree.getClasses(); for (ClassNode classNode : classes) { visitClass(classNode); this.visitClass((ClassNode) annotatedNode); } else if (!applyToAllMembers && annotatedNode instanceof MethodNode) { this.visitMethod((MethodNode) annotatedNode); this.visitClass(annotatedNode.getDeclaringClass()); } else if (!applyToAllMembers && annotatedNode instanceof FieldNode) { this.visitField((FieldNode) annotatedNode); this.visitClass(annotatedNode.getDeclaringClass()); } else if (!applyToAllMembers && annotatedNode instanceof DeclarationExpression) { this.visitDeclarationExpression((DeclarationExpression) annotatedNode); this.visitClass(annotatedNode.getDeclaringClass()); } else { for (ClassNode classNode : classes) { if (classNode.isScript()) { visitClass(classNode);
public void visit(ASTNode[] nodes, SourceUnit source) { if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) { internalError("Expecting [AnnotationNode, AnnotatedClass] but got: " + Arrays.asList(nodes)); AnnotatedNode annotatedNode = (AnnotatedNode) nodes[1]; if (!type().equals(node.getClassNode())) { internalError("Transformation called from wrong annotation: " + node.getClassNode().getName()); setupTransform(node); final List<ClassNode> classes = tree.getClasses(); for (ClassNode classNode : classes) { visitClass(classNode); this.visitClass((ClassNode) annotatedNode); } else { for (ClassNode classNode : classes) { if (classNode.isScript()) { visitClass(classNode);
@Override public final void visitForLoop(ForStatement forStatement) { visitLoop(forStatement); super.visitForLoop(forStatement); }
protected static ClassNode getClassAnnotationParameter(AnnotationNode node, String parameterName, ClassNode defaultValue) { Expression member = node.getMember(parameterName); if (member != null) { if (member instanceof ClassExpression) { try { return member.getType(); } catch (Exception e) { internalError("Expecting class value for " + parameterName + " annotation parameter. Found " + member + "member"); } } else { internalError("Expecting class value for " + parameterName + " annotation parameter. Found " + member + "member"); } } return defaultValue; }
@Override public final void visitWhileLoop(WhileStatement whileStatement) { visitLoop(whileStatement); super.visitWhileLoop(whileStatement); } }
protected static boolean getBooleanAnnotationParameter(AnnotationNode node, String parameterName, boolean defaultValue) { Expression member = node.getMember(parameterName); if (member != null) { if (member instanceof ConstantExpression) { try { return DefaultGroovyMethods.asType(((ConstantExpression) member).getValue(), Boolean.class); } catch (Exception e) { internalError("Expecting boolean value for " + parameterName + " annotation parameter. Found " + member + "member"); } } else { internalError("Expecting boolean value for " + parameterName + " annotation parameter. Found " + member + "member"); } } return defaultValue; }
protected void setupTransform(AnnotationNode node) { checkOnMethodStart = getBooleanAnnotationParameter(node, CHECK_METHOD_START_MEMBER, true); applyToAllClasses = getBooleanAnnotationParameter(node, PROPAGATE_TO_COMPILE_UNIT, true); thrownExceptionType = getClassAnnotationParameter(node, THROWN_EXCEPTION_TYPE, ClassHelper.make(InterruptedException.class)); }
/** * @return Returns the interruption check statement. */ protected Statement createInterruptStatement() { return new IfStatement( new BooleanExpression( createCondition() ), new ThrowStatement( new ConstructorCallExpression(thrownExceptionType, new ArgumentListExpression(new ConstantExpression(getErrorMessage()))) ), new EmptyStatement() ); }
@Override public final void visitDoWhileLoop(DoWhileStatement doWhileStatement) { visitLoop(doWhileStatement); super.visitDoWhileLoop(doWhileStatement); }
protected static ClassNode getClassAnnotationParameter(AnnotationNode node, String parameterName, ClassNode defaultValue) { Expression member = node.getMember(parameterName); if (member != null) { if (member instanceof ClassExpression) { try { return member.getType(); } catch (Exception e) { internalError("Expecting class value for " + parameterName + " annotation parameter. Found " + member + "member"); } } else { internalError("Expecting class value for " + parameterName + " annotation parameter. Found " + member + "member"); } } return defaultValue; }
/** * Takes a statement and wraps it into a block statement which first element is the interruption check statement. * * @param statement the statement to be wrapped * @return a {@link BlockStatement block statement} which first element is for checking interruption, and the * second one the statement to be wrapped. */ protected final Statement wrapBlock(Statement statement) { BlockStatement stmt = new BlockStatement(); stmt.addStatement(createInterruptStatement()); stmt.addStatement(statement); return stmt; }
@Override public final void visitDoWhileLoop(DoWhileStatement doWhileStatement) { visitLoop(doWhileStatement); super.visitDoWhileLoop(doWhileStatement); }
protected static boolean getBooleanAnnotationParameter(AnnotationNode node, String parameterName, boolean defaultValue) { Expression member = node.getMember(parameterName); if (member != null) { if (member instanceof ConstantExpression) { try { return DefaultGroovyMethods.asType(((ConstantExpression) member).getValue(), Boolean.class); } catch (Exception e) { internalError("Expecting boolean value for " + parameterName + " annotation parameter. Found " + member + "member"); } } else { internalError("Expecting boolean value for " + parameterName + " annotation parameter. Found " + member + "member"); } } return defaultValue; }
@Override public final void visitWhileLoop(WhileStatement whileStatement) { visitLoop(whileStatement); super.visitWhileLoop(whileStatement); } }
@Override public final void visitForLoop(ForStatement forStatement) { visitLoop(forStatement); super.visitForLoop(forStatement); }