use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy-core by groovy.
the class TraitASTTransformation method processProperty.
/**
* Mostly copied from the {@link Verifier} class but does *not* generate bytecode
*
* @param cNode
* @param node
*/
private static void processProperty(final ClassNode cNode, PropertyNode node) {
String name = node.getName();
FieldNode field = node.getField();
int propNodeModifiers = node.getModifiers();
String getterName = "get" + Verifier.capitalize(name);
String setterName = "set" + Verifier.capitalize(name);
// GROOVY-3726: clear volatile, transient modifiers so that they don't get applied to methods
if ((propNodeModifiers & Modifier.VOLATILE) != 0) {
propNodeModifiers = propNodeModifiers - Modifier.VOLATILE;
}
if ((propNodeModifiers & Modifier.TRANSIENT) != 0) {
propNodeModifiers = propNodeModifiers - Modifier.TRANSIENT;
}
Statement getterBlock = node.getGetterBlock();
if (getterBlock == null) {
MethodNode getter = cNode.getGetterMethod(getterName);
if (getter == null && ClassHelper.boolean_TYPE == node.getType()) {
String secondGetterName = "is" + Verifier.capitalize(name);
getter = cNode.getGetterMethod(secondGetterName);
}
if (!node.isPrivate() && methodNeedsReplacement(cNode, getter)) {
getterBlock = new ExpressionStatement(new FieldExpression(field));
}
}
Statement setterBlock = node.getSetterBlock();
if (setterBlock == null) {
// 2nd arg false below: though not usual, allow setter with non-void return type
MethodNode setter = cNode.getSetterMethod(setterName, false);
if (!node.isPrivate() && (propNodeModifiers & ACC_FINAL) == 0 && methodNeedsReplacement(cNode, setter)) {
setterBlock = new ExpressionStatement(new BinaryExpression(new FieldExpression(field), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression("value")));
}
}
if (getterBlock != null) {
MethodNode getter = new MethodNode(getterName, propNodeModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
getter.setSynthetic(true);
cNode.addMethod(getter);
if (ClassHelper.boolean_TYPE == node.getType() || ClassHelper.Boolean_TYPE == node.getType()) {
String secondGetterName = "is" + Verifier.capitalize(name);
MethodNode secondGetter = new MethodNode(secondGetterName, propNodeModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
secondGetter.setSynthetic(true);
cNode.addMethod(secondGetter);
}
}
if (setterBlock != null) {
Parameter[] setterParameterTypes = { new Parameter(node.getType(), "value") };
VariableExpression var = (VariableExpression) ((BinaryExpression) ((ExpressionStatement) setterBlock).getExpression()).getRightExpression();
var.setAccessedVariable(setterParameterTypes[0]);
MethodNode setter = new MethodNode(setterName, propNodeModifiers, ClassHelper.VOID_TYPE, setterParameterTypes, ClassNode.EMPTY_ARRAY, setterBlock);
setter.setSynthetic(true);
cNode.addMethod(setter);
}
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy-core by groovy.
the class TraitASTTransformation method processField.
private void processField(final FieldNode field, final MethodNode initializer, final MethodNode staticInitializer, final ClassNode fieldHelper, final ClassNode trait, final Set<String> knownFields) {
Expression initialExpression = field.getInitialExpression();
MethodNode selectedMethod = field.isStatic() ? staticInitializer : initializer;
if (initialExpression != null) {
VariableExpression thisObject = new VariableExpression(selectedMethod.getParameters()[0]);
ExpressionStatement initCode = new ExpressionStatement(initialExpression);
processBody(thisObject, selectedMethod, initCode, trait, fieldHelper, knownFields);
BlockStatement code = (BlockStatement) selectedMethod.getCode();
MethodCallExpression mce;
if (field.isStatic()) {
mce = new MethodCallExpression(new ClassExpression(INVOKERHELPER_CLASSNODE), "invokeStaticMethod", new ArgumentListExpression(thisObject, new ConstantExpression(Traits.helperSetterName(field)), initCode.getExpression()));
} else {
mce = new MethodCallExpression(new CastExpression(createReceiverType(field.isStatic(), fieldHelper), thisObject), Traits.helperSetterName(field), new CastExpression(field.getOriginType(), initCode.getExpression()));
}
mce.setImplicitThis(false);
mce.setSourcePosition(initialExpression);
code.addStatement(new ExpressionStatement(mce));
}
// define setter/getter helper methods
fieldHelper.addMethod(Traits.helperSetterName(field), ACC_PUBLIC | ACC_ABSTRACT, field.getOriginType(), new Parameter[] { new Parameter(field.getOriginType(), "val") }, ClassNode.EMPTY_ARRAY, null);
fieldHelper.addMethod(Traits.helperGetterName(field), ACC_PUBLIC | ACC_ABSTRACT, field.getOriginType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
// dummy fields are only used to carry annotations if instance field
// and to differentiate from static fields otherwise
String dummyFieldName = (field.isStatic() ? Traits.STATIC_FIELD_PREFIX : Traits.FIELD_PREFIX) + (field.isPublic() ? Traits.PUBLIC_FIELD_PREFIX : Traits.PRIVATE_FIELD_PREFIX) + Traits.remappedFieldName(field.getOwner(), field.getName());
FieldNode dummyField = new FieldNode(dummyFieldName, ACC_STATIC | ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC, field.getOriginType(), fieldHelper, null);
// copy annotations from field to dummy field
List<AnnotationNode> copied = new LinkedList<AnnotationNode>();
List<AnnotationNode> notCopied = new LinkedList<AnnotationNode>();
GeneralUtils.copyAnnotatedNodeAnnotations(field, copied, notCopied);
dummyField.addAnnotations(copied);
fieldHelper.addField(dummyField);
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project grails-core by grails.
the class ControllerActionTransformer method initializeStringParameter.
protected void initializeStringParameter(final ClassNode classNode, final BlockStatement wrapper, final Parameter param, final String requestParameterName) {
final ClassNode paramTypeClassNode = param.getType();
final String methodParamName = param.getName();
Expression getParamsExpression = buildGetPropertyExpression(new VariableExpression("this"), "params", classNode);
final Expression paramsContainsKeyMethodArguments = new ArgumentListExpression(new ConstantExpression(requestParameterName));
final BooleanExpression containsKeyExpression = new BooleanExpression(applyDefaultMethodTarget(new MethodCallExpression(getParamsExpression, "containsKey", paramsContainsKeyMethodArguments), Map.class));
final Statement initializeParameterStatement = new ExpressionStatement(new DeclarationExpression(new VariableExpression(methodParamName, paramTypeClassNode), Token.newSymbol(Types.EQUALS, 0, 0), new TernaryExpression(containsKeyExpression, buildGetMapExpression(getParamsExpression, requestParameterName), new ConstantExpression(null))));
wrapper.addStatement(initializeParameterStatement);
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project grails-core by grails.
the class ControllerActionTransformer method addMethodToInvokeClosure.
protected void addMethodToInvokeClosure(ClassNode controllerClassNode, PropertyNode closureProperty, SourceUnit source, GeneratorContext context) {
MethodNode method = controllerClassNode.getMethod(closureProperty.getName(), ZERO_PARAMETERS);
if (method == null || !method.getDeclaringClass().equals(controllerClassNode)) {
ClosureExpression closureExpression = (ClosureExpression) closureProperty.getInitialExpression();
final Parameter[] parameters = closureExpression.getParameters();
final BlockStatement newMethodCode = initializeActionParameters(controllerClassNode, closureProperty, closureProperty.getName(), parameters, source, context);
final ArgumentListExpression closureInvocationArguments = new ArgumentListExpression();
if (parameters != null) {
for (Parameter p : parameters) {
closureInvocationArguments.addExpression(new VariableExpression(p.getName()));
}
}
final MethodCallExpression methodCallExpression = new MethodCallExpression(closureExpression, "call", closureInvocationArguments);
newMethodCode.addStatement(new ExpressionStatement(applyMethodTarget(methodCallExpression, Closure.class, Object.class)));
final MethodNode methodNode = new MethodNode(closureProperty.getName(), Modifier.PUBLIC, new ClassNode(Object.class), ZERO_PARAMETERS, EMPTY_CLASS_ARRAY, newMethodCode);
wrapMethodBodyWithExceptionHandling(controllerClassNode, methodNode);
annotateActionMethod(controllerClassNode, parameters, methodNode);
controllerClassNode.addMethod(methodNode);
}
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project grails-core by grails.
the class DefaultASTValidateableHelper method addStaticInitializer.
private void addStaticInitializer(final ClassNode classNode) {
final Expression nullOutConstrainedPropertiesExpression = new BinaryExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME), Token.newSymbol(Types.EQUALS, 0, 0), new ConstantExpression(null));
List<Statement> statements = new ArrayList<Statement>();
statements.add(new ExpressionStatement(nullOutConstrainedPropertiesExpression));
classNode.addStaticInitializerStatements(statements, true);
}
Aggregations