use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class RangeExpressionTransformer method transformRangeExpression.
public Expression transformRangeExpression(RangeExpression range) {
final ClassNode inferred = range.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
if (INTRANGE_TYPE.equals(inferred)) {
ArgumentListExpression bounds = new ArgumentListExpression(new ConstantExpression(range.isInclusive(), true), range.getFrom(), range.getTo());
ConstructorCallExpression cce = new ConstructorCallExpression(INTRANGE_TYPE, bounds);
cce.setSourcePosition(range);
cce.putNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET, INTRANGE_CTOR);
cce.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, INTRANGE_TYPE);
return transformer.transform(cce);
}
return transformer.superTransform(range);
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class BinaryExpressionTransformer method convertInOperatorToTernary.
private Expression convertInOperatorToTernary(final BinaryExpression bin, final Expression rightExpression, final Expression leftExpression) {
MethodCallExpression call = new MethodCallExpression(rightExpression, "isCase", leftExpression);
call.setMethodTarget((MethodNode) bin.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET));
call.setSourcePosition(bin);
call.copyNodeMetaData(bin);
TernaryExpression tExp = new TernaryExpression(new BooleanExpression(new BinaryExpression(rightExpression, Token.newSymbol("==", -1, -1), new ConstantExpression(null))), new BinaryExpression(leftExpression, Token.newSymbol("==", -1, -1), new ConstantExpression(null)), call);
return staticCompilationTransformer.transform(tExp);
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class JavaStubGenerator method printField.
private void printField(PrintWriter out, FieldNode fieldNode, boolean isInterface) {
if ((fieldNode.getModifiers() & Opcodes.ACC_PRIVATE) != 0)
return;
printAnnotations(out, fieldNode);
if (!isInterface) {
printModifiers(out, fieldNode.getModifiers());
}
ClassNode type = fieldNode.getType();
printType(out, type);
out.print(" ");
out.print(fieldNode.getName());
if (isInterface || (fieldNode.getModifiers() & Opcodes.ACC_FINAL) != 0) {
out.print(" = ");
Expression valueExpr = fieldNode.getInitialValueExpression();
if (valueExpr instanceof ConstantExpression) {
valueExpr = Verifier.transformToPrimitiveConstantIfPossible((ConstantExpression) valueExpr);
}
if (valueExpr instanceof ConstantExpression && fieldNode.isStatic() && fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(valueExpr.getType()) && valueExpr.getType().equals(fieldNode.getType())) {
// GROOVY-5150 : Initialize value with a dummy constant so that Java cross compiles correctly
if (ClassHelper.STRING_TYPE.equals(valueExpr.getType())) {
out.print(formatString(valueExpr.getText()));
} else if (ClassHelper.char_TYPE.equals(valueExpr.getType())) {
out.print("'" + valueExpr.getText() + "'");
} else {
ClassNode constantType = valueExpr.getType();
out.print('(');
printType(out, type);
out.print(") ");
out.print(valueExpr.getText());
if (ClassHelper.Long_TYPE.equals(ClassHelper.getWrapper(constantType)))
out.print('L');
}
} else if (ClassHelper.isPrimitiveType(type)) {
String val = type == ClassHelper.boolean_TYPE ? "false" : "0";
out.print("new " + ClassHelper.getWrapper(type) + "((" + type + ")" + val + ")");
} else {
out.print("null");
}
}
out.println(";");
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class ASTTransformationCollectorCodeVisitor method addCollectedAnnotations.
private boolean addCollectedAnnotations(List<AnnotationNode> collected, AnnotationNode aliasNode, AnnotatedNode origin) {
ClassNode classNode = aliasNode.getClassNode();
boolean ret = false;
for (AnnotationNode annotation : classNode.getAnnotations()) {
if (annotation.getClassNode().getName().equals(AnnotationCollector.class.getName())) {
Expression processorExp = annotation.getMember("processor");
AnnotationCollectorTransform act = null;
assertStringConstant(processorExp);
if (processorExp != null) {
String className = (String) ((ConstantExpression) processorExp).getValue();
Class klass = loadTransformClass(className, aliasNode);
if (klass != null) {
try {
act = (AnnotationCollectorTransform) klass.newInstance();
} catch (InstantiationException e) {
source.getErrorCollector().addErrorAndContinue(new ExceptionMessage(e, true, source));
} catch (IllegalAccessException e) {
source.getErrorCollector().addErrorAndContinue(new ExceptionMessage(e, true, source));
}
}
} else {
act = new AnnotationCollectorTransform();
}
if (act != null)
collected.addAll(act.visit(annotation, aliasNode, origin, source));
ret = true;
}
}
return ret;
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class LazyASTTransformation method visitField.
static void visitField(AnnotationNode node, FieldNode fieldNode) {
final Expression soft = node.getMember("soft");
final Expression init = getInitExpr(fieldNode);
fieldNode.rename("$" + fieldNode.getName());
fieldNode.setModifiers(ACC_PRIVATE | (fieldNode.getModifiers() & (~(ACC_PUBLIC | ACC_PROTECTED))));
if (soft instanceof ConstantExpression && ((ConstantExpression) soft).getValue().equals(true)) {
createSoft(fieldNode, init);
} else {
create(fieldNode, init);
// @Lazy not meaningful with primitive so convert to wrapper if needed
if (ClassHelper.isPrimitiveType(fieldNode.getType())) {
fieldNode.setType(ClassHelper.getWrapper(fieldNode.getType()));
}
}
}
Aggregations