use of org.codehaus.groovy.ast.expr.ConstantExpression 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.ConstantExpression 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)));
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class BinaryExpressionTransformer method transformDeclarationExpression.
private static Expression transformDeclarationExpression(final BinaryExpression bin) {
Expression leftExpression = bin.getLeftExpression();
if (leftExpression instanceof VariableExpression) {
if (ClassHelper.char_TYPE.equals(((VariableExpression) leftExpression).getOriginType())) {
Expression rightExpression = bin.getRightExpression();
if (rightExpression instanceof ConstantExpression && ClassHelper.STRING_TYPE.equals(rightExpression.getType())) {
String text = (String) ((ConstantExpression) rightExpression).getValue();
if (text.length() == 1) {
// optimize char initialization
ConstantExpression ce = new ConstantExpression(text.charAt(0), true);
ce.setSourcePosition(rightExpression);
bin.setRightExpression(ce);
return bin;
}
}
}
}
return null;
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class BinaryExpressionTransformer method optimizeConstantInitialization.
private static DeclarationExpression optimizeConstantInitialization(final BinaryExpression originalDeclaration, final Token operation, final ConstantExpression constant, final Expression leftExpression, final ClassNode declarationType) {
ConstantExpression cexp = new ConstantExpression(convertConstant((Number) constant.getValue(), ClassHelper.getWrapper(declarationType)), true);
cexp.setType(declarationType);
cexp.setSourcePosition(constant);
DeclarationExpression result = new DeclarationExpression(leftExpression, operation, cexp);
result.setSourcePosition(originalDeclaration);
result.copyNodeMetaData(originalDeclaration);
return result;
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class BinaryExpressionTransformer method tryOptimizeCharComparison.
private static BinaryExpression tryOptimizeCharComparison(final Expression left, final Expression right, final BinaryExpression bin) {
int op = bin.getOperation().getType();
if (isCompareToBoolean(op) || op == COMPARE_EQUAL || op == COMPARE_NOT_EQUAL) {
Character cLeft = tryCharConstant(left);
Character cRight = tryCharConstant(right);
if (cLeft != null || cRight != null) {
Expression oLeft = cLeft == null ? left : new ConstantExpression(cLeft, true);
oLeft.setSourcePosition(left);
Expression oRight = cRight == null ? right : new ConstantExpression(cRight, true);
oRight.setSourcePosition(right);
bin.setLeftExpression(oLeft);
bin.setRightExpression(oRight);
return bin;
}
}
return null;
}
Aggregations