use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class ResolveVisitor method transformAnnotationConstantExpression.
protected Expression transformAnnotationConstantExpression(final AnnotationConstantExpression ace) {
AnnotationNode an = (AnnotationNode) ace.getValue();
ClassNode type = an.getClassNode();
resolveOrFail(type, " for annotation", an);
for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
member.setValue(transform(member.getValue()));
}
return ace;
}
use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class StaticImportVisitor method transform.
@Override
public Expression transform(Expression exp) {
if (exp == null)
return null;
Class<? extends Expression> clazz = exp.getClass();
if (clazz == VariableExpression.class) {
return transformVariableExpression((VariableExpression) exp);
}
if (clazz == BinaryExpression.class) {
return transformBinaryExpression((BinaryExpression) exp);
}
if (clazz == PropertyExpression.class) {
return transformPropertyExpression((PropertyExpression) exp);
}
if (clazz == MethodCallExpression.class) {
return transformMethodCallExpression((MethodCallExpression) exp);
}
if (exp instanceof ClosureExpression) {
return transformClosureExpression((ClosureExpression) exp);
}
if (clazz == ConstructorCallExpression.class) {
return transformConstructorCallExpression((ConstructorCallExpression) exp);
}
if (clazz == ArgumentListExpression.class) {
Expression result = exp.transformExpression(this);
if (foundArgs == null && inPropertyExpression) {
foundArgs = result;
}
return result;
}
if (exp instanceof ConstantExpression) {
Expression result = exp.transformExpression(this);
if (foundConstant == null && inPropertyExpression) {
foundConstant = result;
}
if (inAnnotation && exp instanceof AnnotationConstantExpression) {
ConstantExpression ce = (ConstantExpression) result;
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();
Map<String, Expression> attributes = an.getMembers();
for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
Expression attrExpr = transform(entry.getValue());
entry.setValue(attrExpr);
}
}
}
return result;
}
return exp.transformExpression(this);
}
use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class AsmClassGenerator method visitAnnotationDefaultExpression.
private void visitAnnotationDefaultExpression(final AnnotationVisitor av, final ClassNode type, final Expression exp) {
if (exp instanceof ClosureExpression) {
ClassNode closureClass = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) exp, ACC_PUBLIC);
Type t = Type.getType(BytecodeHelper.getTypeDescription(closureClass));
av.visit(null, t);
} else if (type.isArray()) {
AnnotationVisitor avl = av.visitArray(null);
ClassNode componentType = type.getComponentType();
if (exp instanceof ListExpression) {
ListExpression list = (ListExpression) exp;
for (Expression lExp : list.getExpressions()) {
visitAnnotationDefaultExpression(avl, componentType, lExp);
}
} else {
visitAnnotationDefaultExpression(avl, componentType, exp);
}
} else if (isPrimitiveType(type) || isStringType(type)) {
ConstantExpression constExp = (ConstantExpression) exp;
av.visit(null, constExp.getValue());
} else if (isClassType(type)) {
ClassNode clazz = exp.getType();
Type t = Type.getType(BytecodeHelper.getTypeDescription(clazz));
av.visit(null, t);
} else if (type.isDerivedFrom(ClassHelper.Enum_Type)) {
PropertyExpression pExp = (PropertyExpression) exp;
ClassExpression cExp = (ClassExpression) pExp.getObjectExpression();
String desc = BytecodeHelper.getTypeDescription(cExp.getType());
String name = pExp.getPropertyAsString();
av.visitEnum(null, desc, name);
} else if (type.implementsInterface(ClassHelper.Annotation_TYPE)) {
AnnotationConstantExpression avExp = (AnnotationConstantExpression) exp;
AnnotationNode value = (AnnotationNode) avExp.getValue();
AnnotationVisitor avc = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(avExp.getType()));
visitAnnotationAttributes(value, avc);
} else {
throw new GroovyBugError("unexpected annotation type " + type.getName());
}
av.visitEnd();
}
use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class AnnotationVisitor method visitExpression.
protected void visitExpression(String attrName, Expression attrExp, ClassNode attrType) {
if (attrType.isArray()) {
// check needed as @Test(attr = {"elem"}) passes through the parser
if (attrExp instanceof ListExpression) {
ListExpression le = (ListExpression) attrExp;
visitListExpression(attrName, le, attrType.getComponentType());
} else if (attrExp instanceof ClosureExpression) {
addError("Annotation list attributes must use Groovy notation [el1, el2]", attrExp);
} else {
// treat like a singleton list as per Java
ListExpression listExp = new ListExpression();
listExp.addExpression(attrExp);
if (annotation != null) {
annotation.setMember(attrName, listExp);
}
visitExpression(attrName, listExp, attrType);
}
} else if (ClassHelper.isPrimitiveType(attrType)) {
visitConstantExpression(attrName, getConstantExpression(attrExp, attrType), ClassHelper.getWrapper(attrType));
} else if (isStringType(attrType)) {
visitConstantExpression(attrName, getConstantExpression(attrExp, attrType), ClassHelper.STRING_TYPE);
} else if (isClassType(attrType)) {
if (!(attrExp instanceof ClassExpression || attrExp instanceof ClosureExpression)) {
addError("Only classes and closures can be used for attribute '" + attrName + "'", attrExp);
}
} else if (attrType.isDerivedFrom(ClassHelper.Enum_Type)) {
if (attrExp instanceof PropertyExpression) {
visitEnumExpression(attrName, (PropertyExpression) attrExp, attrType);
} else if (attrExp instanceof ConstantExpression) {
visitConstantExpression(attrName, getConstantExpression(attrExp, attrType), attrType);
} else {
addError("Expected enum value for attribute " + attrName, attrExp);
}
} else if (isValidAnnotationClass(attrType)) {
if (attrExp instanceof AnnotationConstantExpression) {
visitAnnotationExpression(attrName, (AnnotationConstantExpression) attrExp, attrType);
} else {
addError("Expected annotation of type '" + attrType.getName() + "' for attribute " + attrName, attrExp);
}
} else {
addError("Unexpected type " + attrType.getName(), attrExp);
}
}
use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class Annotations method annotationValueToExpression.
private static Expression annotationValueToExpression(Object value, AsmReferenceResolver resolver) {
if (value instanceof TypeWrapper) {
ClassNode type = resolver.resolveClassNullable(Type.getType(((TypeWrapper) value).desc).getClassName());
return type != null ? new ClassExpression(type) : null;
}
if (value instanceof EnumConstantWrapper) {
EnumConstantWrapper wrapper = (EnumConstantWrapper) value;
return new PropertyExpression(new ClassExpression(resolver.resolveType(Type.getType(wrapper.enumDesc))), wrapper.constant);
}
if (value instanceof AnnotationStub) {
AnnotationNode annotationNode = createAnnotationNode((AnnotationStub) value, resolver);
return annotationNode != null ? new AnnotationConstantExpression(annotationNode) : nullX();
}
if (value != null && value.getClass().isArray()) {
ListExpression elementExprs = new ListExpression();
int len = Array.getLength(value);
for (int i = 0; i != len; ++i) {
elementExprs.addExpression(annotationValueToExpression(Array.get(value, i), resolver));
}
return elementExprs;
}
if (value instanceof List) {
ListExpression elementExprs = new ListExpression();
for (Object o : (List) value) {
elementExprs.addExpression(annotationValueToExpression(o, resolver));
}
return elementExprs;
}
return new ConstantExpression(value);
}
Aggregations