use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class Verifier method transformToPrimitiveConstantIfPossible.
/**
* When constant expressions are created, the value is always wrapped to a non primitive type.
* Some constant expressions are optimized to return primitive types, but not all primitives are
* handled. This method guarantees to return a similar constant expression but with a primitive type
* instead of a boxed type.
*
* Additionaly, single char strings are converted to 'char' types.
*
* @param constantExpression a constant expression
* @return the same instance of constant expression if the type is already primitive, or a primitive
* constant if possible.
*/
public static ConstantExpression transformToPrimitiveConstantIfPossible(ConstantExpression constantExpression) {
Object value = constantExpression.getValue();
if (value == null)
return constantExpression;
ConstantExpression result;
ClassNode type = constantExpression.getType();
if (ClassHelper.isPrimitiveType(type))
return constantExpression;
if (value instanceof String && ((String) value).length() == 1) {
result = new ConstantExpression(((String) value).charAt(0));
result.setType(ClassHelper.char_TYPE);
} else {
type = ClassHelper.getUnwrapper(type);
result = new ConstantExpression(value, true);
result.setType(type);
}
return result;
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class BinaryExpressionHelper method assignToArray.
protected void assignToArray(Expression parent, Expression receiver, Expression index, Expression rhsValueLoader) {
// let's replace this assignment to a subscript operator with a
// method call
// e.g. x[5] = 10
// -> (x, [], 5), =, 10
// -> methodCall(x, "putAt", [5, 10])
ArgumentListExpression ae = new ArgumentListExpression(index, rhsValueLoader);
controller.getInvocationWriter().makeCall(parent, receiver, new ConstantExpression("putAt"), ae, InvocationWriter.invokeMethod, false, false, false);
controller.getOperandStack().pop();
// return value of assignment
rhsValueLoader.visit(controller.getAcg());
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project gradle by gradle.
the class PluginUseScriptBlockMetadataExtractor method extract.
public void extract(SourceUnit sourceUnit, ScriptBlock scriptBlock) {
ClosureExpression closureArg = scriptBlock.getClosureExpression();
closureArg.getCode().visit(new RestrictiveCodeVisitor(sourceUnit, formatErrorMessage(BASE_MESSAGE)) {
@Override
public void visitBlockStatement(BlockStatement block) {
for (Statement statement : block.getStatements()) {
statement.visit(this);
}
}
@Override
public void visitMethodCallExpression(MethodCallExpression call) {
if (!call.isImplicitThis()) {
Expression target = call.getObjectExpression();
if (!(target instanceof MethodCallExpression)) {
restrict(target, formatErrorMessage(BASE_MESSAGE));
return;
}
visitMethodCallExpression((MethodCallExpression) target);
}
if (call.getMethod() instanceof ConstantExpression) {
ConstantExpression methodName = (ConstantExpression) call.getMethod();
if (isOfType(methodName, String.class)) {
String methodNameText = methodName.getText();
if (methodNameText.equals("id") || methodNameText.equals("version")) {
ConstantExpression argumentExpression = hasSingleConstantArgOfType(call, String.class);
if (argumentExpression == null) {
restrict(call, formatErrorMessage(NEED_SINGLE_STRING));
return;
}
String argStringValue = argumentExpression.getValue().toString();
if (argStringValue.length() == 0) {
restrict(argumentExpression, formatErrorMessage(NEED_SINGLE_STRING));
return;
}
if (methodName.getText().equals("id")) {
if (call.isImplicitThis()) {
try {
DefaultPluginId.validate(argStringValue);
call.setNodeMetaData(PluginDependencySpec.class, pluginRequestCollector.createSpec(call.getLineNumber()).id(argStringValue));
} catch (InvalidPluginIdException e) {
restrict(argumentExpression, formatErrorMessage(e.getReason()));
}
} else {
restrict(call, formatErrorMessage(BASE_MESSAGE));
}
}
if (methodName.getText().equals("version")) {
PluginDependencySpec spec = getSpecFor(call);
if (spec == null) {
return;
}
spec.version(argStringValue);
call.setNodeMetaData(PluginDependencySpec.class, spec);
}
} else if (methodNameText.equals("apply")) {
ConstantExpression arguments = hasSingleConstantArgOfType(call, boolean.class);
if (arguments == null) {
restrict(call, formatErrorMessage(NEED_SINGLE_BOOLEAN));
return;
}
PluginDependencySpec spec = getSpecFor(call);
if (spec == null) {
return;
}
spec.apply((Boolean) arguments.getValue());
} else {
if (!call.isImplicitThis()) {
restrict(methodName, formatErrorMessage(EXTENDED_MESSAGE));
} else {
restrict(methodName, formatErrorMessage(BASE_MESSAGE));
}
}
} else {
restrict(methodName, formatErrorMessage(NOT_LITERAL_ID_METHOD_NAME));
}
} else {
restrict(call);
}
}
private PluginDependencySpec getSpecFor(MethodCallExpression call) {
Expression objectExpression = call.getObjectExpression();
if (objectExpression instanceof MethodCallExpression) {
return objectExpression.getNodeMetaData(PluginDependencySpec.class);
} else {
restrict(call, formatErrorMessage(BASE_MESSAGE));
return null;
}
}
@Override
public void visitExpressionStatement(ExpressionStatement statement) {
statement.getExpression().visit(this);
}
});
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project grails-core by grails.
the class AbstractGrailsArtefactTransformer method addApiLookupFieldAndSetter.
protected void addApiLookupFieldAndSetter(ClassNode classNode, ClassNode implementationNode, String apiProperty, Expression initialValueExpression) {
FieldNode fieldNode = classNode.getField(apiProperty);
if (fieldNode == null || !fieldNode.getDeclaringClass().equals(classNode)) {
fieldNode = new FieldNode(apiProperty, Modifier.PRIVATE | Modifier.STATIC, implementationNode, classNode, initialValueExpression);
classNode.addField(fieldNode);
String setterName = "set" + MetaClassHelper.capitalize(apiProperty);
Parameter setterParameter = new Parameter(implementationNode, apiProperty);
BlockStatement setterBody = new BlockStatement();
setterBody.addStatement(new ExpressionStatement(new BinaryExpression(new AttributeExpression(new ClassExpression(classNode), new ConstantExpression(apiProperty)), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression(setterParameter))));
GrailsASTUtils.addCompileStaticAnnotation(classNode.addMethod(setterName, Modifier.PUBLIC | Modifier.STATIC, ClassHelper.VOID_TYPE, new Parameter[] { setterParameter }, null, setterBody));
}
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project grails-core by grails.
the class ArtefactTypeAstTransformation method postProcess.
protected void postProcess(SourceUnit sourceUnit, AnnotationNode annotationNode, ClassNode classNode, String artefactType) {
if (!getAnnotationType().equals(annotationNode.getClassNode())) {
// add @Artefact annotation to resulting class so that "short cut" annotations like @TagLib
// also produce an @Artefact annotation in the resulting class file
AnnotationNode annotation = new AnnotationNode(getAnnotationType());
annotation.addMember("value", new ConstantExpression(artefactType));
classNode.addAnnotation(annotation);
}
}
Aggregations