use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class TraitReceiverTransformer method transformBinaryExpression.
private Expression transformBinaryExpression(final BinaryExpression exp, final ClassNode weavedType) {
Expression leftExpression = exp.getLeftExpression();
Expression rightExpression = exp.getRightExpression();
Token operation = exp.getOperation();
if (operation.getText().equals("=")) {
String leftFieldName = null;
// it's an assignment
if (leftExpression instanceof VariableExpression && ((VariableExpression) leftExpression).getAccessedVariable() instanceof FieldNode) {
leftFieldName = ((VariableExpression) leftExpression).getAccessedVariable().getName();
} else if (leftExpression instanceof FieldExpression) {
leftFieldName = ((FieldExpression) leftExpression).getFieldName();
} else if (leftExpression instanceof PropertyExpression && (((PropertyExpression) leftExpression).isImplicitThis() || "this".equals(((PropertyExpression) leftExpression).getObjectExpression().getText()))) {
leftFieldName = ((PropertyExpression) leftExpression).getPropertyAsString();
FieldNode fn = tryGetFieldNode(weavedType, leftFieldName);
if (fieldHelper == null || fn == null && !fieldHelper.hasPossibleMethod(Traits.helperSetterName(new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null)), rightExpression)) {
return createAssignmentToField(rightExpression, operation, leftFieldName);
}
}
if (leftFieldName != null) {
FieldNode fn = weavedType.getDeclaredField(leftFieldName);
FieldNode staticField = tryGetFieldNode(weavedType, leftFieldName);
if (fn == null) {
fn = new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null);
}
Expression receiver = createFieldHelperReceiver();
boolean isStatic = staticField != null && staticField.isStatic();
if (fn.isStatic()) {
// DO NOT USE isStatic variable here!
receiver = new PropertyExpression(receiver, "class");
}
String method = Traits.helperSetterName(fn);
MethodCallExpression mce = new MethodCallExpression(receiver, method, new ArgumentListExpression(super.transform(rightExpression)));
mce.setSourcePosition(exp);
mce.setImplicitThis(false);
markDynamicCall(mce, staticField, isStatic);
return mce;
}
}
Expression leftTransform = transform(leftExpression);
Expression rightTransform = transform(rightExpression);
Expression ret = exp instanceof DeclarationExpression ? new DeclarationExpression(leftTransform, operation, rightTransform) : new BinaryExpression(leftTransform, operation, rightTransform);
ret.setSourcePosition(exp);
ret.copyNodeMetaData(exp);
return ret;
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class EqualsAndHashCodeASTTransformation method bothSelfRecursiveFieldX.
private static BinaryExpression bothSelfRecursiveFieldX(FieldNode fNode, Expression other) {
final Expression fieldExpr = varX(fNode);
final Expression otherExpr = propX(other, fNode.getName());
return andX(sameX(fieldExpr, varX("this")), sameX(otherExpr, other));
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class SuperCallTraitTransformer method transformBinaryExpression.
private Expression transformBinaryExpression(final BinaryExpression exp) {
Expression trn = super.transform(exp);
if (trn instanceof BinaryExpression) {
BinaryExpression bin = (BinaryExpression) trn;
Expression leftExpression = bin.getLeftExpression();
if (bin.getOperation().getType() == Types.EQUAL && leftExpression instanceof PropertyExpression) {
ClassNode traitReceiver = null;
PropertyExpression leftPropertyExpression = (PropertyExpression) leftExpression;
if (isTraitSuperPropertyExpression(leftPropertyExpression.getObjectExpression())) {
PropertyExpression pexp = (PropertyExpression) leftPropertyExpression.getObjectExpression();
traitReceiver = pexp.getObjectExpression().getType();
}
if (traitReceiver != null) {
// A.super.foo = ...
TraitHelpersTuple helpers = Traits.findHelpers(traitReceiver);
ClassNode helper = helpers.getHelper();
String setterName = MetaProperty.getSetterName(leftPropertyExpression.getPropertyAsString());
List<MethodNode> methods = helper.getMethods(setterName);
for (MethodNode method : methods) {
Parameter[] parameters = method.getParameters();
if (parameters.length == 2 && parameters[0].getType().equals(traitReceiver)) {
ArgumentListExpression args = new ArgumentListExpression(new VariableExpression("this"), transform(exp.getRightExpression()));
MethodCallExpression setterCall = new MethodCallExpression(new ClassExpression(helper), setterName, args);
setterCall.setMethodTarget(method);
setterCall.setImplicitThis(false);
return setterCall;
}
}
return bin;
}
}
}
return trn;
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project spock by spockframework.
the class AbstractDeepBlockRewriter method visitBinaryExpression.
@Override
public final void visitBinaryExpression(BinaryExpression expr) {
BinaryExpression oldBinaryExpression = currBinaryExpr;
currBinaryExpr = expr;
try {
doVisitBinaryExpression(expr);
} finally {
currBinaryExpr = oldBinaryExpression;
}
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy-core by groovy.
the class Verifier method moveOptimizedConstantsInitialization.
private boolean moveOptimizedConstantsInitialization(final ClassNode node) {
if (node.isInterface() && !Traits.isTrait(node))
return false;
final int mods = Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC;
String name = SWAP_INIT;
BlockStatement methodCode = new BlockStatement();
methodCode.addStatement(new SwapInitStatement());
boolean swapInitRequired = false;
for (FieldNode fn : node.getFields()) {
if (!fn.isStatic() || !fn.isSynthetic() || !fn.getName().startsWith("$const$"))
continue;
if (fn.getInitialExpression() == null)
continue;
final FieldExpression fe = new FieldExpression(fn);
if (fn.getType().equals(ClassHelper.REFERENCE_TYPE))
fe.setUseReferenceDirectly(true);
ConstantExpression init = (ConstantExpression) fn.getInitialExpression();
init = new ConstantExpression(init.getValue(), true);
ExpressionStatement statement = new ExpressionStatement(new BinaryExpression(fe, Token.newSymbol(Types.EQUAL, fn.getLineNumber(), fn.getColumnNumber()), init));
fn.setInitialValueExpression(null);
methodCode.addStatement(statement);
swapInitRequired = true;
}
if (swapInitRequired) {
node.addSyntheticMethod(name, mods, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, methodCode);
}
return swapInitRequired;
}
Aggregations