use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.
the class AbstractGrailsArtefactTransformer method addEnhancedAnnotation.
protected void addEnhancedAnnotation(ClassNode classNode) {
final AnnotationNode annotationNode = GrailsASTUtils.addEnhancedAnnotation(classNode);
AnnotationNode annotation = GrailsASTUtils.findAnnotation(classNode, Mixin.class);
if (annotation != null) {
Expression value = annotation.getMember("value");
if (value != null) {
annotationNode.setMember("mixins", value);
}
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.
the class MockTransformation method visit.
@Override
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 node = (AnnotationNode) astNodes[0];
if (!MY_TYPE.equals(node.getClassNode()) || !(parent instanceof ClassNode)) {
return;
}
ClassNode classNode = (ClassNode) parent;
String cName = classNode.getName();
if (classNode.isInterface()) {
error(source, "Error processing interface '" + cName + "'. " + MY_TYPE_NAME + " not allowed for interfaces.");
}
ListExpression values = getListOfClasses(node);
if (values == null) {
error(source, "Error processing class '" + cName + "'. " + MY_TYPE_NAME + " annotation expects a class or a list of classes to mock");
return;
}
List<ClassExpression> domainClassNodes = new ArrayList<ClassExpression>();
for (Expression expression : values.getExpressions()) {
if (expression instanceof ClassExpression) {
ClassExpression classEx = (ClassExpression) expression;
ClassNode cn = classEx.getType();
Class<?> mixinClassForArtefactType = getMixinClassForArtefactType(cn);
if (mixinClassForArtefactType != null) {
weaveMock(classNode, classEx, false);
} else {
domainClassNodes.add(classEx);
}
}
}
if (!domainClassNodes.isEmpty()) {
weaveMixinClass(classNode, DomainClassUnitTestMixin.class);
addMockCollaborators(classNode, "Domain", domainClassNodes);
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.
the class TestMixinTransformation method autoAddTestAnnotation.
private void autoAddTestAnnotation(ClassNode classNode) {
if (isSpockTest(classNode))
return;
Map<String, MethodNode> declaredMethodsMap = classNode.getDeclaredMethodsMap();
for (String methodName : declaredMethodsMap.keySet()) {
MethodNode methodNode = declaredMethodsMap.get(methodName);
ClassNode testAnnotationClassNode = TEST_ANNOTATION.getClassNode();
List<AnnotationNode> existingTestAnnotations = methodNode.getAnnotations(testAnnotationClassNode);
if (isCandidateMethod(methodNode) && (methodNode.getName().startsWith("test") || existingTestAnnotations.size() > 0)) {
if (existingTestAnnotations.size() == 0) {
ClassNode returnType = methodNode.getReturnType();
if (returnType.getName().equals(VOID_TYPE)) {
methodNode.addAnnotation(TEST_ANNOTATION);
}
}
}
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.
the class LineNumberTransform method visit.
// LOG statements commented out because they were causing
// compilation problems when used outside of grails
// static final Log LOG = LogFactory.getLog(LineNumberTransform)
public void visit(ASTNode[] nodes, SourceUnit source) {
List<ClassNode> classes = source.getAST().getClasses();
AnnotationNode annotation = null;
for (ClassNode clazz : classes) {
annotation = findAnnotation(clazz);
if (annotation != null) {
break;
}
}
if (annotation == null) {
return;
}
int[] array = extractLineNumberArray(annotation);
if (array != null) {
LineNumberVisitor visitor = new LineNumberVisitor(array);
for (ClassNode clazz : classes) {
visitor.visitClass(clazz);
}
}
String sourceName = extractSourceName(annotation);
if (sourceName != null) {
source.getAST().setDescription(sourceName);
// source.name = sourceName
Field field = ReflectionUtils.findField(SourceUnit.class, "name");
field.setAccessible(true);
ReflectionUtils.setField(field, source, sourceName);
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class Java5 method setAnnotationMetaData.
private void setAnnotationMetaData(Annotation[] annotations, AnnotatedNode an) {
for (Annotation annotation : annotations) {
AnnotationNode node = new AnnotationNode(ClassHelper.make(annotation.annotationType()));
configureAnnotation(node, annotation);
an.addAnnotation(node);
}
}
Aggregations