use of org.codehaus.groovy.ast.AnnotationNode in project gradle by gradle.
the class GradleResolveVisitor method transformInlineConstants.
// resolve constant-looking expressions statically (do here as gets transformed away later)
private Expression transformInlineConstants(Expression exp) {
if (exp instanceof PropertyExpression) {
PropertyExpression pe = (PropertyExpression) exp;
if (pe.getObjectExpression() instanceof ClassExpression) {
ClassExpression ce = (ClassExpression) pe.getObjectExpression();
ClassNode type = ce.getType();
if (type.isEnum()) {
return exp;
}
FieldNode fn = type.getField(pe.getPropertyAsString());
if (fn != null && !fn.isEnum() && fn.isStatic() && fn.isFinal()) {
if (fn.getInitialValueExpression() instanceof ConstantExpression) {
return fn.getInitialValueExpression();
}
}
}
} else if (exp instanceof ListExpression) {
ListExpression le = (ListExpression) exp;
ListExpression result = new ListExpression();
for (Expression e : le.getExpressions()) {
result.addExpression(transformInlineConstants(e));
}
return result;
} else if (exp instanceof AnnotationConstantExpression) {
ConstantExpression ce = (ConstantExpression) exp;
if (ce.getValue() instanceof AnnotationNode) {
// replicate a little bit of AnnotationVisitor here
// because we can't wait until later to do this
AnnotationNode an = (AnnotationNode) ce.getValue();
for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
member.setValue(transformInlineConstants(member.getValue()));
}
}
}
return exp;
}
use of org.codehaus.groovy.ast.AnnotationNode in project gradle by gradle.
the class GradleResolveVisitor method visitAnnotations.
public void visitAnnotations(AnnotatedNode node) {
List<AnnotationNode> annotations = node.getAnnotations();
if (annotations.isEmpty()) {
return;
}
Map<String, AnnotationNode> tmpAnnotations = new HashMap<String, AnnotationNode>();
ClassNode annType;
for (AnnotationNode an : annotations) {
// skip built-in properties
if (an.isBuiltIn()) {
continue;
}
annType = an.getClassNode();
resolveOrFail(annType, ", unable to find class for annotation", an);
for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
Expression newValue = transform(member.getValue());
newValue = transformInlineConstants(newValue);
member.setValue(newValue);
checkAnnotationMemberValue(newValue);
}
if (annType.isResolved()) {
Class annTypeClass = annType.getTypeClass();
Retention retAnn = (Retention) annTypeClass.getAnnotation(Retention.class);
if (retAnn != null && retAnn.value().equals(RetentionPolicy.RUNTIME)) {
AnnotationNode anyPrevAnnNode = tmpAnnotations.put(annTypeClass.getName(), an);
if (anyPrevAnnNode != null) {
addError("Cannot specify duplicate annotation on the same member : " + annType.getName(), an);
}
}
}
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.
the class EntityASTTransformation method visit.
public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {
if (!(astNodes[0] instanceof AnnotationNode) || !(astNodes[1] instanceof AnnotatedNode)) {
throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class");
}
AnnotatedNode parent = (AnnotatedNode) astNodes[1];
AnnotationNode node = (AnnotationNode) astNodes[0];
if (!MY_TYPE.equals(node.getClassNode()) || !(parent instanceof ClassNode)) {
return;
}
ClassNode cNode = (ClassNode) parent;
String cName = cNode.getName();
if (cNode.isInterface()) {
throw new RuntimeException("Error processing interface '" + cName + "'. " + MY_TYPE_NAME + " not allowed for interfaces.");
}
applyTransformation(sourceUnit, cNode);
}
use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.
the class ApiDelegateTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
throw new GroovyBugError("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
}
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode annotationNode = (AnnotationNode) nodes[0];
if (parent instanceof FieldNode) {
Expression value = annotationNode.getMember("value");
FieldNode fieldNode = (FieldNode) parent;
final ClassNode type = fieldNode.getType();
final ClassNode owner = fieldNode.getOwner();
ClassNode supportedType = owner;
if (value instanceof ClassExpression) {
supportedType = value.getType();
}
GrailsASTUtils.addDelegateInstanceMethods(supportedType, owner, type, new VariableExpression(fieldNode.getName()), resolveGenericsPlaceHolders(supportedType), isNoNullCheck(), isUseCompileStatic());
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.
the class ArtefactTypeAstTransformation method postProcess.
protected void postProcess(SourceUnit sourceUnit, AnnotationNode annotationNode, ClassNode classNode, String artefactType) {
if (!getAnnotationType().equals(annotationNode.getClassNode())) {
// add @Artefact annotation to resulting class so that "short cut" annotations like @TagLib
// also produce an @Artefact annotation in the resulting class file
AnnotationNode annotation = new AnnotationNode(getAnnotationType());
annotation.addMember("value", new ConstantExpression(artefactType));
classNode.addAnnotation(annotation);
}
}
Aggregations