use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class AsmClassGenerator method determineCommonArrayType.
private static int determineCommonArrayType(final List<Expression> values) {
Expression expr = values.get(0);
int arrayElementType = -1;
if (expr instanceof AnnotationConstantExpression) {
arrayElementType = 1;
} else if (expr instanceof ConstantExpression) {
arrayElementType = 2;
} else if (expr instanceof ClassExpression || expr instanceof ClosureExpression) {
arrayElementType = 3;
} else if (expr instanceof PropertyExpression) {
arrayElementType = 4;
}
return arrayElementType;
}
use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class AsmClassGenerator method visitAnnotationAttributes.
/**
* Generates the annotation attributes.
*
* @param an the node with an annotation
* @param av the visitor to use
*/
private void visitAnnotationAttributes(final AnnotationNode an, final AnnotationVisitor av) {
Map<String, Object> constantAttrs = new HashMap<>();
Map<String, PropertyExpression> enumAttrs = new HashMap<>();
Map<String, Object> atAttrs = new HashMap<>();
Map<String, ListExpression> arrayAttrs = new HashMap<>();
for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
String name = member.getKey();
Expression expr = member.getValue();
if (expr instanceof AnnotationConstantExpression) {
atAttrs.put(name, ((AnnotationConstantExpression) expr).getValue());
} else if (expr instanceof ConstantExpression) {
constantAttrs.put(name, ((ConstantExpression) expr).getValue());
} else if (expr instanceof ClassExpression) {
constantAttrs.put(name, Type.getType(BytecodeHelper.getTypeDescription((expr.getType()))));
} else if (expr instanceof PropertyExpression) {
enumAttrs.put(name, (PropertyExpression) expr);
} else if (expr instanceof ListExpression) {
arrayAttrs.put(name, (ListExpression) expr);
} else if (expr instanceof ClosureExpression) {
ClassNode closureClass = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) expr, ACC_PUBLIC);
constantAttrs.put(name, Type.getType(BytecodeHelper.getTypeDescription(closureClass)));
}
}
for (Map.Entry<String, Object> entry : constantAttrs.entrySet()) {
av.visit(entry.getKey(), entry.getValue());
}
for (Map.Entry<String, PropertyExpression> entry : enumAttrs.entrySet()) {
PropertyExpression propExp = entry.getValue();
av.visitEnum(entry.getKey(), BytecodeHelper.getTypeDescription(propExp.getObjectExpression().getType()), String.valueOf(((ConstantExpression) propExp.getProperty()).getValue()));
}
for (Map.Entry<String, Object> entry : atAttrs.entrySet()) {
AnnotationNode atNode = (AnnotationNode) entry.getValue();
AnnotationVisitor av2 = av.visitAnnotation(entry.getKey(), BytecodeHelper.getTypeDescription(atNode.getClassNode()));
visitAnnotationAttributes(atNode, av2);
av2.visitEnd();
}
visitArrayAttributes(an, arrayAttrs, av);
}
use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.
the class ExtendedVerifier method processDuplicateAnnotationContainers.
private void processDuplicateAnnotationContainers(AnnotatedNode node, Map<String, List<AnnotationNode>> nonSourceAnnotations) {
for (Map.Entry<String, List<AnnotationNode>> next : nonSourceAnnotations.entrySet()) {
if (next.getValue().size() > 1) {
ClassNode repeatable = null;
AnnotationNode repeatee = next.getValue().get(0);
for (AnnotationNode anno : repeatee.getClassNode().getAnnotations()) {
if (anno.getClassNode().getName().equals("java.lang.annotation.Repeatable")) {
Expression value = anno.getMember("value");
if (value instanceof ClassExpression && value.getType().isAnnotationDefinition()) {
repeatable = value.getType();
break;
}
}
}
if (repeatable != null) {
if (nonSourceAnnotations.containsKey(repeatable.getName())) {
addError("Cannot specify duplicate annotation on the same member. Explicit " + repeatable.getName() + " found when creating implicit container for " + next.getKey(), node);
}
AnnotationNode collector = new AnnotationNode(repeatable);
if (repeatee.hasRuntimeRetention()) {
collector.setRuntimeRetention(true);
} else if (repeatable.isResolved()) {
Class<?> repeatableType = repeatable.getTypeClass();
Retention retention = repeatableType.getAnnotation(Retention.class);
collector.setRuntimeRetention(retention != null && retention.value().equals(RetentionPolicy.RUNTIME));
} else {
for (AnnotationNode annotation : repeatable.getAnnotations()) {
if (annotation.getClassNode().getName().equals("java.lang.annotation.Retention")) {
Expression value = annotation.getMember("value");
assert value != null;
Object retention = evaluateExpression(value, source.getConfiguration());
collector.setRuntimeRetention(retention != null && retention.toString().equals("RUNTIME"));
break;
}
}
}
collector.addMember("value", new ListExpression(next.getValue().stream().map(AnnotationConstantExpression::new).collect(Collectors.toList())));
node.getAnnotations().removeAll(next.getValue());
node.addAnnotation(collector);
}
}
}
}
use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project gradle by gradle.
the class GradleResolveVisitor method transformAnnotationConstantExpression.
@Override
protected Expression transformAnnotationConstantExpression(AnnotationConstantExpression ace) {
AnnotationNode an = (AnnotationNode) ace.getValue();
ClassNode type = an.getClassNode();
resolveOrFail(type, ", unable to find class for annotation", an);
for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
member.setValue(transform(member.getValue()));
}
return ace;
}
Aggregations