use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class TraitComposer method doCreateSuperForwarder.
/**
* Creates a method to dispatch to "super traits" in a "stackable" fashion. The generated method looks like this:
* <p>
* <code>ReturnType trait$super$method(Class clazz, Arg1 arg1, Arg2 arg2, ...) {
* if (SomeTrait.is(A) { return SomeOtherTrait$Trait$Helper.method(this, arg1, arg2) }
* super.method(arg1,arg2)
* }</code>
* </p>
* @param targetNode
* @param forwarderMethod
* @param interfacesToGenerateForwarderFor
* @param genericsSpec
*/
private static void doCreateSuperForwarder(ClassNode targetNode, MethodNode forwarderMethod, ClassNode[] interfacesToGenerateForwarderFor, Map<String, ClassNode> genericsSpec) {
Parameter[] parameters = forwarderMethod.getParameters();
Parameter[] superForwarderParams = new Parameter[parameters.length];
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
ClassNode originType = parameter.getOriginType();
superForwarderParams[i] = new Parameter(correctToGenericsSpecRecurse(genericsSpec, originType), parameter.getName());
}
for (int i = 0; i < interfacesToGenerateForwarderFor.length; i++) {
final ClassNode current = interfacesToGenerateForwarderFor[i];
final ClassNode next = i < interfacesToGenerateForwarderFor.length - 1 ? interfacesToGenerateForwarderFor[i + 1] : null;
String forwarderName = Traits.getSuperTraitMethodName(current, forwarderMethod.getName());
if (targetNode.getDeclaredMethod(forwarderName, superForwarderParams) == null) {
ClassNode returnType = correctToGenericsSpecRecurse(genericsSpec, forwarderMethod.getReturnType());
Statement delegate = next == null ? createSuperFallback(forwarderMethod, returnType) : createDelegatingForwarder(forwarderMethod, next);
MethodNode methodNode = targetNode.addMethod(forwarderName, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, returnType, superForwarderParams, ClassNode.EMPTY_ARRAY, delegate);
methodNode.setGenericsTypes(forwarderMethod.getGenericsTypes());
}
}
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class StatementWriter method visitExpressionOrStatement.
private void visitExpressionOrStatement(Object o) {
if (o == EmptyExpression.INSTANCE)
return;
if (o instanceof Expression) {
Expression expr = (Expression) o;
int mark = controller.getOperandStack().getStackLength();
expr.visit(controller.getAcg());
controller.getOperandStack().popDownTo(mark);
} else {
((Statement) o).visit(controller.getAcg());
}
}
use of org.codehaus.groovy.ast.stmt.Statement 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.Statement in project groovy by apache.
the class BindableASTTransformation method createListenerSetter.
private void createListenerSetter(ClassNode classNode, PropertyNode propertyNode) {
String setterName = "set" + MetaClassHelper.capitalize(propertyNode.getName());
if (classNode.getMethods(setterName).isEmpty()) {
Statement setterBlock = createBindableStatement(propertyNode, fieldX(propertyNode.getField()));
// create method void <setter>(<type> fieldName)
createSetterMethod(classNode, propertyNode, setterName, setterBlock);
} else {
wrapSetterMethod(classNode, propertyNode.getName());
}
}
use of org.codehaus.groovy.ast.stmt.Statement 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);
}
}
Aggregations