use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class AsmClassGenerator method visitAnnotationArrayElement.
private void visitAnnotationArrayElement(final Expression expr, final int arrayElementType, final AnnotationVisitor av) {
switch(arrayElementType) {
case 1:
AnnotationNode atAttr = (AnnotationNode) ((AnnotationConstantExpression) expr).getValue();
AnnotationVisitor av2 = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(atAttr.getClassNode()));
visitAnnotationAttributes(atAttr, av2);
av2.visitEnd();
break;
case 2:
av.visit(null, ((ConstantExpression) expr).getValue());
break;
case 3:
ClassNode type = expr.getType();
if (expr instanceof ClosureExpression) {
type = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) expr, ACC_PUBLIC);
}
av.visit(null, Type.getType(BytecodeHelper.getTypeDescription(type)));
break;
case 4:
PropertyExpression propExpr = (PropertyExpression) expr;
av.visitEnum(null, BytecodeHelper.getTypeDescription(propExpr.getObjectExpression().getType()), String.valueOf(((ConstantExpression) propExpr.getProperty()).getValue()));
break;
}
}
use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class AnnotationVisitor method checkCircularReference.
public void checkCircularReference(ClassNode searchClass, ClassNode attrType, Expression startExp) {
if (!isValidAnnotationClass(attrType))
return;
if (!(startExp instanceof AnnotationConstantExpression)) {
addError("Found '" + startExp.getText() + "' when expecting an Annotation Constant", startExp);
return;
}
AnnotationConstantExpression ace = (AnnotationConstantExpression) startExp;
AnnotationNode annotationNode = (AnnotationNode) ace.getValue();
if (annotationNode.getClassNode().equals(searchClass)) {
addError("Circular reference discovered in " + searchClass.getName(), startExp);
return;
}
ClassNode cn = annotationNode.getClassNode();
for (MethodNode method : cn.getMethods()) {
if (method.getReturnType().equals(searchClass)) {
addError("Circular reference discovered in " + cn.getName(), startExp);
}
ReturnStatement code = (ReturnStatement) method.getCode();
if (code == null)
continue;
checkCircularReference(searchClass, method.getReturnType(), code.getExpression());
}
}
use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class AnnotationCollectorTransform method makeExpression.
private static Expression makeExpression(Object o) {
if (o instanceof Class) {
return new ClassExpression(ClassHelper.make((Class<?>) o));
}
// TODO: value as Annotation here!
if (o instanceof Object[][]) {
List<AnnotationNode> annotations = makeListOfAnnotations((Object[][]) o);
ListExpression le = new ListExpression();
for (AnnotationNode an : annotations) {
le.addExpression(new AnnotationConstantExpression(an));
}
return le;
} else if (o instanceof Object[]) {
ListExpression le = new ListExpression();
Object[] values = (Object[]) o;
for (Object val : values) {
le.addExpression(makeExpression(val));
}
return le;
}
return new ConstantExpression(o, true);
}
use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class StaticTypeCheckingVisitor method checkNamedParamsAnnotation.
private void checkNamedParamsAnnotation(final Parameter param, final MapExpression args) {
if (!isOrImplements(param.getType(), MAP_TYPE))
return;
List<MapEntryExpression> entryExpressions = args.getMapEntryExpressions();
Map<Object, Expression> entries = new LinkedHashMap<>();
for (MapEntryExpression entry : entryExpressions) {
Object key = entry.getKeyExpression();
if (key instanceof ConstantExpression) {
key = ((ConstantExpression) key).getValue();
}
entries.put(key, entry.getValueExpression());
}
List<String> collectedNames = new ArrayList<>();
List<AnnotationNode> annotations = param.getAnnotations(NAMED_PARAMS_CLASSNODE);
if (annotations != null && !annotations.isEmpty()) {
AnnotationNode an = null;
for (AnnotationNode next : annotations) {
if (next.getClassNode().getName().equals(NamedParams.class.getName())) {
an = next;
}
}
if (an != null) {
Expression value = an.getMember("value");
if (value instanceof AnnotationConstantExpression) {
processNamedParam((AnnotationConstantExpression) value, entries, args, collectedNames);
} else if (value instanceof ListExpression) {
ListExpression le = (ListExpression) value;
for (Expression next : le.getExpressions()) {
if (next instanceof AnnotationConstantExpression) {
processNamedParam((AnnotationConstantExpression) next, entries, args, collectedNames);
}
}
}
}
}
annotations = param.getAnnotations(NAMED_PARAM_CLASSNODE);
if (annotations != null && !annotations.isEmpty()) {
for (AnnotationNode next : annotations) {
if (next.getClassNode().getName().equals(NamedParam.class.getName())) {
processNamedParam(next, entries, args, collectedNames);
}
}
}
if (!collectedNames.isEmpty()) {
for (Map.Entry<Object, Expression> entry : entries.entrySet()) {
if (!collectedNames.contains(entry.getKey())) {
addStaticTypeError("unexpected named arg: " + entry.getKey(), args);
}
}
}
}
use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression 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;
}
Aggregations