use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class AbstractASTTransformation method copyAnnotatedNodeAnnotations.
/**
* Copies all <tt>candidateAnnotations</tt> with retention policy {@link java.lang.annotation.RetentionPolicy#RUNTIME}
* and {@link java.lang.annotation.RetentionPolicy#CLASS}.
* <p>
* Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not supported for now.
*/
protected List<AnnotationNode> copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, String myTypeName) {
final List<AnnotationNode> copiedAnnotations = new ArrayList<AnnotationNode>();
final List<AnnotationNode> notCopied = new ArrayList<AnnotationNode>();
GeneralUtils.copyAnnotatedNodeAnnotations(annotatedNode, copiedAnnotations, notCopied);
for (AnnotationNode annotation : notCopied) {
addError(myTypeName + " does not support keeping Closure annotation members.", annotation);
}
return copiedAnnotations;
}
use of org.codehaus.groovy.ast.AnnotationNode 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);
}
}
}
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class AutoImplementASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode anno = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(anno.getClassNode()))
return;
if (parent instanceof ClassNode) {
ClassNode cNode = (ClassNode) parent;
if (!checkNotInterface(cNode, MY_TYPE_NAME))
return;
ClassNode exception = getMemberClassValue(anno, "exception");
if (exception != null && Undefined.isUndefinedException(exception)) {
exception = null;
}
String message = getMemberStringValue(anno, "message");
Expression code = anno.getMember("code");
if (code != null && !(code instanceof ClosureExpression)) {
addError("Expected closure value for annotation parameter 'code'. Found " + code, cNode);
return;
}
createMethods(cNode, exception, message, (ClosureExpression) code);
if (code != null) {
anno.setMember("code", new ClosureExpression(new Parameter[0], new EmptyStatement()));
}
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class BaseScriptASTTransformation 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 DeclarationExpression) {
changeBaseScriptTypeFromDeclaration((DeclarationExpression) parent, node);
} else if (parent instanceof ImportNode || parent instanceof PackageNode) {
changeBaseScriptTypeFromPackageOrImport(source, parent, node);
} else if (parent instanceof ClassNode) {
changeBaseScriptTypeFromClass((ClassNode) parent, node);
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class BuilderASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode anno = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(anno.getClassNode()))
return;
if (parent instanceof ClassNode || parent instanceof MethodNode) {
if (parent instanceof ClassNode && !checkNotInterface((ClassNode) parent, MY_TYPE_NAME))
return;
if (parent instanceof MethodNode && !checkStatic((MethodNode) parent, MY_TYPE_NAME))
return;
final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
final BuilderStrategy strategy = createBuilderStrategy(anno, classLoader);
if (strategy == null)
return;
strategy.build(this, parent, anno);
}
}
Aggregations