use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class IndexedPropertyASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(node.getClassNode()))
return;
if (parent instanceof FieldNode) {
FieldNode fNode = (FieldNode) parent;
ClassNode cNode = fNode.getDeclaringClass();
if (cNode.getProperty(fNode.getName()) == null) {
addError("Error during " + MY_TYPE_NAME + " processing. Field '" + fNode.getName() + "' doesn't appear to be a property; incorrect visibility?", fNode);
return;
}
ClassNode fType = fNode.getType();
if (fType.isArray()) {
addArraySetter(fNode);
addArrayGetter(fNode);
} else if (fType.isDerivedFrom(LIST_TYPE)) {
addListSetter(fNode);
addListGetter(fNode);
} else {
addError("Error during " + MY_TYPE_NAME + " processing. Non-Indexable property '" + fNode.getName() + "' found. Type must be array or list but found " + fType.getName(), fNode);
}
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class InheritConstructorsASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(node.getClassNode()))
return;
if (parent instanceof ClassNode) {
processClass((ClassNode) parent, node);
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class NotYetImplementedASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
throw new RuntimeException("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
}
AnnotationNode annotationNode = (AnnotationNode) nodes[0];
ASTNode node = nodes[1];
if (!(node instanceof MethodNode)) {
addError("@NotYetImplemented must only be applied on test methods!", node);
return;
}
MethodNode methodNode = (MethodNode) node;
ArrayList<Statement> statements = new ArrayList<Statement>();
Statement statement = methodNode.getCode();
if (statement instanceof BlockStatement) {
statements.addAll(((BlockStatement) statement).getStatements());
}
if (statements.isEmpty())
return;
BlockStatement rewrittenMethodCode = new BlockStatement();
rewrittenMethodCode.addStatement(tryCatchAssertionFailedError(annotationNode, methodNode, statements));
rewrittenMethodCode.addStatement(throwAssertionFailedError(annotationNode));
methodNode.setCode(rewrittenMethodCode);
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class BindableASTTransformation method visit.
/**
* Handles the bulk of the processing, mostly delegating to other methods.
*
* @param nodes the ast nodes
* @param source the source unit for the nodes
*/
public void visit(ASTNode[] nodes, SourceUnit source) {
if (!(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class");
}
AnnotationNode node = (AnnotationNode) nodes[0];
AnnotatedNode parent = (AnnotatedNode) nodes[1];
if (VetoableASTTransformation.hasVetoableAnnotation(parent)) {
// VetoableASTTransformation will handle both @Bindable and @Vetoable
return;
}
ClassNode declaringClass = parent.getDeclaringClass();
if (parent instanceof FieldNode) {
if ((((FieldNode) parent).getModifiers() & Opcodes.ACC_FINAL) != 0) {
source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Bindable cannot annotate a final property.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
}
if (VetoableASTTransformation.hasVetoableAnnotation(parent.getDeclaringClass())) {
// VetoableASTTransformation will handle both @Bindable and @Vetoable
return;
}
addListenerToProperty(source, node, declaringClass, (FieldNode) parent);
} else if (parent instanceof ClassNode) {
addListenerToClass(source, (ClassNode) parent);
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class AbstractInterruptibleASTTransformation method visit.
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));
}
this.source = source;
AnnotationNode node = (AnnotationNode) nodes[0];
AnnotatedNode annotatedNode = (AnnotatedNode) nodes[1];
if (!type().equals(node.getClassNode())) {
internalError("Transformation called from wrong annotation: " + node.getClassNode().getName());
}
setupTransform(node);
// should be limited to the current SourceUnit or propagated to the whole CompilationUnit
final ModuleNode tree = source.getAST();
if (applyToAllClasses) {
// guard every class and method defined in this script
if (tree != null) {
final List<ClassNode> classes = tree.getClasses();
for (ClassNode classNode : classes) {
visitClass(classNode);
}
}
} else if (annotatedNode instanceof ClassNode) {
// only guard this particular class
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 {
// only guard the script class
if (tree != null) {
final List<ClassNode> classes = tree.getClasses();
for (ClassNode classNode : classes) {
if (classNode.isScript()) {
visitClass(classNode);
}
}
}
}
}
Aggregations