use of org.codehaus.groovy.ast.AnnotatedNode in project grails-core by grails.
the class ArtefactTypeAstTransformation method visit.
public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {
AnnotatedNode parent = (AnnotatedNode) astNodes[1];
AnnotationNode node = (AnnotationNode) astNodes[0];
if (!(node instanceof AnnotationNode) || !(parent instanceof AnnotatedNode)) {
throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class");
}
if (!isArtefactAnnotationNode(node) || !(parent instanceof ClassNode)) {
return;
}
ClassNode cNode = (ClassNode) parent;
if (cNode.isInterface()) {
throw new RuntimeException("Error processing interface '" + cNode.getName() + "'. @" + getAnnotationType().getNameWithoutPackage() + " not allowed for interfaces.");
}
if (isApplied(cNode)) {
return;
}
String artefactType = resolveArtefactType(sourceUnit, node, cNode);
if (artefactType != null) {
AbstractGrailsArtefactTransformer.addToTransformedClasses(cNode.getName());
}
performInjectionOnArtefactType(sourceUnit, cNode, artefactType);
performTraitInjectionOnArtefactType(sourceUnit, cNode, artefactType);
postProcess(sourceUnit, node, cNode, artefactType);
markApplied(cNode);
}
use of org.codehaus.groovy.ast.AnnotatedNode in project grails-core by grails.
the class TestMixinTransformation method visit.
public void visit(ASTNode[] astNodes, SourceUnit source) {
if (!(astNodes[0] instanceof AnnotationNode) || !(astNodes[1] instanceof AnnotatedNode)) {
throw new RuntimeException("Internal error: wrong types: " + astNodes[0].getClass() + " / " + astNodes[1].getClass());
}
AnnotatedNode parent = (AnnotatedNode) astNodes[1];
AnnotationNode annotationNode = (AnnotationNode) astNodes[0];
if (!MY_TYPE.equals(annotationNode.getClassNode()) || !(parent instanceof ClassNode)) {
return;
}
String mainClass = MainClassFinder.searchMainClass(source.getSource().getURI());
ClassNode applicationClassNode = null;
if (mainClass != null) {
applicationClassNode = ClassHelper.make(mainClass);
}
ClassNode classNode = (ClassNode) parent;
ListExpression values = getListOfClasses(annotationNode);
weaveTestMixins(classNode, values, applicationClassNode);
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy-core by groovy.
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);
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy-core by groovy.
the class CanonicalASTTransformation 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;
// TODO remove - let other validation steps pick this up
if (hasAnnotation(cNode, ImmutableASTTransformation.MY_TYPE)) {
addError(MY_TYPE_NAME + " class '" + cNode.getName() + "' can't also be " + ImmutableASTTransformation.MY_TYPE_NAME, parent);
}
if (!checkNotInterface(cNode, MY_TYPE_NAME))
return;
List<String> excludes = getMemberList(anno, "excludes");
List<String> includes = getMemberList(anno, "includes");
if (!checkIncludeExclude(anno, excludes, includes, MY_TYPE_NAME))
return;
if (!hasAnnotation(cNode, TupleConstructorASTTransformation.MY_TYPE)) {
createConstructor(cNode, false, true, false, false, false, false, excludes, includes, false);
}
if (!hasAnnotation(cNode, EqualsAndHashCodeASTTransformation.MY_TYPE)) {
createHashCode(cNode, false, false, false, excludes, includes);
createEquals(cNode, false, false, true, excludes, includes);
}
if (!hasAnnotation(cNode, ToStringASTTransformation.MY_TYPE)) {
createToString(cNode, false, false, excludes, includes, false);
}
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy-core by groovy.
the class ReadWriteLockASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
final boolean isWriteLock;
if (READ_LOCK_TYPE.equals(node.getClassNode())) {
isWriteLock = false;
} else if (WRITE_LOCK_TYPE.equals(node.getClassNode())) {
isWriteLock = true;
} else {
throw new GroovyBugError("Internal error: expecting [" + READ_LOCK_TYPE.getName() + ", " + WRITE_LOCK_TYPE.getName() + "]" + " but got: " + node.getClassNode().getName());
}
String myTypeName = "@" + node.getClassNode().getNameWithoutPackage();
String value = getMemberStringValue(node, "value");
if (parent instanceof MethodNode) {
MethodNode mNode = (MethodNode) parent;
ClassNode cNode = mNode.getDeclaringClass();
String lockExpr = determineLock(value, cNode, mNode.isStatic(), myTypeName);
if (lockExpr == null)
return;
// get lock type
final Expression lockType;
if (isWriteLock) {
lockType = callX(varX(lockExpr, LOCK_TYPE), "writeLock");
} else {
lockType = callX(varX(lockExpr, LOCK_TYPE), "readLock");
}
Expression acquireLock = callX(lockType, "lock");
Expression releaseLock = callX(lockType, "unlock");
Statement originalCode = mNode.getCode();
mNode.setCode(block(stmt(acquireLock), new TryCatchStatement(originalCode, stmt(releaseLock))));
}
}
Aggregations