use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class BindableASTTransformation method wrapSetterMethod.
/*
* Wrap an existing setter.
*/
private static void wrapSetterMethod(ClassNode classNode, String propertyName) {
String getterName = "get" + MetaClassHelper.capitalize(propertyName);
MethodNode setter = classNode.getSetterMethod("set" + MetaClassHelper.capitalize(propertyName));
if (setter != null) {
// Get the existing code block
Statement code = setter.getCode();
Expression oldValue = varX("$oldValue");
Expression newValue = varX("$newValue");
BlockStatement block = new BlockStatement();
// create a local variable to hold the old value from the getter
block.addStatement(declS(oldValue, callThisX(getterName)));
// call the existing block, which will presumably set the value properly
block.addStatement(code);
// get the new value to emit in the event
block.addStatement(declS(newValue, callThisX(getterName)));
// add the firePropertyChange method call
block.addStatement(stmt(callThisX("firePropertyChange", args(constX(propertyName), oldValue, newValue))));
// replace the existing code block with our new one
setter.setCode(block);
}
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class VetoableASTTransformation method wrapSetterMethod.
/**
* Wrap an existing setter.
*/
private static void wrapSetterMethod(ClassNode classNode, boolean bindable, String propertyName) {
String getterName = "get" + MetaClassHelper.capitalize(propertyName);
MethodNode setter = classNode.getSetterMethod("set" + MetaClassHelper.capitalize(propertyName));
if (setter != null) {
// Get the existing code block
Statement code = setter.getCode();
Expression oldValue = varX("$oldValue");
Expression newValue = varX("$newValue");
Expression proposedValue = varX(setter.getParameters()[0].getName());
BlockStatement block = new BlockStatement();
// create a local variable to hold the old value from the getter
block.addStatement(declS(oldValue, callThisX(getterName)));
// add the fireVetoableChange method call
block.addStatement(stmt(callThisX("fireVetoableChange", args(constX(propertyName), oldValue, proposedValue))));
// call the existing block, which will presumably set the value properly
block.addStatement(code);
if (bindable) {
// get the new value to emit in the event
block.addStatement(declS(newValue, callThisX(getterName)));
// add the firePropertyChange method call
block.addStatement(stmt(callThisX("firePropertyChange", args(constX(propertyName), oldValue, newValue))));
}
// replace the existing code block with our new one
setter.setCode(block);
}
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class GrabAnnotationTransformation method callGrabAsStaticInitIfNeeded.
private void callGrabAsStaticInitIfNeeded(ClassNode classNode, ClassNode grapeClassNode, List<Map<String, Object>> grabMapsInit, List<Map<String, Object>> grabExcludeMaps) {
List<Statement> grabInitializers = new ArrayList<Statement>();
MapExpression basicArgs = new MapExpression();
if (autoDownload != null) {
basicArgs.addMapEntryExpression(constX(AUTO_DOWNLOAD_SETTING), constX(autoDownload));
}
if (disableChecksums != null) {
basicArgs.addMapEntryExpression(constX(DISABLE_CHECKSUMS_SETTING), constX(disableChecksums));
}
if (systemProperties != null && !systemProperties.isEmpty()) {
BlockStatement block = new BlockStatement();
for (Map.Entry e : systemProperties.entrySet()) {
block.addStatement(stmt(callX(SYSTEM_CLASSNODE, "setProperty", args(constX(e.getKey()), constX(e.getValue())))));
}
StaticMethodCallExpression enabled = callX(SYSTEM_CLASSNODE, "getProperty", args(constX("groovy.grape.enable"), constX("true")));
grabInitializers.add(ifS(eqX(enabled, constX("true")), block));
}
if (!grabExcludeMaps.isEmpty()) {
ListExpression list = new ListExpression();
for (Map<String, Object> map : grabExcludeMaps) {
Set<Map.Entry<String, Object>> entries = map.entrySet();
MapExpression inner = new MapExpression();
for (Map.Entry<String, Object> entry : entries) {
inner.addMapEntryExpression(constX(entry.getKey()), constX(entry.getValue()));
}
list.addExpression(inner);
}
basicArgs.addMapEntryExpression(constX("excludes"), list);
}
List<Expression> argList = new ArrayList<Expression>();
argList.add(basicArgs);
if (grabMapsInit.isEmpty())
return;
for (Map<String, Object> grabMap : grabMapsInit) {
// add Grape.grab(excludeArgs, [group:group, module:module, version:version, classifier:classifier])
// or Grape.grab([group:group, module:module, version:version, classifier:classifier])
MapExpression dependencyArg = new MapExpression();
for (String s : GRAB_REQUIRED) {
dependencyArg.addMapEntryExpression(constX(s), constX(grabMap.get(s)));
}
for (String s : GRAB_OPTIONAL) {
if (grabMap.containsKey(s))
dependencyArg.addMapEntryExpression(constX(s), constX(grabMap.get(s)));
}
argList.add(dependencyArg);
}
grabInitializers.add(stmt(callX(grapeClassNode, "grab", args(argList))));
// insert at beginning so we have the classloader set up before the class is called
classNode.addStaticInitializerStatements(grabInitializers, true);
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class DefaultStrategy method createBuildMethod.
private static MethodNode createBuildMethod(AnnotationNode anno, ClassNode buildee, List<FieldNode> fields) {
String buildMethodName = getMemberStringValue(anno, "buildMethodName", "build");
final BlockStatement body = new BlockStatement();
body.addStatement(returnS(initializeInstance(buildee, fields, body)));
return new MethodNode(buildMethodName, ACC_PUBLIC, newClass(buildee), NO_PARAMS, NO_EXCEPTIONS, body);
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class JavaAwareResolveVisitor method getConstructorCall.
private static Expression getConstructorCall(Statement code) {
if (code == null)
return null;
if (code instanceof BlockStatement) {
BlockStatement bs = (BlockStatement) code;
if (bs.isEmpty())
return null;
return getConstructorCall(bs.getStatements().get(0));
}
if (!(code instanceof ExpressionStatement))
return null;
ExpressionStatement es = (ExpressionStatement) code;
Expression exp = es.getExpression();
if (!(exp instanceof ConstructorCallExpression))
return null;
ConstructorCallExpression cce = (ConstructorCallExpression) exp;
if (!cce.isSpecialCall())
return null;
return cce;
}
Aggregations