use of org.codehaus.groovy.ast.expr.Expression in project groovy by apache.
the class AnnotationVisitor method visit.
public AnnotationNode visit(AnnotationNode node) {
this.annotation = node;
this.reportClass = node.getClassNode();
if (!isValidAnnotationClass(node.getClassNode())) {
addError("class " + node.getClassNode().getName() + " is not an annotation");
return node;
}
// check if values have been passed for all annotation attributes that don't have defaults
if (!checkIfMandatoryAnnotationValuesPassed(node)) {
return node;
}
// if enum constants have been used, check if they are all valid
if (!checkIfValidEnumConstsAreUsed(node)) {
return node;
}
Map<String, Expression> attributes = node.getMembers();
for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
String attrName = entry.getKey();
Expression attrExpr = transformInlineConstants(entry.getValue());
entry.setValue(attrExpr);
ClassNode attrType = getAttributeType(node, attrName);
visitExpression(attrName, attrExpr, attrType);
}
VMPluginFactory.getPlugin().configureAnnotation(node);
return this.annotation;
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy by apache.
the class GeneralUtils method copyAnnotatedNodeAnnotations.
/**
* Copies all <tt>candidateAnnotations</tt> with retention policy {@link java.lang.annotation.RetentionPolicy#RUNTIME}
* and {@link java.lang.annotation.RetentionPolicy#CLASS}.
* <p>
* Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not supported at present.
*/
public static void copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, final List<AnnotationNode> copied, List<AnnotationNode> notCopied) {
List<AnnotationNode> annotationList = annotatedNode.getAnnotations();
for (AnnotationNode annotation : annotationList) {
List<AnnotationNode> annotations = annotation.getClassNode().getAnnotations(AbstractASTTransformation.RETENTION_CLASSNODE);
if (annotations.isEmpty())
continue;
if (hasClosureMember(annotation)) {
notCopied.add(annotation);
continue;
}
AnnotationNode retentionPolicyAnnotation = annotations.get(0);
Expression valueExpression = retentionPolicyAnnotation.getMember("value");
if (!(valueExpression instanceof PropertyExpression))
continue;
PropertyExpression propertyExpression = (PropertyExpression) valueExpression;
boolean processAnnotation = propertyExpression.getProperty() instanceof ConstantExpression && ("RUNTIME".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue()) || "CLASS".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue()));
if (processAnnotation) {
AnnotationNode newAnnotation = new AnnotationNode(annotation.getClassNode());
for (Map.Entry<String, Expression> member : annotation.getMembers().entrySet()) {
newAnnotation.addMember(member.getKey(), member.getValue());
}
newAnnotation.setSourcePosition(annotatedNode);
copied.add(newAnnotation);
}
}
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy by apache.
the class GeneralUtils method copyStatementsWithSuperAdjustment.
public static boolean copyStatementsWithSuperAdjustment(ClosureExpression pre, BlockStatement body) {
Statement preCode = pre.getCode();
boolean changed = false;
if (preCode instanceof BlockStatement) {
BlockStatement block = (BlockStatement) preCode;
List<Statement> statements = block.getStatements();
for (int i = 0; i < statements.size(); i++) {
Statement statement = statements.get(i);
// adjust the first statement if it's a super call
if (i == 0 && statement instanceof ExpressionStatement) {
ExpressionStatement es = (ExpressionStatement) statement;
Expression preExp = es.getExpression();
if (preExp instanceof MethodCallExpression) {
MethodCallExpression mce = (MethodCallExpression) preExp;
String name = mce.getMethodAsString();
if ("super".equals(name)) {
es.setExpression(new ConstructorCallExpression(ClassNode.SUPER, mce.getArguments()));
changed = true;
}
}
}
body.addStatement(statement);
}
}
return changed;
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy by apache.
the class GeneralUtils method hasClosureMember.
private static boolean hasClosureMember(AnnotationNode annotation) {
Map<String, Expression> members = annotation.getMembers();
for (Map.Entry<String, Expression> member : members.entrySet()) {
if (member.getValue() instanceof ClosureExpression)
return true;
if (member.getValue() instanceof ClassExpression) {
ClassExpression classExpression = (ClassExpression) member.getValue();
Class<?> typeClass = classExpression.getType().isResolved() ? classExpression.getType().redirect().getTypeClass() : null;
if (typeClass != null && GeneratedClosure.class.isAssignableFrom(typeClass))
return true;
}
}
return false;
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy by apache.
the class GeneralUtils method createConstructorStatementDefault.
public static Statement createConstructorStatementDefault(FieldNode fNode) {
final String name = fNode.getName();
final ClassNode fType = fNode.getType();
final Expression fieldExpr = propX(varX("this"), name);
Expression initExpr = fNode.getInitialValueExpression();
Statement assignInit;
if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
if (ClassHelper.isPrimitiveType(fType)) {
assignInit = EmptyStatement.INSTANCE;
} else {
assignInit = assignS(fieldExpr, ConstantExpression.EMPTY_EXPRESSION);
}
} else {
assignInit = assignS(fieldExpr, initExpr);
}
fNode.setInitialValueExpression(null);
Expression value = findArg(name);
return ifElseS(equalsNullX(value), assignInit, assignS(fieldExpr, castX(fType, value)));
}
Aggregations