use of org.codehaus.groovy.ast.expr.FieldExpression in project groovy-core by groovy.
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.FieldExpression in project groovy-core by groovy.
the class IfElseTest method testLoop.
public void testLoop() throws Exception {
ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
classNode.addProperty(new PropertyNode("result", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
BooleanExpression expression = new BooleanExpression(new BinaryExpression(new FieldExpression(new FieldNode("bar", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("==", 0, 0), new ConstantExpression("abc")));
Statement trueStatement = new ExpressionStatement(new BinaryExpression(new FieldExpression(new FieldNode("result", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("=", 0, 0), new ConstantExpression("worked")));
Statement falseStatement = createPrintlnStatement(new ConstantExpression("false"));
IfStatement statement = new IfStatement(expression, trueStatement, falseStatement);
classNode.addMethod(new MethodNode("ifDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statement));
Class fooClass = loadClass(classNode);
assertTrue("Loaded a new class", fooClass != null);
Object bean = fooClass.newInstance();
assertTrue("Managed to create bean", bean != null);
assertSetProperty(bean, "bar", "abc");
System.out.println("################ Now about to invoke method");
Object[] array = {};
InvokerHelper.invokeMethod(bean, "ifDemo", array);
System.out.println("################ Done");
assertGetProperty(bean, "result", "worked");
}
use of org.codehaus.groovy.ast.expr.FieldExpression in project groovy by apache.
the class ClassNode method positionStmtsAfterEnumInitStmts.
public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) {
MethodNode constructor = getOrAddStaticConstructorNode();
Statement statement = constructor.getCode();
if (statement instanceof BlockStatement) {
BlockStatement block = (BlockStatement) statement;
// add given statements for explicitly declared static fields just after enum-special fields
// are found - the $VALUES binary expression marks the end of such fields.
List<Statement> blockStatements = block.getStatements();
ListIterator<Statement> litr = blockStatements.listIterator();
while (litr.hasNext()) {
Statement stmt = litr.next();
if (stmt instanceof ExpressionStatement && ((ExpressionStatement) stmt).getExpression() instanceof BinaryExpression) {
BinaryExpression bExp = (BinaryExpression) ((ExpressionStatement) stmt).getExpression();
if (bExp.getLeftExpression() instanceof FieldExpression) {
FieldExpression fExp = (FieldExpression) bExp.getLeftExpression();
if (fExp.getFieldName().equals("$VALUES")) {
for (Statement tmpStmt : staticFieldStatements) {
litr.add(tmpStmt);
}
}
}
}
}
}
}
use of org.codehaus.groovy.ast.expr.FieldExpression in project groovy by apache.
the class TraitReceiverTransformer method transform.
@Override
public Expression transform(final Expression exp) {
ClassNode weavedType = weaved.getOriginType();
if (exp instanceof BinaryExpression) {
return transformBinaryExpression((BinaryExpression) exp, weavedType);
} else if (exp instanceof StaticMethodCallExpression) {
StaticMethodCallExpression call = (StaticMethodCallExpression) exp;
ClassNode ownerType = call.getOwnerType();
if (traitClass.equals(ownerType)) {
MethodCallExpression mce = callX(varX(weaved), call.getMethod(), transform(call.getArguments()));
mce.setSafe(false);
mce.setSpreadSafe(false);
mce.setImplicitThis(false);
mce.setSourcePosition(exp);
return mce;
}
} else if (exp instanceof MethodCallExpression) {
MethodCallExpression mce = (MethodCallExpression) exp;
String obj = mce.getObjectExpression().getText();
if (mce.isImplicitThis() || "this".equals(obj)) {
return transformMethodCallOnThis(mce);
} else if ("super".equals(obj)) {
return transformSuperMethodCall(mce);
}
} else if (exp instanceof FieldExpression) {
FieldNode fn = ((FieldExpression) exp).getField();
return transformFieldReference(exp, fn, fn.isStatic());
} else if (exp instanceof VariableExpression) {
VariableExpression vexp = (VariableExpression) exp;
Variable accessedVariable = vexp.getAccessedVariable();
if (accessedVariable instanceof FieldNode || accessedVariable instanceof PropertyNode) {
if (knownFields.contains(vexp.getName())) {
boolean isStatic = Modifier.isStatic(accessedVariable.getModifiers());
return transformFieldReference(exp, accessedVariable instanceof FieldNode ? (FieldNode) accessedVariable : ((PropertyNode) accessedVariable).getField(), isStatic);
} else {
PropertyExpression propertyExpression = propX(varX(weaved), vexp.getName());
propertyExpression.getProperty().setSourcePosition(exp);
return propertyExpression;
}
} else if (accessedVariable instanceof DynamicVariable && !inClosure) {
// GROOVY-9386
PropertyExpression propertyExpression = propX(varX(weaved), vexp.getName());
propertyExpression.getProperty().setSourcePosition(exp);
return propertyExpression;
}
if (vexp.isThisExpression()) {
VariableExpression variableExpression = varX(weaved);
variableExpression.setSourcePosition(exp);
return variableExpression;
}
if (vexp.isSuperExpression()) {
throwSuperError(vexp);
}
} else if (exp instanceof PropertyExpression) {
PropertyExpression pexp = (PropertyExpression) exp;
String obj = pexp.getObjectExpression().getText();
if (pexp.isImplicitThis() || "this".equals(obj)) {
String propName = pexp.getPropertyAsString();
if (knownFields.contains(propName)) {
FieldNode fn = new FieldNode(propName, 0, ClassHelper.OBJECT_TYPE, weavedType, null);
return transformFieldReference(exp, fn, false);
}
}
} else if (exp instanceof ClosureExpression) {
MethodCallExpression mce = callX(exp, "rehydrate", args(varX(weaved), varX(weaved), varX(weaved)));
mce.setImplicitThis(false);
mce.setSourcePosition(exp);
boolean oldInClosure = inClosure;
inClosure = true;
((ClosureExpression) exp).getCode().visit(this);
inClosure = oldInClosure;
// The rewrite we do is causing some troubles with type checking, which will
// not be able to perform closure parameter type inference
// so we store the replacement, which will be done *after* type checking.
exp.putNodeMetaData(TraitASTTransformation.POST_TYPECHECKING_REPLACEMENT, mce);
return exp;
}
// TODO: unary expressions (field++, field+=, ...)
return super.transform(exp);
}
use of org.codehaus.groovy.ast.expr.FieldExpression in project groovy by apache.
the class Verifier method moveOptimizedConstantsInitialization.
private static boolean moveOptimizedConstantsInitialization(final ClassNode node) {
if (node.isInterface() && !Traits.isTrait(node))
return false;
String name = SWAP_INIT;
int mods = ACC_STATIC | ACC_SYNTHETIC | ACC_PUBLIC;
BlockStatement methodCode = block(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 = fieldX(fn);
if (fn.getType().equals(ClassHelper.REFERENCE_TYPE))
fe.setUseReferenceDirectly(true);
ConstantExpression init = (ConstantExpression) fn.getInitialExpression();
init = constX(init.getValue(), true);
Statement statement = stmt(binX(fe, Token.newSymbol(Types.ASSIGN, 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